Recursively removes unsupported fields from a JSON schema for Gemini.
(schema: Any)
| 130 | |
| 131 | # Helper function to clean schema for Gemini |
| 132 | def clean_gemini_schema(schema: Any) -> Any: |
| 133 | """Recursively removes unsupported fields from a JSON schema for Gemini.""" |
| 134 | if isinstance(schema, dict): |
| 135 | # Remove specific keys unsupported by Gemini tool parameters |
| 136 | schema.pop("additionalProperties", None) |
| 137 | schema.pop("default", None) |
| 138 | |
| 139 | # Check for unsupported 'format' in string types |
| 140 | if schema.get("type") == "string" and "format" in schema: |
| 141 | allowed_formats = {"enum", "date-time"} |
| 142 | if schema["format"] not in allowed_formats: |
| 143 | logger.debug( |
| 144 | f"Removing unsupported format '{schema['format']}' for string type in Gemini schema." |
| 145 | ) |
| 146 | schema.pop("format") |
| 147 | |
| 148 | # Recursively clean nested schemas (properties, items, etc.) |
| 149 | for key, value in list( |
| 150 | schema.items() |
| 151 | ): # Use list() to allow modification during iteration |
| 152 | schema[key] = clean_gemini_schema(value) |
| 153 | elif isinstance(schema, list): |
| 154 | # Recursively clean items in a list |
| 155 | return [clean_gemini_schema(item) for item in schema] |
| 156 | return schema |
| 157 | |
| 158 | |
| 159 | # Models for Anthropic API requests |
no outgoing calls
no test coverage detected