Repository navigation
feat(py): make ModelRef generic over ModelRefConfigT - #5965
huangjeff5 wants to merge 39 commits into
Conversation
There was a problem hiding this comment.
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.
|
@gemini review |
There was a problem hiding this comment.
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.
|
@gemini review |
There was a problem hiding this comment.
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.
…(handled by Mapping[str, Any])
…prompt overload matching
…nerate and generate_stream
0d3979d to
ef03c8c
Compare
…oad serialization
… model_ref_test.py
What's the point of this PR?
This PR extracts the generic
ModelReftype 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:
Required
config_schema:config_schemais intentionally a required keyword argument when creating aModelRefor callingmodel_ref(). EveryModelRefin 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.PEP 696 Default Type Parameter (
default=BaseModel):ModelRefConfigTuses PEP 696 syntax (default=BaseModel) so callers can write bareref: ModelRefannotations without needing explicit type parameters (e.g.ModelRef[Any]) everywhere.Immutable Dataclass (
frozen=True):ModelRefis converted from a PydanticBaseModelinto 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_refcurrently, so the change to requireconfig_schemaarg doesn't actually break any callsites.