fix: sanitize Pydantic V2 schemas for Gemini API compatibility#183
Open
jnMetaCode wants to merge 1 commit intoIntelligent-Internet:mainfrom
Open
fix: sanitize Pydantic V2 schemas for Gemini API compatibility#183jnMetaCode wants to merge 1 commit intoIntelligent-Internet:mainfrom
jnMetaCode wants to merge 1 commit intoIntelligent-Internet:mainfrom
Conversation
Gemini rejects JSON Schema keywords like exclusiveMinimum, const, and anyOf that Pydantic V2 emits by default. Add a recursive sanitizer that converts these into the OpenAPI subset Gemini expects: - exclusiveMinimum -> minimum - exclusiveMaximum -> maximum - const -> enum (single element) - anyOf [T, null] -> flattened to T (Optional pattern) - strips $schema, additionalProperties, title, default, examples Fixes Intelligent-Internet#171
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.
Problem
When using Gemini models, tool schemas generated by Pydantic V2 contain
JSON Schema keywords that Gemini rejects with
extra_forbiddenvalidationerrors:
The Gemini API expects a restricted OpenAPI-style subset of JSON Schema,
but
tool.input_schemais passed through without any sanitization.Fix
Added
_sanitize_schema_for_gemini()which recursively walks the schemaand converts incompatible keywords:
exclusiveMinimumminimumexclusiveMaximummaximumconstenum(single element)anyOf [T, null]T$schema,title,default,examplesApplied to
tool.input_schemain thegenerate()method beforebuilding the function declarations.
Related issue
Fixes #171