Skip to content

fix(ai-chat): sanitize tool schemas for Gemini function_declarations#1276

Open
datlechin wants to merge 2 commits into
mainfrom
fix/gemini-tool-schema-sanitize
Open

fix(ai-chat): sanitize tool schemas for Gemini function_declarations#1276
datlechin wants to merge 2 commits into
mainfrom
fix/gemini-tool-schema-sanitize

Conversation

@datlechin
Copy link
Copy Markdown
Member

Problem

Sending a message to Gemini with AI Chat tools enabled fails with HTTP 400:

Invalid JSON payload received. Unknown name "additionalProperties" at 'tools[0].function_declarations[0].parameters': Cannot find field.
Invalid JSON payload received. Unknown name "type" at 'tools[0].function_declarations[1].parameters.properties[2].value': Proto field is not repeating, cannot start list.

Root cause: ChatToolSchemaBuilder produces a JSON Schema with:

  • additionalProperties: false on every object schema
  • type: ["string", "null"] for fields marked optional: true

Both forms work for OpenAI and Claude, but Gemini's function_declarations API rejects them. Gemini's schema is closer to Protocol Buffers — strict single types, no additionalProperties, optional fields use a nullable: true flag instead.

Fix

New static GeminiProvider.sanitizeSchemaForGemini(_:):

  • Recursively strips additionalProperties at any depth
  • Rewrites type: [X, "null"] to type: X plus nullable: true
  • Leaves enums, required, description, and other fields untouched

Applied to each tool's inputSchema before serializing into parameters. OpenAI/Claude paths are untouched — they still receive the unmodified shared schema.

Test plan

  • 5 new tests in GeminiProviderEncodingTests.swift covering: strip additionalProperties, rewrite optional types, preserve non-nullable types, recurse into array items, preserve enums and required
  • Manual: configure Gemini provider, send a message with tools enabled in AI Chat, verify no 400 errors
  • OpenAI and Claude still work (no shared schema change)

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.

1 participant