Skip to content

feat(py): accept ModelRef on generate, prompt, and agent surfaces - #5977

Closed
huangjeff5 wants to merge 9 commits into
py-model-resolution-helpersfrom
jh-model-ref-generate
Closed

huangjeff5 wants to merge 9 commits into
py-model-resolution-helpersfrom
jh-model-ref-generate

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

What this does

You can now pass a ModelRef — not just a string — anywhere the SDK takes a model: ai.generate(), ai.generate_stream(), ai.generate_operation(), ai.define_prompt(), and agent definitions. The ref's defaults (version, config) merge with your call-time config under one documented precedence rule, matching the JS SDK.

For SDK users

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

# Ref defaults apply:
await ai.generate(model=flash, prompt='hi')
# temperature=0.7

# Call-time config wins per key:
await ai.generate(model=flash, prompt='hi', config={'temperature': 0.2})
# temperature=0.2

# Explicitly passing None clears a ref default:
await ai.generate(model=flash, prompt='hi', config={'temperature': None})
# temperature not sent

Merge precedence, lowest to highest: ref.version → ref.config → call-time config. Unknown config keys pass through to the plugin untouched (no stripping or validation at this layer). After merging, keys whose value is None are removed, so plugins see an absent key rather than null. Plain string model names behave exactly as before.

generate_operation() gets the same treatment, so background models can be invoked via a typed ref too.

Behavior change to prompt config merging

ExecutablePrompt previously merged its defined config with per-call config using exclude_none dumps. It now uses exclude_unset semantics, aligning prompts with generate():

Fields you never set on a Pydantic config no longer participate in the merge (they can't clobber lower-precedence values with implicit Nones).
A field you explicitly set to None now survives the merge and clears the inherited default (previously it was dropped).
If you relied on explicit Nones being ignored in prompt config overrides, this changes your behavior.

Stacked on

#5969 — typed ModelRef (the type this PR wires in).
#6015 — normalize_config / resolve_model_name / resolve_model_ref / ResolvedModel, the pure functions that implement the merge rule above, with direct unit tests. This PR contains only the surface signatures, the dispatch into those helpers, and end-to-end tests.

Test plan

model_ref_generate_test.py (e2e): refs through generate / stream / prompt / agents / generate_operation, default-and-override merging, same-key override, explicit-None clearing via both generate and prompt, unknown-key pass-through, version seeding and override, string-model paths unchanged, and rejection of refs to non-long-running models in generate_operation.

@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 support for ModelRef across various Genkit components, including agents, prompts, generation, and operations, along with helper functions to resolve model references and merge configurations. Feedback highlights an issue where validating the merged configuration against a schema can strip or fail on the version field, and suggests extracting and re-applying version around schema validation, as well as adding a corresponding test case.

@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate branch from 38b2319 to d0f73e8 Compare August 12, 2026 03:50
@huangjeff5
huangjeff5 changed the base branch from jh-generic-model-ref to py-model-resolution-helpers August 12, 2026 03:53
@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate branch 3 times, most recently from a0fa67d to 5d71caa Compare August 12, 2026 04:26
huangjeff5 added a commit that referenced this pull request Aug 12, 2026
Extracted from #5977: ResolvedModel, normalize_config,
resolve_model_name, resolve_model_ref + ModelArg/ConfigArg aliases.
No call sites change. Merge precedence (low to high): ref.version,
ref.config, call-time config; explicit None clears lower layers.
@huangjeff5
huangjeff5 force-pushed the py-model-resolution-helpers branch from d88730a to c3f4097 Compare August 12, 2026 04:34
@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate branch 2 times, most recently from 1b2e489 to 5959a69 Compare August 13, 2026 23:20
@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate branch from 5959a69 to 0952f04 Compare August 13, 2026 23:29
…ompt

Move resolve_call_model from _aio.py into _model.py next to
resolve_model_name / resolve_model_ref, so generate and define_prompt
share it without import cycles.

Prompt still merges defined config with per-call config first; only
the ModelRef-vs-string step goes through the helper.

Add unit tests in model_resolution_test.py: string path keeps the config
instance; ModelRef path dumps and merges.
@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 resolve_call_model to handle model and configuration resolution when using ModelRef or string model names, updating define_agent, define_prompt, generate, generate_stream, and generate_operation to support ModelRef and properly merge default and call-time configurations. Comprehensive unit tests have also been added. The review feedback highlights that using ModelRef[BaseModel] in several function signatures can cause type-checking errors due to generic invariance in Python, and suggests using ModelRef[Any] instead to avoid these issues.


def resolve_call_model(
*,
model: str | ModelRef[BaseModel] | None,

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.

medium

Using ModelRef[BaseModel] can cause type-checking errors (e.g., in mypy or pyright) when passing a specific ModelRef[GeminiConfig] because generic types in Python are invariant by default. Since resolve_call_model is a non-generic helper that accepts any model reference, using ModelRef[Any] is safer and avoids invariance issues.

Suggested change
model: str | ModelRef[BaseModel] | None,
model: str | ModelRef[Any] | None,

variant: str | None = None,
model: str | None = None,
config: dict[str, object] | ModelConfig | None = None,
model: str | ModelRef[BaseModel] | None = None,

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.

medium

Using ModelRef[BaseModel] can cause type-checking errors when passing a specific ModelRef[GeminiConfig] because generic types in Python are invariant by default. Since this is the implementation signature of define_prompt, using ModelRef[Any] is safer and avoids invariance issues.

Suggested change
model: str | ModelRef[BaseModel] | None = None,
model: str | ModelRef[Any] | None = None,

self,
*,
model: str | None = None,
model: str | ModelRef[BaseModel] | None = None,

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.

medium

Using ModelRef[BaseModel] can cause type-checking errors when passing a specific ModelRef[GeminiConfig] because generic types in Python are invariant by default. Since this is the implementation signature of generate, using ModelRef[Any] is safer and avoids invariance issues.

Suggested change
model: str | ModelRef[BaseModel] | None = None,
model: str | ModelRef[Any] | None = None,

self,
*,
model: str | None = None,
model: str | ModelRef[BaseModel] | None = None,

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.

medium

Using ModelRef[BaseModel] can cause type-checking errors when passing a specific ModelRef[GeminiConfig] because generic types in Python are invariant by default. Since this is the implementation signature of generate_stream, using ModelRef[Any] is safer and avoids invariance issues.

Suggested change
model: str | ModelRef[BaseModel] | None = None,
model: str | ModelRef[Any] | None = None,

Add a unit test ensuring a ModelRef parameterized with a custom Pydantic
schema (CustomConfig) is assignable to ModelRef[BaseModel]. Because
ModelRefConfigT is defined as covariant (covariant=True), type checkers
(ty, pyright, pyrefly) accept this assignment.
@huangjeff5 huangjeff5 closed this Aug 15, 2026
huangjeff5 added a commit that referenced this pull request Aug 15, 2026
Extracted from #5977: ResolvedModel, normalize_config,
resolve_model_name, resolve_model_ref + ModelArg/ConfigArg aliases.
No call sites change. Merge precedence (low to high): ref.version,
ref.config, call-time config; explicit None clears lower layers.
huangjeff5 added a commit that referenced this pull request Aug 18, 2026
Extracted from #5977: ResolvedModel, normalize_config,
resolve_model_name, resolve_model_ref + ModelArg/ConfigArg aliases.
No call sites change. Merge precedence (low to high): ref.version,
ref.config, call-time config; explicit None clears lower layers.
huangjeff5 added a commit that referenced this pull request Aug 19, 2026
Extracted from #5977: ResolvedModel, normalize_config,
resolve_model_name, resolve_model_ref + ModelArg/ConfigArg aliases.
No call sites change. Merge precedence (low to high): ref.version,
ref.config, call-time config; explicit None clears lower layers.
huangjeff5 added a commit that referenced this pull request Aug 19, 2026
Extracted from #5977: ResolvedModel, normalize_config,
resolve_model_name, resolve_model_ref + ModelArg/ConfigArg aliases.
No call sites change. Merge precedence (low to high): ref.version,
ref.config, call-time config; explicit None clears lower layers.
huangjeff5 added a commit that referenced this pull request Aug 19, 2026
Extracted from #5977: ResolvedModel, normalize_config,
resolve_model_name, resolve_model_ref + ModelArg/ConfigArg aliases.
No call sites change. Merge precedence (low to high): ref.version,
ref.config, call-time config; explicit None clears lower layers.
huangjeff5 added a commit that referenced this pull request Aug 19, 2026
Extracted from #5977: ResolvedModel, normalize_config,
resolve_model_name, resolve_model_ref + ModelArg/ConfigArg aliases.
No call sites change. Merge precedence (low to high): ref.version,
ref.config, call-time config; explicit None clears lower layers.
huangjeff5 added a commit that referenced this pull request Aug 19, 2026
Extracted from #5977: ResolvedModel, normalize_config,
resolve_model_name, resolve_model_ref + ModelArg/ConfigArg aliases.
No call sites change. Merge precedence (low to high): ref.version,
ref.config, call-time config; explicit None clears lower layers.
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