Skip to content

fix(py)!: one declaration per common config field; non-text configs extend GenkitConfig - #6978

Merged
huangjeff5 merged 2 commits into
mainfrom
fix/py-common-config-fields
Nov 3, 2026
Merged

huangjeff5 merged 2 commits into
mainfrom
fix/py-common-config-fields

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Stack 1/2. Base PR on main. #6712 stacks on this.

Each common config setting has one Python name, declared once on ModelConfig (temperature, max_output_tokens, top_k, top_p, stop_sequences, version). Plugins map the common field to their wire spelling instead of re-declaring it or adding a provider-specific alias; raw wire fields still pass straight through extra.

On main that didn't hold:

  • OpenAIConfig had max_tokens, max_completion_tokens and stop next to max_output_tokens and stop_sequences, plus a rule for which one wins. fix(py/ollama)!: per-request key comes from context.secrets; OllamaConfig declares all sampler options #6643 copied the pattern into OllamaConfig (num_predict, stop).
  • The Dev UI form showed both names, with nothing saying which applies.
  • GeminiConfig re-declared five common fields to tighten bounds, behind pyright/pyrefly ignores.
  • ModelConfig inherited the generated GenerationCommonConfig, which types token counts as float.
  • OpenAI's image, speech and transcription configs extended ModelConfig, so temperature on dall-e-3 was accepted and silently dropped.
# --- BEFORE ---
# 1. Two names for the reply cap; the native one wins
await ai.generate(model='ollama/llama3.2', prompt='Suggest a dish.', config={'num_predict': 64, 'max_output_tokens': 256})
# => options={'num_predict': 64}

# 2. A text knob on an image model is accepted and dropped
await ai.generate(model='openai/dall-e-3', prompt='A tartine.', config={'temperature': 0.2})
# => image, temperature ignored

# --- AFTER ---
# 1. One name per setting; the plugin picks the wire name
await ai.generate(model='ollama/llama3.2', prompt='Suggest a dish.', config={'max_output_tokens': 256, 'stop_sequences': ['###']})
# => options={'num_predict': 256, 'stop': ['###']}
await ai.generate(model='openai/o3-mini', prompt='Suggest a dish.', config={'max_output_tokens': 256})
# => body: max_completion_tokens=256   (max_tokens=256 on gpt-4o)

# 2. Undeclared native names raise at the top level like any unknown key
await ai.generate(model='openai/gpt-4o', prompt='Suggest a dish.', config={'stop': ['###']})
# => GenkitError INVALID_ARGUMENT: openai/gpt-4o: unknown config key 'stop'; ...

# 3. Raw wire spellings pass through extra as usual
await ai.generate(model='openai/custom-vllm', prompt='Suggest a dish.', config={'extra': {'max_completion_tokens': 64}})
# => body: max_completion_tokens=64

# 4. Image and audio configs take only their endpoint's settings
await ai.generate(model='openai/dall-e-3', prompt='A tartine.', config={'temperature': 0.2})
# => GenkitError INVALID_ARGUMENT: openai/dall-e-3: unknown config key 'temperature'; ...

# 5. Provider ranges still apply to the inherited field
await ai.generate(model='openai/gpt-4o', prompt='Suggest a dish.', config={'temperature': 3.0})
# => GenkitError INVALID_ARGUMENT: openai/gpt-4o: config 'temperature': temperature must be between 0.0 and 2.0, got 3.0

