Skip to content

feat(py)!: config pipeline: definition defaults, one merge, None is a value - #6712

Merged
huangjeff5 merged 2 commits into
mainfrom
feat/py-config-pipeline
Nov 3, 2026
Merged

huangjeff5 merged 2 commits into
mainfrom
feat/py-config-pipeline

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Stack 2/2. Stacked on #6978. Review #6978 first.

A model's or embedder's config class, set at definition, owns the defaults and the shape. Every entry point (ai.generate, ai.embed, the Dev UI, the CLI) goes through one merge and one validation at the action boundary, and setting a field to None (config={"temperature": None}) overrides lower layers instead of being stripped (config=None still means unset).

Evaluators aren't covered: #6947 removes them from 1.0 pending a redesign.

class TableConfig(BaseModel):
    temperature: float = 0.7
    allergens: list[str] = ['peanut']

ai.define_model(name='bistro', fn=bistro, config_schema=TableConfig)

# --- BEFORE ---
await ai.generate(model='bistro', prompt='Suggest a dish.')
# typed bistro sees   => TableConfig(temperature=0.7, allergens=['peanut'])
# untyped bistro sees => {}    same model, different input depending on the fn signature
await ai.generate(model='bistro', config={'temperature': None})
# bistro sees => {}    generate stripped None; embed passed it through

# --- AFTER ---
await ai.generate(model='bistro', prompt='Suggest a dish.')
# typed bistro sees   => TableConfig(temperature=0.7, allergens=['peanut'])
# untyped bistro sees => {'temperature': 0.7, 'allergens': ['peanut']}

ref = ModelRef(name='bistro', config_schema=TableConfig, config=TableConfig(temperature=0.1))
await ai.generate(model=ref, config=TableConfig(allergens=['shellfish']))
# bistro sees => {'temperature': 0.1, 'allergens': ['shellfish']}

await ai.generate(model='bistro', config={'temperature': None})
# => GenkitError INVALID_ARGUMENT: bistro: config 'temperature': ...

Why the input used to differ

Two separate causes:

  1. Each entry point had its own merge. generate and embed layered ref and call config with separate code, and they drifted. embed let ref.version beat ref.config['version'], the reverse of generate. generate stripped None, embed passed it through.
  2. Defaults were a side effect of typing. Nothing applied schema defaults on purpose. A typed fn got them because Pydantic fills defaults while validating into TableConfig. An untyped fn got the raw dict. The Dev UI and CLI call Action.run directly and skipped generate's merge entirely.

The fix for (1) is one merge helper. The fix for (2) is moving defaults and validation into Action.run, the one point every path goes through.

The rules

  1. One class per action, set at definition. It comes from ModelRequest[Cfg] / EmbedRequest[Cfg], from config_schema=, or from both, which must then be the same class. With neither, there are no defaults and no validation.
  2. Layers, lowest to highest: schema defaults < ref.version < ref.config < call config.
    • Only non-None schema defaults count.
    • Every other layer contributes only the keys it set.
    • Keys fold to the spelling the class accepts, so maxOutputTokens and max_output_tokens are one setting.
  3. An explicit None is a value. It overrides lower layers, then validation decides. config=None still means "nothing set".
  4. Validation runs at Action.run, after middleware.
    • Call time checks only the caller's keys: bad type, unknown key under extra='forbid', or None on a non-optional field.
    • A required field nothing filled fails at the boundary, so a wrap_model middleware can still supply it.
    • generate returns that as a failed turn (finish_reason == FAILED). embed, the Dev UI and the CLI raise.
  5. Refs belong to the caller. A ref's config_schema must name the action's class, or, for models, the generic ModelConfig. A ref never adds defaults or changes the shape, because the Dev UI never has a ref.

Decisions

  • Defaults belong to the definition, not the call path. Previously an untyped fn got only the caller's fields while a typed fn got defaults from Pydantic. We inject the class's non-None defaults at the boundary, so typed and untyped fns see the same values from every entry point. Built-in plugin configs default every field to None, so their wire calls are unchanged.
  • One merge for generate and embed. EmbedderRef mirrors ModelRef, and both paths resolve the action, check the call config against its class, and layer through one helper.
  • None stops meaning "unset". None reaches validation on every path. Plugin paths that read raw dicts (OpenAI TTS/image/STT, Bedrock image, Ollama) drop None to keep plugin defaults, so their requests are unchanged.
  • Published customOptions drop "default": null. Pydantic writes it on every optional field. With None now a value, a form that pre-fills defaults would send null for every untouched field. Model, background-model and embedder metadata strip only null defaults; inputSchema is unchanged. This matches what JS publishes.

SDK parity

  • Merge order and None match JS; Go replaces wholesale. Layering version < ref.config < call and preserving explicit None match JS (js/ai/src/generate.ts, embedder.ts), whereas Go replaces config wholesale (go/ai/generate.go).
  • Schema defaults and boundary validation are Python ergonomics. Injecting config_schema field defaults and runtime INVALID_ARGUMENT validation are deliberate Python ergonomics (JS types config as z.any() and runs Ajv without useDefaults).

Breaking

  • Untyped fns with a class now receive its non-None defaults, validated on every path, including the Dev UI and CLI.
  • Untyped fns receive keys in the class's spelling: config['maxOutputTokens'] becomes config['max_output_tokens'].
  • A missing required field fails at the boundary on every path. Before, an untyped fn ran without it.
  • config={'field': None} on a non-optional field raises INVALID_ARGUMENT up front. Passing None to clear a prompt's or ref's value now fails validation on a non-optional field.
  • One carve-out for prompts called on another model: prompt(model='other', config={'thinkingConfig': None}) drops thinkingConfig from both the prompt's and the call's config when other's class doesn't declare it and the prompt's config has it. A None for a key the prompt doesn't have ('temprature') still raises unknown config key. Models compare by name, so 'gem' and ModelRef(name='gem') are the same model, not a hop. ai.generate() is unchanged.
  • Both spellings of one setting in one dict ({'max_output_tokens': 100, 'maxOutputTokens': 200}) raise. Across layers, the higher layer wins.
  • A ref whose config_schema names another class raises INVALID_ARGUMENT.
  • EmbedderRef is a frozen generic dataclass like ModelRef, so model_validate/model_dump are gone. When version and config['version'] are both set, the config value wins.
  • A JSON-schema config_schema next to a typed annotation raises at define time.
  • A typed fn's boundary errors name the field (strict: unknown config key 'thinking'), not Invalid input for model 'strict'.
  • Third-party untyped plugins that call config.get('k', default) can now see None where the key used to be absent.
  • mime_type/auto_truncate raise on the Gemini Developer API embedder; multimodal embedding raises on task_type/title.
  • GoogleAI.embedding()/VertexAI.embedding() dropped the version= kwarg and dict config (use EmbeddingConfig(version=...)).

Also in this PR: Google GenAI embedders

  • GoogleAI.embedding() and VertexAI.embedding() return EmbedderRef[EmbeddingConfig] and take config: EmbeddingConfig | None = None, mirroring gemini(). Pass raw dicts to ai.embed(config=...).
  • EmbeddingConfig inherits GenkitConfig (extra='forbid'), so unknown keys fail with INVALID_ARGUMENT. Embedder actions bind config_schema=EmbeddingConfig and keep backend-filtered customOptions for the Dev UI.

Where to look

  • py/packages/genkit/src/genkit/_core/_config_check.py: shared config pipeline (layer_call_config, overlay_config, config_defaults, check_config_values, validate_whole_config).
  • py/packages/genkit/src/genkit/_core/_action.py: Action.run boundary default injection and whole-config validation.
  • py/packages/genkit/src/genkit/_ai/_model.py and py/packages/genkit/src/genkit/_ai/_aio.py: call_config_class, resolve_call_config, and _embedder_and_options.
  • py/packages/genkit-google-genai/src/genkit_google_genai/_models/_embedder.py: EmbeddingConfig (GenkitConfig) and EmbedderRef[EmbeddingConfig].

