Skip to content

fix(gemini): filter unsupported schema formats for Gemini API #11539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

colesmcintosh
Copy link
Collaborator

Title

fix(gemini): filter unsupported schema formats for Gemini API

Relevant issues

Fixes #11427

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix

Changes

Problem

Gemini API calls were failing when tool schemas contained unsupported format values like format: "email". The error message indicates that Gemini only supports enum and date-time formats for STRING types, while Vertex AI supports additional formats like email, uri, etc.

Both gemini/ and vertex_ai/ providers were using the same schema transformation logic, which didn't account for these API differences.

Solution

This PR implements Gemini-specific format filtering in the GoogleAIStudioGeminiConfig class:

  1. Overrides the _map_function method to apply Gemini-specific schema filtering after the standard transformation
  2. Removes unsupported format values like email, uri, etc. while preserving supported ones like date-time and enum
  3. Works recursively on nested objects, arrays, and anyOf constructs
  4. Preserves all other schema properties (type, description, validation rules, etc.)

Modified Files

  • litellm/llms/gemini/chat/transformation.py:
    • Added _filter_gemini_unsupported_formats() method for recursive schema filtering
    • Overrode _map_function() to apply Gemini-specific filtering
    • Only affects the gemini/ provider; vertex_ai/ behavior is unchanged

Added Files

  • tests/test_litellm/llms/gemini/test_gemini_format_filtering.py:
    • Comprehensive test suite verifying format filtering behavior
    • Tests for nested objects, arrays, and preservation of other properties

Example Usage

# This now works for both providers:
tool = {
    "type": "function",
    "function": {
        "name": "git_commit", 
        "parameters": {
            "type": "object",
            "properties": {
                "author": {
                    "type": "object",
                    "properties": {
                        "email": {"type": "string", "format": "email"}  # This format gets filtered for Gemini
                    }
                }
            }
        }
    }
}

# Works with gemini/ (format removed)
litellm.completion(model="gemini/gemini-2.5-flash-preview-05-20", tools=[tool], ...)

# Works with vertex_ai/ (format preserved)  
litellm.completion(model="vertex_ai/gemini-2.5-flash-preview-05-20", tools=[tool], ...)

The fix ensures backward compatibility and follows the principle of making the libraries "just work" without requiring users to modify their schemas for different providers.

…ssue BerriAI#11427 where Gemini API calls fail with format == email in tool schemas - Add Gemini-specific format filtering in GoogleAIStudioGeminiConfig - Remove unsupported formats (email, uri, etc.) while preserving supported ones (date-time, enum) - Implement recursive filtering for nested objects and arrays - Add comprehensive test suite for format filtering - Maintain backward compatibility and preserve all other schema properties  Resolves: BerriAI#11427
Copy link

vercel bot commented Jun 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
litellm ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 9, 2025 2:37pm

…ng logic - Update _filter_gemini_unsupported_formats method to use Dict[Any, Any> for better type clarity - Ensure recursive filtering handles both dict and non-dict items correctly - Maintain existing functionality while improving code readability
…te _filter_gemini_unsupported_formats to accept Union[Schema, Dict] for enhanced flexibility - Ensure proper handling of schema objects with a to_dict method - Maintain existing functionality while improving code clarity
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 calls fail with format == email in schema
1 participant