Skip to content

fix: expand $ref references in JSON Schema for Gemini compatibility#12292

Open
high726 wants to merge 1 commit intoanomalyco:devfrom
high726:fix/gemini-ref-schema-expansion
Open

fix: expand $ref references in JSON Schema for Gemini compatibility#12292
high726 wants to merge 1 commit intoanomalyco:devfrom
high726:fix/gemini-ref-schema-expansion

Conversation

@high726
Copy link

@high726 high726 commented Feb 5, 2026

Summary

Gemini API rejects schemas containing $ref alongside other fields, causing errors like:

$ref 'QuestionOption' was set alongside unsupported fields. 
If a schema node has $ref set, then only description and default can be set alongside it

This fix adds $ref expansion in ProviderTransform.schema() before sending schemas to Gemini.

Fixes #12295

Changes

  • 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
  • Only applies to Gemini models (does not affect other providers)

Files Changed

File Change
packages/opencode/src/provider/transform.ts Add expandRefs() function before sanitizeGemini()
packages/opencode/test/provider/transform.test.ts Add 7 test cases for $ref expansion

Test Cases

  • ✅ Expands simple $ref with $defs
  • ✅ Expands $ref in array items
  • ✅ Expands nested $ref references
  • ✅ Handles circular $ref by returning simple object
  • ✅ Expands definitions (legacy format)
  • ✅ Preserves description when expanding $ref
  • ✅ Does not expand $ref for non-Gemini providers

Related Issues

Related PRs

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

The following comment was made by an LLM, it may be inaccurate:

Potential Related PR Found

PR #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 $ref expansion while #11968 handles the broader schema transformation for MCP tools with Gemini. These are related fixes addressing the same issue from different angles.

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
@high726 high726 force-pushed the fix/gemini-ref-schema-expansion branch from 508f84e to 7ad4a99 Compare February 6, 2026 08:44
dgokeeffe added a commit to dgokeeffe/opencode that referenced this pull request Feb 8, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gemini API rejects JSON Schema containing $ref references

1 participant