🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@foivos-all
Copy link

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
The _dereference_schema() function in google.adk.tools._gemini_schema_util triggers a RecursionError when processing JSON schemas with circular $ref references. This occurs because the function recursively resolves references without detecting circular dependencies, causing infinite recursion when a schema references itself (e.g., a tree structure where a node can contain child nodes of the same type).

Additionally, the original implementation only handled $defs-style references (#/$defs/TypeName) but not JSON Pointer references (#/properties/path/to/field), which are commonly used in OpenAPI and other schema formats.

Solution:
Enhanced _dereference_schema() with the following improvements:

  1. Circular reference detection: Track references currently being resolved using a resolving set. When a circular reference is detected, return a placeholder schema ({"type": "object", "description": "Circular reference to <ref>"}) instead of recursing infinitely.

  2. JSON Pointer support: Added _resolve_json_pointer() helper function to resolve references that use JSON Pointer syntax (#/properties/...), in addition to existing $defs support.

This allows for a graceful degradation, where rather than raising an error, circular references are replaced with valid placeholder schemas, allowing tools with recursive data structures to function correctly.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.
    • 3408 passed
    • 2504 warnings
    • Took 71.89s

Manual End-to-End (E2E) Tests:

  1. Schema with circular $defs reference

    from google.adk.tools._gemini_schema_util import _to_gemini_schema
    
    schema = {
        "type": "object",
        "properties": {
            "attendees": {
                "type": "array",
                "items": {"$ref": "#/$defs/Attendee"}
            }
        },
        "$defs": {
            "Attendee": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "alternateAttendee": {"$ref": "#/$defs/Attendee"} # Circular
                }
            }
        }
    }
    
    gemini_schema = _to_gemini_schema(schema)
    
    • Before fix: RecursionError
    • After fix: Success
  2. Schema with JSON Pointer circular reference

    from google.adk.tools._gemini_schema_util import _to_gemini_schema
    
    schema = {
        "type": "object",
        "properties": {
            "body": {
                "type": "object",
                "properties": {
                    "attendees": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "start": {
                                    "$ref": "#/properties/body/properties/end" # JSON Pointer
                                }
                            }
                        }
                    },
                    "end": {"type": "object", "properties": {"dateTime": {"type": "string"}}}
                }
            }
        }
    }
    
    gemini_schema = _to_gemini_schema(schema)
    
    • Before fix: RecursionError
    • After fix: Success
  3. Schema with 2 circular $defs reference

    from google.adk.tools._gemini_schema_util import _to_gemini_schema
    
    schema = {
        "type": "object",
        "properties": {
            "attendees": {
                "type": "array",
                "items": {"$ref": "#/$defs/Attendee"}
            },
            "organizer": {"$ref": "#/$defs/Person"}
        },
        "$defs": {
            "Attendee": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "alternateAttendee": {"$ref": "#/$defs/Attendee"}  # Circular 1
                }
            },
            "Person": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "manager": {"$ref": "#/$defs/Person"}  # Circular 2
                }
            }
        }
    }
    
    gemini_schema = _to_gemini_schema(schema)
    
    • Before fix: RecursionError
    • After fix: Success

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Note on google-genai dependency:
While this fix resolves the issue for ADK's schema conversion pipeline, the google-genai library's Schema.from_json_schema() method has a similar issue in its convert_json_schema() function. However, since ADK calls _dereference_schema() before passing schemas to google-genai, this fix protects ADK users from the RecursionError. Direct users of google-genai would need a similar fix in that library's auto-generated types.py file.

Applicability:
This fix enables building MCP server tools that wrap complex APIs which often contain recursive schema definitions for representing hierarchical data structures.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @foivos-all, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical issue in the _dereference_schema utility, which previously failed with a RecursionError when processing JSON schemas containing circular $ref references. The changes introduce robust circular dependency detection and expand the utility's capability to resolve JSON Pointer references, ensuring that complex and recursive schemas can be processed without errors and with appropriate placeholders for circular dependencies.

Highlights

  • Circular Reference Detection: Implemented a mechanism to detect and handle circular $ref references within JSON schemas during dereferencing. When a circular reference is encountered, a placeholder schema {"type": "object", "description": "Circular reference to <ref>"} is returned to prevent RecursionError.
  • JSON Pointer Support: Added support for resolving $ref references using JSON Pointer syntax (e.g., #/properties/path/to/field), in addition to the existing $defs-style references. A new helper function _resolve_json_pointer was introduced for this purpose.
  • Improved Schema Dereferencing: The _dereference_schema function now gracefully handles complex and recursive schema definitions, enhancing robustness for tools that process hierarchical data structures and preventing RecursionError.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the tools [Component] This issue is related to tools label Dec 9, 2025
Copy link
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 effectively resolves a RecursionError caused by circular $ref dependencies in JSON schemas by introducing a tracking set to detect and handle such loops. The addition of support for JSON Pointer references is also a valuable enhancement. The code is well-structured and the manual tests provided are comprehensive. I've included a couple of suggestions to further improve code clarity and robustness, primarily by simplifying a loop and using a try...finally block for more reliable resource management.

@rkrumins
Copy link

Great job on this @foivos-all and thank you very much! I'm being affected by the same issue using various MCPs together with ADK. Very keen on getting this merged given that resolves the circular $ref issue. 🙏

@ryanaiagent ryanaiagent self-assigned this Dec 11, 2025
@ryanaiagent ryanaiagent added the needs-review [Status] The PR is awaiting review from the maintainer label Dec 12, 2025
@ryanaiagent
Copy link
Collaborator

Hi @foivos-all ,Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

@ryanaiagent
Copy link
Collaborator

Hi @seanzhou1023 , can you please review this.

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

Labels

needs-review [Status] The PR is awaiting review from the maintainer tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RecursionError in _to_gemini_schema() when processing circular $ref in $defs

4 participants