Skip to content

feat(py): make ModelRef generic over ModelRefConfigT - #5969

Merged
huangjeff5 merged 8 commits into
genkit-ai:mainfrom
huangjeff5:jh-generic-model-ref
Aug 13, 2026
Merged

huangjeff5 merged 8 commits into
genkit-ai:mainfrom
huangjeff5:jh-generic-model-ref

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

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

from genkit.model import model_ref

ref = model_ref(
    'gemini-2.5-flash',
    namespace='googleai',
    config_schema=GeminiConfig,
    config=GeminiConfig(temperature=0.7),
)

Your editor autocompletes ref.config when 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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@huangjeff5
huangjeff5 requested review from apascal07 and pavelgj August 6, 2026 15:26
@huangjeff5
huangjeff5 force-pushed the jh-generic-model-ref branch from 3af5497 to 388ccea Compare August 6, 2026 17:28
@huangjeff5

Copy link
Copy Markdown
Contributor Author

@gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@huangjeff5
huangjeff5 force-pushed the jh-generic-model-ref branch from 388ccea to 3af5497 Compare August 6, 2026 17:35
@huangjeff5
huangjeff5 requested a review from apascal07 August 13, 2026 17:47
@huangjeff5
huangjeff5 merged commit 4d87359 into genkit-ai:main Aug 13, 2026
24 checks passed
@huangjeff5
huangjeff5 deleted the jh-generic-model-ref branch August 13, 2026 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants