Skip to content

feat(py): accept ModelRef on generate, prompt, agent, and Genkit constructor - #6074

Merged
huangjeff5 merged 9 commits into
py-model-resolution-helpersfrom
jh-model-ref-generate2
Aug 21, 2026
Merged

huangjeff5 merged 9 commits into
py-model-resolution-helpersfrom
jh-model-ref-generate2

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Product

One model= slot, two shapes — a name or a ModelRef — on every surface:

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

ai = Genkit(model=flash)
await ai.generate(prompt='hi')
# version=001, temperature=0.7

await ai.generate(model=flash, prompt='hi', config={'temperature': 0.2})
# version=001, temperature=0.2

joke = ai.define_prompt(model=flash, prompt='hi')
await joke()

flash is a wire name plus defaults. A string is just the name. Same slot on Genkit(), generate, generate_stream, generate_operation, define_prompt, and agents.

The merge lives in #6015. This PR opens the slots and pins the generate-level cases that layer was built for.

What generate has to survive

# Call-time keys win; None clears a ref default; 0 / '' / [] / False stay.
await ai.generate(model=flash, config={'temperature': 0.2})
await ai.generate(model=flash, config={'temperature': None})
await ai.generate(model=flash, config={'temperature': 0, 'stop_sequences': []})

# Either spelling is the same knob. The ref set max_output_tokens=100; the
# call's maxOutputTokens folds onto that slot and wins — the plugin sees 5,
# with no leftover sibling spelling in extras.
await ai.generate(model=flash, config={'maxOutputTokens': 5})
await ai.generate(model=flash, config={'maxOutputTokens': None})           # clears
await ai.generate(model=flash, config={'max_output_tokens': 1,
                                       'maxOutputTokens': 5})              # one slot; later key wins

# Nested bag replaces whole — the ref's thinking fields are gone. The outer
# key folds (thinkingConfig -> thinking_config); inner keys stay as written
# and the nested schema parses them at the plugin edge.
await ai.generate(model=flash, config={'thinkingConfig': {'thinkingBudget': 256}})

# Leftover keys stay when the model changes (bag is on the prompt, not the old ref).
joke = ai.define_prompt(model=flash, config=GeminiConfig(safety_settings=...), prompt='hi')
await joke(model=openai_gpt)

# Explicit name is a different model; constructor knobs do not leak onto it.
ai = Genkit(model=flash)
await ai.generate(model='openai/gpt-4o', prompt='hi')

# '' is omitted (unset MODEL=). 123 is a hard error, not the default.
await ai.generate(model='', prompt='hi')
await ai.generate(model=123, prompt='hi')  # INVALID_ARGUMENT

ref.version is copied into config.version as the lowest layer. Overlaying temperature does not drop it. ref.config.version beats the constructor version= field.

The same overlay runs on the constructor default (Genkit(model=flash) + camel call dict) and on prompts (joke(config={'maxOutputTokens': 5})).

Also

ExecutablePrompt merges defined config with per-call config using exclude_unset (same as generate): untouched fields cannot clobber defaults; an explicit None clears.

generate_operation() accepts a ModelRef and uses the same merge. Looking up a background model (BACKGROUND_MODEL, e.g. Veo) is #5854.

What this does not fix

  • ModelRequest(config=dict) still rehydrates as generic ModelConfig. That's feat(py): Make ModelRequest generic #5986.
  • Inner keys of a nested bag stay as written; the nested schema is what accepts or rejects them at the plugin edge.
  • With a bare string model= there is no schema, so nothing folds — dict keys pass through as written.

@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 core interfaces including generate, generate_stream, define_prompt, define_agent, and generate_operation. It adds a new helper resolve_call_model to handle model name resolution and configuration merging, alongside comprehensive unit tests. The review feedback highlights two key improvement opportunities: first, stripping None values from Mapping configurations when resolving string models to prevent validation errors in plugins; and second, removing a redundant 'resume': resume update in render_prompt_config_for_executable_call since PromptConfig does not define a resume field.

@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate2 branch from 699b502 to 0ef598b Compare August 15, 2026 16:59
@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 support for ModelRef and improved configuration merging across Genkit's Python SDK, specifically within agents, prompts, generation, and operations. It adds the resolve_call_model helper to handle model name resolution and configuration merging for ModelRef instances while preserving the original configuration objects for string-based models. Type annotations have been updated to accommodate these changes, and comprehensive unit tests have been added to verify the new behavior. No review comments were provided, so there is no additional feedback to provide.

@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate2 branch from c312a3d to 19964a9 Compare August 19, 2026 02:51
@huangjeff5 huangjeff5 changed the title feat(py): accept ModelRef on generate, prompt, and agent surfaces feat(py): accept ModelRef on generate, prompt, agent, and Genkit constructor Aug 19, 2026
@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate2 branch from db87e49 to 7be9ecf Compare August 19, 2026 04:03
@huangjeff5
huangjeff5 requested a review from apascal07 August 19, 2026 04:04
@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate2 branch from 7bf5459 to dd431a4 Compare August 19, 2026 04:25
@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate2 branch from dd431a4 to aef3202 Compare August 19, 2026 04:27
@huangjeff5
huangjeff5 requested a review from pavelgj August 19, 2026 04:49
@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate2 branch from aef3202 to 354d2f6 Compare August 19, 2026 05:15
@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate2 branch 2 times, most recently from b904e2f to 2bb35ef Compare August 19, 2026 20:33
@huangjeff5
huangjeff5 force-pushed the jh-model-ref-generate2 branch 2 times, most recently from de76436 to 5442b96 Compare August 19, 2026 20:52
Support passing ModelRef across generate, generate_stream,
generate_operation, define_prompt, and define_agent surfaces, merging
model defaults with call-time configuration.

Includes end-to-end tests for ModelRef resolution, config merging,
explicit None clearing, unknown key pass-through, version override, and
covariance verification for ModelRef[BaseModel].
@huangjeff5
huangjeff5 merged commit ba384f5 into main Aug 21, 2026
24 checks passed
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