Main Changes

  • Core owns the common field types.
    • ModelConfig now extends GenkitConfig and declares the six fields itself, with max_output_tokens: int and top_k: int, matching Go.
    • The generated GenerationCommonConfig stays as the schema's record. A test pins ModelConfig's names and wire aliases to it.
    • JSON has one number type, so the wire format is unchanged: 256.0 validates, 2.5 does not.
    • The int(...) casts in Anthropic, Bedrock and OpenAI are gone.
  • Provider ranges without re-declaring the field.
    • A plugin narrows a field with common_field_bounds = {'temperature': (0.0, 2.0)}. This is new public API for plugin authors.
    • ModelConfig checks it in a field validator, so a violation is the usual INVALID_ARGUMENT on that field, with error type common_field_bounds. It also writes minimum/maximum into the Dev UI schema.
    • A bad key or an inverted range raises when the subclass is defined.
    • Gemini and OpenAI use it. Gemma narrows only top_p.
  • Provider spellings are omitted at the top level; extra stays a raw passthrough.
    • num_predict/stop on Ollama and max_tokens/max_completion_tokens/stop on OpenAI are omitted from the config schema, so top-level use fails via GenkitConfig's extra='forbid' without per-plugin special cases.
    • extra passes raw wire fields through untouched (matching Anthropic), giving OpenAI-compatible servers an explicit override when model-name heuristics pick the wrong token-cap field.
    • No deprecation aliases: an alias puts two names back in the Dev UI and Python API.
  • Non-text configs extend GenkitConfig.
    • OpenAIDalleConfig, OpenAIGptImageConfig, OpenAITtsConfig and OpenAISttConfig declare only what their endpoint reads, like VeoConfig.
    • STT declares temperature, so 0 is now sent, and translate, which the model already read but the schema rejected.
    • These endpoints reject version; name the model directly.
    • GeminiTtsConfig/GeminiImageConfig stay on GeminiConfig, because generateContent takes the sampling knobs.
  • A guard keeps it that way. py/tests/config_field_guard_test.py walks every config class in the Bedrock, Anthropic, Google GenAI, Ollama, OpenAI and Vertex AI plugins. It fails when a plugin class re-annotates a common field or declares a known native alias. It reads annotations with inspect.get_annotations, so it still checks on 3.14 (PEP 649), and positive-control tests prove it flags a re-declared field.

Breaking

  • ModelConfig no longer subclasses GenerationCommonConfig. max_output_tokens and top_k are int | None, and a fractional value raises.
  • OllamaConfig: num_predict and stop removed from top-level config. Use max_output_tokens and stop_sequences (raw options passthrough via extra still works).
  • OpenAIConfig: max_tokens, max_completion_tokens and stop removed from top-level config.
    • Use max_output_tokens and stop_sequences, or pass raw body fields in extra (e.g., to force max_tokens vs max_completion_tokens on an OpenAI-compatible server). max_output_tokens is sent as max_completion_tokens on reasoning models or with reasoning_effort, else as max_tokens.
    • A bare-string stop at the top level is gone.
  • OpenAI image, TTS and STT configs extend GenkitConfig. Text knobs (except temperature on STT) and version raise instead of being dropped.
  • Out-of-range values:
    • Gemini fails with error type common_field_bounds and temperature must be between …, not pydantic's less_than_equal/greater_than_equal.
    • OpenAI temperature/top_p are now range-checked locally instead of being sent to the API.
    • A served flow reports these as Invalid value, like any custom validator error, not pydantic's built-in range text.
  • The six common fields now carry descriptions in every plugin's Dev UI form (Anthropic, Bedrock and Ollama too), not only Gemini/OpenAI.
  • Gemini's temperature/top_p schema is now nullable (anyOf: [number, null]), with minimum/maximum beside it.

@github-actions github-actions Bot added docs Improvements or additions to documentation python Python fix labels Oct 30, 2026

@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 model configuration across multiple plugins (including Amazon Bedrock, Anthropic, Google GenAI, Ollama, and OpenAI) by consolidating common fields into a unified ModelConfig base class in genkit core. Subclasses now use a common_field_bounds dictionary to specify provider-specific ranges rather than re-declaring fields, and native provider-specific names are rejected in favor of the common fields. Feedback on the changes suggests using .get() when retrieving fields from cls.model_fields to prevent potential KeyError exceptions if common_field_bounds is misconfigured.

@pavelgj pavelgj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with comments

@huangjeff5
huangjeff5 force-pushed the fix/py-common-config-fields branch from 84707c6 to 96a7e2c Compare November 2, 2026 23:03
…xtend GenkitConfig

ModelConfig declares the common fields itself (int token counts) instead of
inheriting the generated GenerationCommonConfig. Plugins no longer re-declare
or alias them: Gemini/OpenAI ranges move to common_field_bounds, Ollama drops
num_predict/stop, OpenAI drops max_tokens/max_completion_tokens/stop (top-level
use fails as an unknown config key; extra passes raw wire fields through).
OpenAI image, TTS and STT configs extend GenkitConfig. A repo-level guard test
enforces the rule.
@huangjeff5
huangjeff5 force-pushed the fix/py-common-config-fields branch from 96a7e2c to 2769d65 Compare November 2, 2026 23:46
@huangjeff5
huangjeff5 merged commit 6ecfd2e into main Nov 3, 2026
37 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-common-config-fields branch November 3, 2026 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation fix python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants