From 59adac89a000924c94f0701b2c9d1ab35b51c6db Mon Sep 17 00:00:00 2001 From: weijiafu14 <17469139+weijiafu14@users.noreply.github.com> Date: Wed, 18 Mar 2026 14:04:12 +0800 Subject: [PATCH] fix(gemini): expand schema cleaning to cover all unsupported JSON Schema fields The current clean_gemini_schema() only removes additionalProperties, default, and some format values. But Gemini rejects many more JSON Schema fields, causing 400 errors on tools with complex schemas (e.g. Claude Code's built-in tools use oneOf, allOf, nullable, pattern, etc.). This expands the cleaning to cover 30+ unsupported fields including: - Validation: format, minimum, maximum, pattern, minLength, maxLength, etc. - Metadata: default, examples, const, title - Composition: oneOf, anyOf, allOf, not - Extensions: $ref, $schema, nullable, propertyNames, etc. Reference: https://github.com/weijiafu14/gemini-claude-bridge --- server.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/server.py b/server.py index bf2ac6c1..92875a1b 100644 --- a/server.py +++ b/server.py @@ -124,24 +124,39 @@ def format(self, record): # Helper function to clean schema for Gemini def clean_gemini_schema(schema: Any) -> Any: - """Recursively removes unsupported fields from a JSON schema for Gemini.""" - if isinstance(schema, dict): - # Remove specific keys unsupported by Gemini tool parameters - schema.pop("additionalProperties", None) - schema.pop("default", None) + """Recursively removes JSON Schema fields unsupported by Gemini API. + + Gemini only supports a subset of JSON Schema: type, properties, required, + items, enum, description. All other fields cause 400 errors. - # Check for unsupported 'format' in string types - if schema.get("type") == "string" and "format" in schema: - allowed_formats = {"enum", "date-time"} - if schema["format"] not in allowed_formats: - logger.debug(f"Removing unsupported format '{schema['format']}' for string type in Gemini schema.") - schema.pop("format") + Reference: https://ai.google.dev/gemini-api/docs/function-calling + See also: https://github.com/weijiafu14/gemini-claude-bridge + """ + if isinstance(schema, dict): + # Fields that Gemini API does not support + unsupported_keys = [ + # Top-level extensions + "$schema", "additionalProperties", + # Validation constraints + "format", "minimum", "maximum", "minLength", "maxLength", + "minItems", "maxItems", "pattern", "exclusiveMinimum", "exclusiveMaximum", + # Metadata + "default", "examples", "const", "title", + # Composition (Gemini doesn't support these) + "oneOf", "anyOf", "allOf", "not", + # Other + "nullable", "$ref", "$id", "$comment", + # Object schema extensions + "propertyNames", "patternProperties", "unevaluatedProperties", + "dependentSchemas", "dependentRequired", "if", "then", "else", + ] + for key in unsupported_keys: + schema.pop(key, None) # Recursively clean nested schemas (properties, items, etc.) - for key, value in list(schema.items()): # Use list() to allow modification during iteration + for key, value in list(schema.items()): schema[key] = clean_gemini_schema(value) elif isinstance(schema, list): - # Recursively clean items in a list return [clean_gemini_schema(item) for item in schema] return schema