Skip to content

feat(py): normalize model+config for str | ModelRef - #6015

Merged
huangjeff5 merged 17 commits into
mainfrom
py-model-resolution-helpers
Aug 21, 2026
Merged

huangjeff5 merged 17 commits into
mainfrom
py-model-resolution-helpers

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Product (this is the layer for #6074)

generate / define_prompt / agents / Genkit(model=) will take one model= that is a name or a ModelRef:

await ai.generate(model='googleai/gemini-2.5-flash', prompt='hi')
await ai.generate(model=flash, prompt='hi')
await ai.generate(model=flash, prompt='hi', config={'temperature': 0.2})

ai = Genkit(model=flash)
await ai.generate(prompt='hi')

flash carries a wire name plus defaults (version, config). A string is just the name. Same slot, two shapes. That union — including the constructor — is #6074.

To implement it, every path has to become one wire name + one config bag before the plugin runs. This PR is that normalization layer.

What the layer has to survive

E2e of these lives on #6074. They are listed here because they are what the helpers are for.

# 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 dumped max_output_tokens=100;
# the call's maxOutputTokens folds onto that slot and wins. No leftover sibling.
await ai.generate(model=flash, config={'maxOutputTokens': 5})
await ai.generate(model=flash, config={'maxOutputTokens': None})   # clears the ref default

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

# Switching models does not strip leftover keys (safety_settings still there).
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

With a bare string model= there is no schema, so nothing folds — dict keys pass through as written.

Principles (this PR)

The pipeline is four steps, each doing one thing:

  • Dump (normalize_config): a config object or dict becomes a plain dict. Pydantic dumps the Python field names; dict keys stay as written; no fold, no merge. api_key (exclude=True) is copied back so a per-request key still reaches the plugin.
  • Fold (fold_config_aliases): schema aliases rename to the field name (maxOutputTokens → max_output_tokens). A rename, not validation — unknown keys keep their spelling.
  • Overlay (overlay_config): fold each layer, last layer wins, drop None. Both spellings of one knob land in one slot, so a camel override or clear cannot leave a stale snake sibling.
  • Resolve (resolve_model_ref / resolve_call_model): stack ref.version → dumped ref.config → call config=, overlay once. Merge is shallow, by key. Only None means omit; empty values are explicit. The helper does not know families.

The merged result is a dict. ModelRequest rebuilds generic ModelConfig today; plugin-typed parse is #5986.

Genkit(model=) stays a string here. Dev UI /api/values unwraps a stored ModelRef to the wire name so listing does not 500; that path is ready for #6074.

What lands here vs later

@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 model resolution helper functions (normalize_config, resolve_model_name, and resolve_model_ref) along with their corresponding unit tests. The review feedback suggests enhancing type safety and robustness by explicitly checking for supported types in normalize_config, validating that the resolved model name is a string in resolve_model_name, and adding corresponding unit tests for these edge cases.

@huangjeff5
huangjeff5 force-pushed the py-model-resolution-helpers branch from d88730a to c3f4097 Compare August 12, 2026 04:34
@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 model resolution and configuration normalization helpers in the Genkit Python SDK, including the normalize_config, resolve_model_name, and resolve_model_ref functions, along with a ResolvedModel dataclass to handle merging and normalizing model configurations. It also adds a comprehensive suite of unit tests to verify these behaviors. There are no review comments, so I have no feedback to provide.

@huangjeff5
huangjeff5 changed the base branch from jh-generic-model-ref to main August 15, 2026 16:52
@huangjeff5
huangjeff5 force-pushed the py-model-resolution-helpers branch from 5006e6b to b340f97 Compare August 15, 2026 16:55
@huangjeff5 huangjeff5 changed the title feat(py): veneer model resolution helpers feat(py): normalize model+config for str | ModelRef Aug 19, 2026
@huangjeff5
huangjeff5 requested a review from apascal07 August 19, 2026 04:04
@huangjeff5
huangjeff5 force-pushed the py-model-resolution-helpers branch from 8907f22 to c73fdc3 Compare August 19, 2026 04:27
@huangjeff5
huangjeff5 requested a review from pavelgj August 19, 2026 04:49
@huangjeff5
huangjeff5 force-pushed the py-model-resolution-helpers branch 3 times, most recently from d8cbd8f to c7aa20e Compare August 19, 2026 20:37
huangjeff5 and others added 12 commits August 19, 2026 17:19
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.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
… and ormalize_config still keeps None so it can overlay a ref default
@huangjeff5
huangjeff5 force-pushed the py-model-resolution-helpers branch from 2bb58bd to c8feb38 Compare August 19, 2026 22:19
@huangjeff5
huangjeff5 merged commit eefecf7 into main Aug 21, 2026
24 checks passed
@huangjeff5
huangjeff5 deleted the py-model-resolution-helpers branch August 21, 2026 20:13
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