Skip to content

fix(py/plugins/google-genai): resolve JSON pointer $refs in tool schemas - #6969

Closed
hilariie wants to merge 2 commits into
mainfrom
fix/py-google-genai-tool-schema-refs
Closed

hilariie wants to merge 2 commits into
mainfrom
fix/py-google-genai-tool-schema-refs

Conversation

@hilariie

Copy link
Copy Markdown
Contributor

Why

The Gemini schema converter only resolved $refs that point into a root $defs, matching on the ref's last
path segment, and raised for anything else. Draft-07 #/definitions/X and JSON pointer refs like
#/properties/a (zod-to-json-schema's default for a reused sub-schema) are common in schemas from MCP servers,
which the MCP plugin passes through as-is, so any request carrying such a tool failed before it was sent.

Changes

  • $ref resolves as a local JSON pointer into the root schema: #, #/definitions/..., paths like
    #/properties/a/items, list indexes, ~0/~1 escapes and percent-encoding. Remote refs and pointers that
    don't resolve still raise.
  • A ref to a node that contains it converts to None, the same as a recursive $defs ref already did. A
    {"$ref": "#"} inside the root is now treated that way instead of raising.
  • If a tool's output schema can't be converted, the tool is sent without response and a warning is logged.
    Input schemas and structured output (response_schema) still fail on a schema that can't be converted.

@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 enhances the Gemini model implementation by resolving local $ref schemas as JSON pointers against the root document, replacing the previous limited $defs resolution. It also introduces robust handling for recursive references using node IDs and allows tools to be sent without an output schema if schema conversion fails, logging a warning instead of raising an error. The review feedback suggests utilizing structured logging fields to include the tool name in the warning log and handling potential empty tokens when splitting malformed JSON pointer paths.

@huangjeff5 huangjeff5 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.

Pointer resolution checks out: zod's #/definitions and #/properties/a refs, a recursive pydantic model, ~1, and remote/missing refs raising INVALID_ARGUMENT with the ref named. Decoding before the split matches RFC 6901 §6. All 1366 tests pass. Two notes inline.

Follow-up idea: google-genai 2.21 has FunctionDeclaration.parameters_json_schema / response_json_schema, and structured output already sends response_json_schema by default. Routing tool schemas that way would pass MCP schemas through as is and make both inline cases go away.

  • py/packages/genkit-google-genai/tests/models/googlegenai_gemini_test.py:934: nit: stale after the rename; root defaults to input_schema now. Same for Test Case 12's "missing defs dict" at L1080.

A tool output schema that still can't be converted is now dropped with a
warning instead of failing the request.
@hilariie
hilariie force-pushed the fix/py-google-genai-tool-schema-refs branch from 223f33e to e586621 Compare November 5, 2026 11:06
@hilariie

hilariie commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Fixed the stale test comments in e586621. Filed issue here: #7151

@hilariie

Copy link
Copy Markdown
Contributor Author

opened in the python repo: genkit-ai/genkit-python/pull/90

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