Skip to content

fix: sanitize tool parameter schemas for Gemini API compatibility#12221

Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/gemini-sanitize-tool-schemas
Draft

fix: sanitize tool parameter schemas for Gemini API compatibility#12221
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/gemini-sanitize-tool-schemas

Conversation

@roomote-v0
Copy link
Copy Markdown
Contributor

@roomote-v0 roomote-v0 Bot commented Apr 29, 2026

Related GitHub Issue

Closes: #12202

Description

This PR attempts to address Issue #12202 where Gemini models return 400 INVALID_ARGUMENT errors.

The root cause is that the Gemini API's parametersJsonSchema field does not fully support certain standard JSON Schema constructs that our tool definitions use:

  • type: ["string", "null"] (array-type for nullable params) -- used by execute_command, search_files, codebase_search, ask_followup_question, generate_image, new_task, run_slash_command, and skill tools
  • additionalProperties: false -- used by most tool definitions

The fix adds a sanitizeSchemaForGemini() utility that recursively converts these constructs before passing them to the Gemini API:

  • type: ["string", "null"] becomes type: "string", nullable: true
  • additionalProperties is stripped from all schema levels
  • Handles nested properties, items, and anyOf/oneOf/allOf

The sanitization is applied in GeminiHandler.createMessage() when building function declarations. Since VertexHandler extends GeminiHandler, Vertex is also covered.

Feedback and guidance are welcome.

Test Procedure

  • Added 12 unit tests in src/api/transform/__tests__/gemini-schema.spec.ts covering:
    • Nullable type conversion (["string", "null"], ["number", "null"])
    • additionalProperties removal (top-level and nested)
    • Deeply nested schemas (array items, nested objects)
    • anyOf/oneOf/allOf handling
    • Edge cases (null input, type arrays without null, single string types)
    • Real-world schemas matching execute_command and ask_followup_question tools
  • All 12 new tests pass
  • All 46 existing Gemini/Vertex tests continue to pass
  • Full lint and type-check pass

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes.
  • Documentation Impact: No documentation updates are required.
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

This fix is defensive -- if the Gemini API already accepts these schemas (e.g. via newer SDK versions), the sanitization is a no-op for valid schemas. The conversion only modifies constructs known to be problematic.

Get in Touch

N/A (automated PR from Roomote)

Interactively review PR in Roo Code Cloud

The Gemini API does not support certain JSON Schema constructs in
parametersJsonSchema for function declarations:
- type arrays like ["string", "null"] for nullable types
- additionalProperties field

This causes 400 INVALID_ARGUMENT errors when the API rejects these
schemas. Multiple tool definitions (execute_command, search_files,
codebase_search, etc.) use type: ["string", "null"] for optional
nullable parameters.

Add sanitizeSchemaForGemini() that recursively converts:
- type: ["string", "null"] -> type: "string", nullable: true
- Removes additionalProperties from all schema levels
- Handles nested properties, items, anyOf/oneOf/allOf

Fixes #12202
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.

[BUG] Gemini models in WSL env: Request contains an invalid argument.

1 participant