-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: Detect and handle circular $ref in _dereference_schema
#3880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
|
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. 🙏 |
|
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. |
|
Hi @seanzhou1023 , can you please review this. |
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
RecursionErrorin_to_gemini_schema()when processing circular$refin$defs#3870RecursionErrorin_to_gemini_schema()when processing circular$refin$defs#3870Problem:
The
_dereference_schema()function ingoogle.adk.tools._gemini_schema_utiltriggers aRecursionErrorwhen processing JSON schemas with circular$refreferences. 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:Circular reference detection: Track references currently being resolved using a
resolvingset. When a circular reference is detected, return a placeholder schema ({"type": "object", "description": "Circular reference to <ref>"}) instead of recursing infinitely.JSON Pointer support: Added
_resolve_json_pointer()helper function to resolve references that use JSON Pointer syntax (#/properties/...), in addition to existing$defssupport.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:
Manual End-to-End (E2E) Tests:
Schema with circular
$defsreferenceRecursionErrorSchema with JSON Pointer circular reference
RecursionErrorSchema with 2 circular
$defsreferenceRecursionErrorChecklist
Additional context
Note on google-genai dependency:
While this fix resolves the issue for ADK's schema conversion pipeline, the
google-genailibrary'sSchema.from_json_schema()method has a similar issue in itsconvert_json_schema()function. However, since ADK calls_dereference_schema()before passing schemas togoogle-genai, this fix protects ADK users from theRecursionError. Direct users ofgoogle-genaiwould need a similar fix in that library's auto-generatedtypes.pyfile.Applicability:
This fix enables building MCP server tools that wrap complex APIs which often contain recursive schema definitions for representing hierarchical data structures.