[PR #13666] fix(google): sanitize unsupported JSON Schema keywords for Gemini API #14762

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

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

State: open
Merged: No


Summary

The sanitizeGemini function in packages/opencode/src/provider/transform.ts was incomplete, causing INVALID_ARGUMENT (400) errors when MCP tools exposed schemas with unsupported JSON Schema constructs (e.g. additionalProperties, anyOf/oneOf, $ref, missing type).

Problem

When using Google Gemini models with MCP tools, the API frequently returns:

API request failed (400): { error: { code: 400, message: "Request contains an invalid argument", status: "INVALID_ARGUMENT" } }

Root causes:

  1. convertMcpTool() adds additionalProperties: false to every MCP tool schema, but Gemini API doesn't support this keyword
  2. sanitizeGemini() doesn't remove unsupported JSON Schema keywords like additionalProperties, $ref, $schema, minItems, maxItems, pattern, const, default, title, etc.
  3. anyOf/oneOf with const values are not converted to Gemini-compatible enum
  4. When type is undefined, the cleanup logic for properties/required is short-circuited (because result.type && result.type !== "object" evaluates to false, skipping all cleanup)

Changes

  • Strip 28 unsupported JSON Schema keywords before sending to Gemini: additionalProperties, $ref, $schema, $id, $defs, definitions, const, default, title, pattern, patternProperties, propertyNames, uniqueItems, minItems, maxItems, minLength, maxLength, minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf, if, then, else, not, allOf, anyOf, oneOf
  • Convert anyOf/oneOf with const values to string enum (Gemini-compatible)
  • Infer type: "object" when properties or required exist but type is missing

Related Issues

Fixes #12908 (anyOf/const not handled)
Fixes #13148 (type=undefined causes missed cleanup)
Fixes #9233 (empty params missing type:object)
Fixes #12295 ($ref not resolved)

Testing

Tested with MCP tools that have:

  • Empty schemas ({}) → correctly inferred as type: "object"
  • additionalProperties: false → correctly stripped
  • anyOf with const values → correctly converted to enum
  • Standard schemas with type → unchanged behavior
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/13666 **State:** open **Merged:** No --- ## Summary The `sanitizeGemini` function in `packages/opencode/src/provider/transform.ts` was incomplete, causing `INVALID_ARGUMENT` (400) errors when MCP tools exposed schemas with unsupported JSON Schema constructs (e.g. `additionalProperties`, `anyOf/oneOf`, `$ref`, missing `type`). ## Problem When using Google Gemini models with MCP tools, the API frequently returns: ``` API request failed (400): { error: { code: 400, message: "Request contains an invalid argument", status: "INVALID_ARGUMENT" } } ``` Root causes: 1. **`convertMcpTool()` adds `additionalProperties: false`** to every MCP tool schema, but Gemini API doesn't support this keyword 2. **`sanitizeGemini()` doesn't remove unsupported JSON Schema keywords** like `additionalProperties`, `$ref`, `$schema`, `minItems`, `maxItems`, `pattern`, `const`, `default`, `title`, etc. 3. **`anyOf`/`oneOf` with `const` values** are not converted to Gemini-compatible `enum` 4. **When `type` is undefined**, the cleanup logic for `properties`/`required` is short-circuited (because `result.type && result.type !== "object"` evaluates to `false`, skipping all cleanup) ## Changes - **Strip 28 unsupported JSON Schema keywords** before sending to Gemini: `additionalProperties`, `$ref`, `$schema`, `$id`, `$defs`, `definitions`, `const`, `default`, `title`, `pattern`, `patternProperties`, `propertyNames`, `uniqueItems`, `minItems`, `maxItems`, `minLength`, `maxLength`, `minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum`, `multipleOf`, `if`, `then`, `else`, `not`, `allOf`, `anyOf`, `oneOf` - **Convert `anyOf`/`oneOf` with `const` values** to string `enum` (Gemini-compatible) - **Infer `type: "object"`** when `properties` or `required` exist but `type` is missing ## Related Issues Fixes #12908 (anyOf/const not handled) Fixes #13148 (type=undefined causes missed cleanup) Fixes #9233 (empty params missing type:object) Fixes #12295 ($ref not resolved) ## Testing Tested with MCP tools that have: - Empty schemas (`{}`) → correctly inferred as `type: "object"` - `additionalProperties: false` → correctly stripped - `anyOf` with `const` values → correctly converted to `enum` - Standard schemas with `type` → unchanged behavior
yindo added the pull-request label 2026-02-16 18:19:31 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14762