Repository navigation
fix(afc): disambiguate colliding $defs entries across tool parameters - #3478
dsugurtuna wants to merge 2 commits into
Conversation
Avoid silently overwriting colliding $defs / definitions entries when merging parameter schemas in parse_function_declaration_json_schema. When a parameter introduces a definition whose name already exists in root_defs with a different schema (directly or transitively after rewriting internal $refs), disambiguate it with the parameter name (and a numeric suffix if needed) and rewrite all matching $refs across the parameter schema and its sibling definitions, including promoting draft-07 #/definitions/ refs to #/$defs/. Fixes googleapis#3476
Sun-Wukhan
left a comment
There was a problem hiding this comment.
This fixes #3476, which I filed, and it handles the parts of the problem that are easy to miss. Checked against the reproduction in the issue and against the surrounding code:
-
Identical definitions are still deduplicated rather than renamed (the
rewritten_schema != root_defs[k]comparison), so the common case of two parameters genuinely sharing one nested model produces the same output as before this change. The self-recursive and mutually recursive tests pin the cases where a naive rename would loop or diverge, and the fixed-point loop in_merge_param_defsis bounded becauserename_maponly grows and is capped by the number of definitions in the parameter being merged. -
The ref rewriting is careful in two places I specifically checked:
$defs,definitions,properties,patternPropertiesanddependentSchemasare treated as name-keyed maps whose keys are not themselves rewritten, whileconst,default,enumandexamplesvalues are passed through untouched, so a literal string value that happens to look like a ref is not corrupted. The JSON pointer escaping (~0,~1) is applied consistently to both the token map keys and the ref tokens being matched. -
The draft-07 path is handled separately and correctly: definitions promoted from a
definitionscontainer have every ref prefix updated to#/$defs/, not only the renamed ones, which the oldupdatemerge never did at all.
Two observations, neither blocking:
- The generated definition names now depend on parameter names and on parameter order (the first parameter keeps the plain name, later ones get the prefix). That is deterministic for a given callable, but reordering parameters in a tool signature will rename definitions in the emitted schema. Worth a sentence in the PR description so downstream consumers who snapshot schemas are not surprised.
_rewrite_refsrecurses through any other dict values, which coversallOf,anyOf,oneOf,itemsandadditionalPropertieswithout listing them. The one legacy keyword I could not find handled or tested is draft-07dependencies(the predecessor ofdependentSchemas), where a ref could sit under a property name key. Given pydantic v2 emitsdependentSchemasstyle output, this may be out of scope, but a maintainer call on it would be useful.
The test file is thorough; the transitive collision test (wrapper and leaf both colliding) is the one that would have caught a single-level rename.
|
Thanks for checking this against the #3476 repro and the surrounding schema handling, @Sun-Wukhan! Both points make sense:
|
Fixes #3476
parse_function_declaration_json_schemapromoted each parameter's$defs(anddefinitions) into a single sharedroot_defsdict usingroot_defs.update(...). When two parameters use different Pydantic models whose nested models share a class name (for exampleItemin two different modules), the second parameter's definition overwrote the first while both parameters still referenced#/$defs/Item.This adds
_merge_param_defsto disambiguate colliding definition keys per parameter ({param_name}_{key}, falling back to numeric suffixes if needed) and rewrite matching$refpointers across the parameter schema and its sibling definitions:{param_name}_prefix, generated definition names on collision depend on parameter name and parameter order.Outer -> Mid -> Leaf), self-recursive, and mutually recursive collisions, and rewrites promoted#/definitions/...references to#/$defs/....properties,patternProperties,$defs,definitions,dependentSchemas,dependencies) preserve map keys while instance-data keywords (const,default,enum,examples) are left untouched.Verification:
google/genai/tests/afc/test_parse_function_declaration_json_schema.py(12 passed), covering#3476, end-to-endFunctionDeclaration.from_callable/t_tool/process_schema, shared-model deduplication, transitive and recursive collisions, draft-07definitionspromotion, and draft-07dependencies.pytestacrosstests/afc/,tests/transformers/,tests/types/, andtests/mcp/(564 passed, 6 skipped), plusmypy google/genai/(49files clean) andpyink/ruffchecks.