@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 unifies the configuration pipeline for models, embedders, and evaluators in Genkit, introducing generic EmbedRequest[Cfg] and EvalRequest[Cfg] types to support typed options. It consolidates config validation, layering, and default injection, and updates plugins to use custom_options_schema to filter out null defaults for the Dev UI. Feedback on these changes highlights a performance concern regarding the repeated instantiation of TypeAdapter in config_defaults, suggesting caching or pre-computing these instances to avoid overhead in Pydantic v2.

@huangjeff5
huangjeff5 force-pushed the feat/py-config-pipeline branch from 4932cab to 0bc31e6 Compare October 9, 2026 09:59
@huangjeff5 huangjeff5 changed the title feat(py)!: one config pipeline for models, embedders and evaluators feat(py)!: config pipeline: definition defaults, one merge, None is a value Oct 9, 2026
@huangjeff5
huangjeff5 removed this pull request from stack #6713 October 9, 2026 10:00
@huangjeff5
huangjeff5 changed the base branch from fix/py-embed-eval-options-dict to feat/py-config-class October 9, 2026 10:00
@huangjeff5
huangjeff5 added this pull request to stack #6715 October 9, 2026 10:00
@huangjeff5
huangjeff5 force-pushed the feat/py-config-pipeline branch from 0bc31e6 to a0b2f10 Compare October 9, 2026 15:47
Base automatically changed from feat/py-config-class to main October 29, 2026 21:14
@huangjeff5
huangjeff5 force-pushed the feat/py-config-pipeline branch from 9e498db to f41c73e Compare November 2, 2026 04:53
@github-actions github-actions Bot added the docs Improvements or additions to documentation label Nov 2, 2026
@huangjeff5
huangjeff5 removed this pull request from stack #6715 November 2, 2026 04:56
@huangjeff5
huangjeff5 changed the base branch from main to fix/py-common-config-fields November 2, 2026 04:56
@huangjeff5
huangjeff5 force-pushed the feat/py-config-pipeline branch 2 times, most recently from f41c73e to d687ec2 Compare November 2, 2026 12:23
@huangjeff5
huangjeff5 added this pull request to stack #7013 November 2, 2026 16:33
@huangjeff5
huangjeff5 force-pushed the feat/py-config-pipeline branch from d687ec2 to 7289d1b Compare November 2, 2026 16:58
@huangjeff5
huangjeff5 requested a review from pavelgj November 2, 2026 18:16

@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 closed this Nov 2, 2026
@huangjeff5
huangjeff5 force-pushed the feat/py-config-pipeline branch from 7289d1b to 2769d65 Compare November 2, 2026 23:57
@huangjeff5 huangjeff5 reopened this Nov 3, 2026
@huangjeff5
huangjeff5 force-pushed the feat/py-config-pipeline branch from 63e412a to a77e900 Compare November 3, 2026 02:02
Base automatically changed from fix/py-common-config-fields to main November 3, 2026 02:25
… value

Unify model and embedder config merging and validation at Action.run:
- Layer schema defaults < ref.version < ref.config < call config via one shared pipeline in _core/_config_check.py.
- Treat explicit None as a value through layering and validation rather than stripping it early.
- Validate whole merged configs at the Action.run boundary and strip null defaults from published customOptions.
- Align GoogleAI.embedding() and VertexAI.embedding() with EmbedderRef[EmbeddingConfig].
@huangjeff5
huangjeff5 force-pushed the feat/py-config-pipeline branch from a77e900 to 27919e8 Compare November 3, 2026 02:58
… key

A prompt called with a model other than its own (compared by name, so
'gem' and ModelRef(name='gem') are the same model) drops a key from both
the prompt's and the call's config when the call sets it to None, the
target's config class doesn't declare it, and the prompt's config has it.
A None for a key the prompt doesn't have still raises unknown config key.
Everywhere else None stays a value; ai.generate() is unchanged.
@huangjeff5
huangjeff5 merged commit df2da7b into main Nov 3, 2026
25 of 26 checks passed
@huangjeff5
huangjeff5 deleted the feat/py-config-pipeline branch November 3, 2026 04:11
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 python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants