fix: expand $ref references in JSON Schema for Gemini compatibility#12292
fix: expand $ref references in JSON Schema for Gemini compatibility#12292high726 wants to merge 1 commit intoanomalyco:devfrom
Conversation
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: Potential Related PR FoundPR #11968 - "fix: apply schema transformation to MCP tools for Gemini compatibility" Why it's related: According to the PR description (line 53), PR #11968 applies schema transformation to MCP tools and is directly complementary to this PR. The current PR (#12292) handles This appears to be the primary related PR. The other results are either the current PR itself or unrelated changes. |
Gemini API rejects schemas with $ref alongside other fields, causing errors like:
"$ref 'QuestionOption' was set alongside unsupported fields"
This fix adds $ref expansion before sending schemas to Gemini:
- Expands $ref references by inlining definitions from $defs/definitions
- Handles nested $ref references recursively
- Protects against circular references by returning { type: 'object' }
- Preserves additional fields (like description) when expanding
- Removes $defs/definitions after expansion
Related issues:
- google-gemini/gemini-cli#13326
- anomalyco#5832
- anomalyco#8417
508f84e to
7ad4a99
Compare
The sanitizeForGemini function could infinite loop when a schema
contained circular $ref references (e.g., TreeNode referencing itself).
Added a Set to track refs currently being resolved. When a circular
ref is detected, returns {type: "object"} as a safe fallback,
matching the approach in PR anomalyco#12292.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
Gemini API rejects schemas containing
$refalongside other fields, causing errors like:This fix adds
$refexpansion inProviderTransform.schema()before sending schemas to Gemini.Fixes #12295
Changes
$refreferences by inlining definitions from$defs/definitions$refreferences recursively{ type: "object" }description) when expanding$defs/definitionsafter expansionFiles Changed
packages/opencode/src/provider/transform.tsexpandRefs()function beforesanitizeGemini()packages/opencode/test/provider/transform.test.ts$refexpansionTest Cases
$refwith$defs$refin array items$refreferences$refby returning simple objectdefinitions(legacy format)descriptionwhen expanding$ref$reffor non-Gemini providersRelated Issues
$defsreferencesRelated PRs
$ref)