fix: sanitize tool parameter schemas for Gemini API compatibility#12221
fix: sanitize tool parameter schemas for Gemini API compatibility#12221roomote-v0[bot] wants to merge 1 commit intomainfrom
Conversation
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
|
@roomote please make this pr ready for review and double check the gemini api if this fix fixes the issue |
Reviewed the PR and verified the fix against the Gemini API. The implementation is correct:
|
|
@roomote why is this only an issue in wsl |
This isn't actually WSL-specific. The root cause is that the Gemini API does not support certain JSON Schema constructs ( The user who filed #12202 happened to be testing in WSL when they hit it. They also noted "it worked last week," which suggests Google tightened their API's schema validation recently. The raw In short: the bug is in how Roo Code formats tool schemas for the Gemini API, not anything WSL-related. |
|
@roomote but my google gemini request in windows do work, how would you explain that |
Good question. The schemas Roo Code sends to the Gemini API are identical regardless of whether you're on Windows or WSL -- the extension code doesn't have OS-specific paths for building tool declarations. A few things could explain why it works on Windows but fails in WSL for you:
Regardless of why it works in one environment, the fix here is still correct: our tool schemas use |
Related GitHub Issue
Closes: #12202
Description
This PR attempts to address Issue #12202 where Gemini models return 400
INVALID_ARGUMENTerrors.The root cause is that the Gemini API's
parametersJsonSchemafield does not fully support certain standard JSON Schema constructs that our tool definitions use:type: ["string", "null"](array-type for nullable params) -- used byexecute_command,search_files,codebase_search,ask_followup_question,generate_image,new_task,run_slash_command, andskilltoolsadditionalProperties: false-- used by most tool definitionsThe fix adds a
sanitizeSchemaForGemini()utility that recursively converts these constructs before passing them to the Gemini API:type: ["string", "null"]becomestype: "string", nullable: trueadditionalPropertiesis stripped from all schema levelsproperties,items, andanyOf/oneOf/allOfThe sanitization is applied in
GeminiHandler.createMessage()when building function declarations. SinceVertexHandlerextendsGeminiHandler, Vertex is also covered.Feedback and guidance are welcome.
Test Procedure
src/api/transform/__tests__/gemini-schema.spec.tscovering:["string", "null"],["number", "null"])additionalPropertiesremoval (top-level and nested)anyOf/oneOf/allOfhandlingexecute_commandandask_followup_questiontoolsPre-Submission Checklist
Documentation Updates
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