Skip to content

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

Closed
huangjeff5 wants to merge 39 commits into
genkit-ai:mainfrom
huangjeff5:feat/py-generic-model-ref
Closed

huangjeff5 wants to merge 39 commits into
genkit-ai:mainfrom
huangjeff5:feat/py-generic-model-ref

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

What's the point of this PR?

This PR extracts the generic ModelRef type refactoring out of broader feature branches (like background models in #5854) into a clean, standalone PR.

It introduces generic parameterization (ModelRef[ModelRefConfigT]) so model references across plugins (Gemini, Claude, Ollama, etc.) carry their specific Pydantic configuration schemas with full IDE autocomplete and static type safety.


Main decisions:

  1. Required config_schema:
    config_schema is intentionally a required keyword argument when creating a ModelRef or calling model_ref(). Every ModelRef in Genkit Python is intended to be explicitly typed with a configuration schema. Use a model string if you don't care about model-specific configuration.

  2. PEP 696 Default Type Parameter (default=BaseModel):
    ModelRefConfigT uses PEP 696 syntax (default=BaseModel) so callers can write bare ref: ModelRef annotations without needing explicit type parameters (e.g. ModelRef[Any]) everywhere.

  3. Immutable Dataclass (frozen=True):
    ModelRef is converted from a Pydantic BaseModel into a @dataclass(frozen=True, kw_only=True) generic object. Model references are immutable value objects once instantiated.

NOTE: There are no call-sites to model_ref currently, so the change to require config_schema arg doesn't actually break any callsites.

@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 into a generic, frozen dataclass parameterized by a configuration schema type variable ModelRefConfigT. It also updates the model_ref helper function to require a config_schema argument. The reviewer feedback highlights that making config_schema a required keyword-only argument is a breaking change. To maintain backwards compatibility, the reviewer suggests making config_schema optional in both model_ref and ModelRef, setting a default type of BaseModel for ModelRefConfigT, and adding corresponding test coverage.

@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 refactors ModelRef from a Pydantic BaseModel to a frozen generic dataclass parameterized by ModelRefConfigT (bound to BaseModel), and updates the model_ref helper function to accept and enforce this configuration schema. While these changes improve type safety, the reviewer noted that making config_schema a required keyword-only argument introduces breaking changes for existing callers. To preserve backwards compatibility, the feedback suggests making config_schema optional (defaulting to None) and defining a default type for ModelRefConfigT using PEP 696 syntax.

@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 refactors ModelRef from a Pydantic BaseModel to a frozen generic dataclass parameterized by a Pydantic config schema (ModelRefConfigT), and updates the model_ref helper function accordingly. Feedback suggests making config_schema optional with a default of None in both ModelRef and model_ref to preserve backward compatibility and align with the class's docstring.

@huangjeff5
huangjeff5 requested review from apascal07 and pavelgj August 6, 2026 01:43
@huangjeff5
huangjeff5 marked this pull request as draft August 6, 2026 04:18
@huangjeff5
huangjeff5 force-pushed the feat/py-generic-model-ref branch from 0d3979d to ef03c8c Compare August 6, 2026 04:34
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.

1 participant