Repository navigation
feat(py): make ModelRef generic over ModelRefConfigT - #5969
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors ModelRef from a Pydantic BaseModel to a frozen dataclass parameterized with a generic Pydantic config schema (ModelRefConfigT), and updates the model_ref helper function to require an explicit config_schema. It also adds comprehensive unit tests to verify its behavior. The reviewer feedback suggests several valuable improvements: allowing model_ref to accept a raw dictionary for config and automatically coercing it to the specified Pydantic model at runtime, adding a default value of BaseModel to ModelRefConfigT for simpler type hinting, and adding a corresponding unit test to verify dictionary coercion.
3af5497 to
388ccea
Compare
|
@gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces strong typing and parameterization for model configurations and references across Genkit's Python SDK. Key changes include refactoring ModelRef as a frozen dataclass generic over its configuration schema, parameterizing ModelRequest and background model actions with ModelRequestConfigT, and updating the middleware chain to support returning Operation objects from background models. Additionally, model resolution has been improved to fall back to background models and provide clearer error messages when plugin namespaces are missing. A comprehensive suite of unit tests has been added to verify these typing, validation, and error-handling improvements. I have no feedback to provide as there are no review comments to assess.
388ccea to
3af5497
Compare
What this does
Makes ModelRef a typed, immutable reference to a model, generic over its config schema. A plugin can now export refs like gemini_25_flash: ModelRef[GeminiConfig], and users who pass a config of the wrong shape find out immediately — at construction, with a clear error — instead of deep inside a request, or worse, silently.
For SDK users
Your editor autocompletes
ref.configwhen the ref is ModelRef[GeminiConfig], and rejects a dict passed in as config.Passing a config that isn't an instance of config_schema raises GenkitError with status INVALID_ARGUMENT at construction time, naming the expected and actual types.
Refs are frozen: mutating a field raises FrozenInstanceError, so a ref shared across your codebase can't be changed out from under you. Two refs with the same fields compare equal.
For plugin authors
Export typed refs alongside your models. ModelRefConfigT is covariant and bound to pydantic.BaseModel, so a ModelRef[GeminiConfig] is assignable wherever a ModelRef[BaseModel] is accepted.
Breaking changes
model_ref() now requires a config_schema keyword argument, and all arguments after name are keyword-only. Before → after: model_ref('m', namespace='ns') → model_ref('m', namespace='ns', config_schema=MyConfig).
config must be an instance of that schema (previously a plain dict).
ModelRef is now an unhashable frozen dataclass rather than a mutable Pydantic model; use its fields, not .model_dump().
Notes
This is the foundation PR of a stack extracted from #5854 / the original #5965: it deliberately changes no call sites. Accepting ModelRef at generate(), define_prompt(), and agent surfaces lands separately (#5977), on top of a small helpers PR that carries the config-merge semantics.
Test plan
py/packages/genkit/tests/genkit/ai/model_ref_test.py covers construction, namespace prefixing, typed-config validation (including the INVALID_ARGUMENT path), frozen-ness, equality, and the missing-kwarg TypeError.