Skip to content

fix(py): Fix code execution serialization issue for googleai plugin#5326

Merged
huangjeff5 merged 2 commits into
mainfrom
jh-code-execution-fix
May 14, 2026
Merged

fix(py): Fix code execution serialization issue for googleai plugin#5326
huangjeff5 merged 2 commits into
mainfrom
jh-code-execution-fix

Conversation

@huangjeff5
Copy link
Copy Markdown
Contributor

Fixes the bug bash entry where ai.generate(config=GeminiConfigSchema(...).model_dump()) with code_execution: True raised:

ValidationError for GenerateContentConfig
codeExecution: Extra inputs are not permitted [type=extra_forbidden]

Root cause: when a request reaches the Gemini plugin, request.config is typed as the generic GenerationCommonConfig, which stores plugin-specific keys verbatim on model_extra (typically alias-form / camelCase). The plugin's _normalize_config_to_dict had a branch that just trusted the base type and dumped it without re-routing through GeminiConfigSchema, so the camelCase extra leaked past the snake_case pop() in _extract_tools_from_config and ended up at the SDK's strict GenerateContentConfig (extra='forbid').

Fix: always re-validate non-plugin inputs through the plugin schema before dumping. Pydantic's populate_by_name=True on the plugin schema folds aliased extras onto their canonical snake_case fields. As a side benefit, this fixes the same latent issue for every other aliased Gemini-only field (googleSearchRetrieval, urlContext, fileSearch, functionCallingConfig, safetySettings, apiKey, contextCache, etc.).

Refactor: extracted _pick_plugin_schema so the routing-by-marker-key logic isn't inlined; the main function now reads as a single top-to-bottom flow.

Tests: 4 parametrized normalize-canonicalization cases (all 3 input shapes + a fast-path GeminiConfigSchema instance) and 1 e2e test that runs _genkit_to_googleai_cfg on the original repro shape and asserts the convenience flag was translated into a Tool(code_execution=...) with no leftover camelCase keys on the SDK config.

Branch: main (apply on top of origin/main).
Preflight: ruff format/check, ty/pyrefly/pyright, pytest 3.10-3.14 -- all green.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

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 configuration normalization in the Gemini plugin to ensure camelCase aliases are correctly mapped to canonical snake_case fields. It introduces a _pick_plugin_schema method to select the appropriate schema based on input keys. Review feedback identifies a potential regression for Gemma models because the logic defaults to GeminiConfigSchema, which has stricter validation than GemmaConfigSchema; the reviewer suggests dynamically selecting the schema based on the model version.

Comment thread py/plugins/google-genai/src/genkit/plugins/google_genai/models/gemini.py Outdated
@huangjeff5 huangjeff5 merged commit 3c39b01 into main May 14, 2026
19 checks passed
@huangjeff5 huangjeff5 deleted the jh-code-execution-fix branch May 14, 2026 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants