|
24 | 24 | RESPONSE_TOOL_NAME = "submit_tool_response" |
25 | 25 | RESPONSE_KEY = "response" |
26 | 26 | _DEFS_PREFIX = "#/$defs/" |
| 27 | +# Keys whose value maps names to schemas, and keys whose value is data. |
| 28 | +_SCHEMA_MAP_KEYS = frozenset( |
| 29 | + {"properties", "patternProperties", "$defs", "definitions"} |
| 30 | +) |
| 31 | +_DATA_KEYS = frozenset({"default", "examples", "const", "enum", "required"}) |
27 | 32 |
|
28 | 33 | logger = logging.getLogger(__name__) |
29 | 34 |
|
@@ -81,6 +86,81 @@ def resolve(node: Any, active: frozenset[str]) -> Any: |
81 | 86 | return inlined, leftover |
82 | 87 |
|
83 | 88 |
|
| 89 | +def _narrowed_type(branch_type: Any, types: list[str]) -> Any: |
| 90 | + """Return the part of ``branch_type`` that ``types`` allows, or None.""" |
| 91 | + if branch_type in types or (branch_type == "integer" and "number" in types): |
| 92 | + return branch_type |
| 93 | + if branch_type == "number" and "integer" in types: |
| 94 | + return "integer" |
| 95 | + return None |
| 96 | + |
| 97 | + |
| 98 | +def _restrict_to_types(branches: list[Any], types: list[str]) -> list[Any]: |
| 99 | + """Narrow ``anyOf`` branches to ``types``, for a node that has both. |
| 100 | +
|
| 101 | + A branch with its own type is kept, narrowed, only when ``types`` allows |
| 102 | + it; a branch without one is copied once per type in ``types``. |
| 103 | + """ |
| 104 | + restricted: list[Any] = [] |
| 105 | + for branch in branches: |
| 106 | + if not isinstance(branch, dict): |
| 107 | + restricted.append(branch) |
| 108 | + elif "type" not in branch: |
| 109 | + restricted.extend({**branch, "type": t} for t in types) |
| 110 | + elif (narrowed := _narrowed_type(branch["type"], types)) is not None: |
| 111 | + restricted.append({**branch, "type": narrowed}) |
| 112 | + # No branch fits the types: keep the branches rather than send an empty anyOf. |
| 113 | + return restricted or branches |
| 114 | + |
| 115 | + |
| 116 | +def _to_gemini_schema(schema: Any) -> Any: |
| 117 | + """Rewrite JSON-schema type lists into the form Gemini accepts. |
| 118 | +
|
| 119 | + Gemini's function declarations take a single ``type`` and mark nullable |
| 120 | + fields with ``nullable: true``; a type list such as ``["string", "null"]`` |
| 121 | + (common in MCP tool schemas) fails the request with a 400. So |
| 122 | + ``{"type": ["string", "null"]}`` becomes |
| 123 | + ``{"type": "string", "nullable": true}``, and a list with several non-null |
| 124 | + types becomes an ``anyOf`` of single types. When the node already has an |
| 125 | + ``anyOf``, the types are applied to its branches instead, so both |
| 126 | + constraints still hold. Values that are data rather than schemas |
| 127 | + (``default``, ``enum``, ...) are left untouched. |
| 128 | + """ |
| 129 | + if isinstance(schema, list): |
| 130 | + return [_to_gemini_schema(item) for item in schema] |
| 131 | + if not isinstance(schema, dict): |
| 132 | + return schema |
| 133 | + |
| 134 | + converted: dict[str, Any] = {} |
| 135 | + for key, value in schema.items(): |
| 136 | + if key in _SCHEMA_MAP_KEYS and isinstance(value, dict): |
| 137 | + converted[key] = { |
| 138 | + name: _to_gemini_schema(sub) for name, sub in value.items() |
| 139 | + } |
| 140 | + elif key in _DATA_KEYS: |
| 141 | + converted[key] = value |
| 142 | + else: |
| 143 | + converted[key] = _to_gemini_schema(value) |
| 144 | + |
| 145 | + types = converted.get("type") |
| 146 | + if isinstance(types, list): |
| 147 | + non_null = [t for t in types if t != "null"] |
| 148 | + if len(non_null) == 1: |
| 149 | + converted["type"] = non_null[0] |
| 150 | + else: |
| 151 | + del converted["type"] |
| 152 | + if len(non_null) > 1: |
| 153 | + branches = converted.get("anyOf") |
| 154 | + converted["anyOf"] = ( |
| 155 | + _restrict_to_types(branches, non_null) |
| 156 | + if isinstance(branches, list) |
| 157 | + else [{"type": t} for t in non_null] |
| 158 | + ) |
| 159 | + if "null" in types: |
| 160 | + converted["nullable"] = True |
| 161 | + return converted |
| 162 | + |
| 163 | + |
84 | 164 | def build_response_tool(schema: dict[str, Any], description: str) -> dict[str, Any]: |
85 | 165 | """Build a normalized-API function tool that wraps ``schema`` under ``response``. |
86 | 166 |
|
@@ -223,7 +303,30 @@ class AnthropicStructuredOutput(ToolCallStructuredOutput): |
223 | 303 |
|
224 | 304 |
|
225 | 305 | class GeminiStructuredOutput(ToolCallStructuredOutput): |
226 | | - """Gemini returns empty content for ``response_format``; go straight to tools.""" |
| 306 | + """Gemini returns empty content for ``response_format``; go straight to tools. |
| 307 | +
|
| 308 | + The tool schema is rewritten into the form Gemini accepts first. |
| 309 | + """ |
| 310 | + |
| 311 | + async def generate( |
| 312 | + self, |
| 313 | + llm: Any, |
| 314 | + messages: list[dict[str, str]], |
| 315 | + *, |
| 316 | + schema: dict[str, Any], |
| 317 | + response_format_name: str, |
| 318 | + description: str, |
| 319 | + completion_kwargs: dict[str, Any], |
| 320 | + ) -> Any: |
| 321 | + """Force a tool call with a Gemini-compatible schema.""" |
| 322 | + return await super().generate( |
| 323 | + llm, |
| 324 | + messages, |
| 325 | + schema=_to_gemini_schema(schema), |
| 326 | + response_format_name=response_format_name, |
| 327 | + description=description, |
| 328 | + completion_kwargs=completion_kwargs, |
| 329 | + ) |
227 | 330 |
|
228 | 331 |
|
229 | 332 | def _strategy_for_model(model: str | None) -> ToolCallStructuredOutput: |
|
0 commit comments