Skip to content

fix(provider): handle anyOf/oneOf/const/$ref in Gemini schema sanitization#12911

Open
nxxxsooo wants to merge 1 commit intoanomalyco:devfrom
nxxxsooo:fix/sanitize-gemini-anyof-oneof-const
Open

fix(provider): handle anyOf/oneOf/const/$ref in Gemini schema sanitization#12911
nxxxsooo wants to merge 1 commit intoanomalyco:devfrom
nxxxsooo:fix/sanitize-gemini-anyof-oneof-const

Conversation

@nxxxsooo
Copy link

Problem

Gemini's generateContent API rejects JSON schemas containing anyOf, oneOf, const, $ref, and $defs — all valid JSON Schema but unsupported by Gemini's function calling format. This causes MCP tools from servers like Notion, Memory, and Obsidian mcp-tools to fail when used with any Gemini model.

Error example:

GenerateContentRequest.tools[0].function_declarations[32].parameters.properties[format].any_of[0].enum: only allowed for STRING type

Root Cause

The sanitizeGemini function in transform.ts handles integer→string enum conversion and a few other edge cases, but does not transform these unsupported JSON Schema patterns:

Pattern Source Example
$ref / $defs Notion MCP { "$ref": "#/$defs/richTextRequest" }
anyOf with const Obsidian mcp-tools { "anyOf": [{ "const": "markdown" }, { "const": "json" }] }
oneOf mixed types Memory MCP { "oneOf": [{ "type": "array" }, { "type": "string" }] }
oneOf multiple objects Notion MCP { "oneOf": [{ "type": "object", "properties": { "page_id": ... } }, ...] }

Solution

Two-phase transformation before existing sanitization:

  1. $ref resolution: Inline all $ref references from $defs/definitions, with circular reference detection
  2. anyOf/oneOf flattening:
    • All constenum array
    • Multiple objects → merge properties
    • Mixed types → pick first (most specific)
    • Single-item → unwrap

Testing

Tested against real MCP server schemas:

  • Notion MCP: 22/22 tools with issues fully fixed (66 anyOf/oneOf patterns resolved)
  • Memory MCP: 4 tools with oneOf tags patterns fixed
  • Obsidian mcp-tools: anyOf+const patterns fixed
  • All existing behavior preserved (enum→string, required filtering, array items)

Changes

  • packages/opencode/src/provider/transform.ts: Added resolveRefs helper and anyOf/oneOf flattening to sanitizeGemini

…ation

Gemini's generateContent API rejects JSON schemas containing anyOf,
oneOf, const, $ref, and $defs — all valid JSON Schema but unsupported
in Gemini's function calling format.

This causes MCP tools from servers like Notion, Memory, and Obsidian
mcp-tools to fail with errors like:
  'only allowed for STRING type'
  'Failed to process error response'

Changes:
- Add $ref/$defs resolution before sanitization (handles circular refs)
- Flatten anyOf/oneOf with const values into enum arrays
- Merge multiple object alternatives into single merged object
- Pick first type for mixed-type oneOf/anyOf
- Single-item unwrap for trivial anyOf/oneOf

Tested against real schemas from Notion MCP (22/22 tools fixed),
Memory MCP (4 tools), and old Obsidian mcp-tools plugin.
@github-actions
Copy link
Contributor

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

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

I found potential related PRs to PR #12911:

  1. PR fix(provider): fix property order bug in Gemini numeric enum stringification #12827: fix(provider): fix property order bug in Gemini numeric enum stringification

    • Related to Gemini schema sanitization and enum handling
  2. PR fix: expand $ref references in JSON Schema for Gemini compatibility #12292: fix: expand $ref references in JSON Schema for Gemini compatibility

  3. PR fix: apply schema transformation to MCP tools for Gemini compatibility #11968: fix: apply schema transformation to MCP tools for Gemini compatibility

    • Related to MCP tool schema transformation for Gemini compatibility

These PRs appear to be working on related or overlapping aspects of Gemini schema compatibility. PRs #12292 and #11968 are particularly relevant as they address similar schema transformation issues for Gemini.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments