fix: sanitize tool parameter schemas for Gemini API compatibility#12221
Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
Draft
fix: sanitize tool parameter schemas for Gemini API compatibility#12221roomote-v0[bot] wants to merge 1 commit intomainfrom
roomote-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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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