[PR #12292] fix: expand $ref references in JSON Schema for Gemini compatibility #14154

Open
opened 2026-02-16 18:18:58 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/12292

State: open
Merged: No


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

  • #11968 - Applies schema transformation to MCP tools (this PR complements that fix by handling $ref)
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/12292 **State:** open **Merged:** No --- ## 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 - google-gemini/gemini-cli#13326 - Gemini API rejects MCP tool schemas with `$defs` references - #5832 - Error with Gemini models when MCP enabled - #8417 - Gemini Bad Request with Github Copilot ## Related PRs - #11968 - Applies schema transformation to MCP tools (this PR complements that fix by handling `$ref`)
yindo added the pull-request label 2026-02-16 18:18:58 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14154