Skip to content

fix(afc): disambiguate colliding $defs entries across tool parameters - #3478

Open
dsugurtuna wants to merge 2 commits into
googleapis:mainfrom
dsugurtuna:fix/afc-defs-collision
Open

dsugurtuna wants to merge 2 commits into
googleapis:mainfrom
dsugurtuna:fix/afc-defs-collision

Conversation

@dsugurtuna

@dsugurtuna dsugurtuna commented Dec 6, 2026 •

Copy link
Copy Markdown

Fixes #3476

parse_function_declaration_json_schema promoted each parameter's $defs (and definitions) into a single shared root_defs dict using root_defs.update(...). When two parameters use different Pydantic models whose nested models share a class name (for example Item in two different modules), the second parameter's definition overwrote the first while both parameters still referenced #/$defs/Item.

This adds _merge_param_defs to disambiguate colliding definition keys per parameter ({param_name}_{key}, falling back to numeric suffixes if needed) and rewrite matching $ref pointers across the parameter schema and its sibling definitions:

  • Identical definitions shared across parameters are left deduplicated under the original key.
  • Because the first parameter keeps the unprefixed name and later colliding parameters receive the {param_name}_ prefix, generated definition names on collision depend on parameter name and parameter order.
  • A fixed-point pass handles transitive (Outer -> Mid -> Leaf), self-recursive, and mutually recursive collisions, and rewrites promoted #/definitions/... references to #/$defs/....
  • Schema-map keywords (properties, patternProperties, $defs, definitions, dependentSchemas, dependencies) preserve map keys while instance-data keywords (const, default, enum, examples) are left untouched.

Verification:

  • Added google/genai/tests/afc/test_parse_function_declaration_json_schema.py (12 passed), covering #3476, end-to-end FunctionDeclaration.from_callable / t_tool / process_schema, shared-model deduplication, transitive and recursive collisions, draft-07 definitions promotion, and draft-07 dependencies.
  • Ran pytest across tests/afc/, tests/transformers/, tests/types/, and tests/mcp/ (564 passed, 6 skipped), plus mypy google/genai/ (49 files clean) and pyink/ruff checks.

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 Sun-Wukhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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:

  1. 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_defs is bounded because rename_map only grows and is capped by the number of definitions in the parameter being merged.

  2. The ref rewriting is careful in two places I specifically checked: $defs, definitions, properties, patternProperties and dependentSchemas are treated as name-keyed maps whose keys are not themselves rewritten, while const, default, enum and examples values 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.

  3. The draft-07 path is handled separately and correctly: definitions promoted from a definitions container have every ref prefix updated to #/$defs/, not only the renamed ones, which the old update merge 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_refs recurses through any other dict values, which covers allOf, anyOf, oneOf, items and additionalProperties without listing them. The one legacy keyword I could not find handled or tested is draft-07 dependencies (the predecessor of dependentSchemas), where a ref could sit under a property name key. Given pydantic v2 emits dependentSchemas style 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.

@dsugurtuna

Copy link
Copy Markdown
Author

Thanks for checking this against the #3476 repro and the surrounding schema handling, @Sun-Wukhan! Both points make sense:

  • Updated the PR description to call out that disambiguated names depend on parameter name and order (first parameter keeps the plain name, later colliding ones get {param_name}_).
  • Added 'dependencies' to _SCHEMA_MAP_KEYWORDS (plus a unit test covering both schema-valued refs under a default property key and list-valued property dependencies) since we already support draft-07 definitions promotion here and it's a one-line addition (12 passed, 564 passed across afc/transformers/types/mcp, mypy clean).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants