|
46 | 46 | from ...models.base_llm import BaseLlm |
47 | 47 | from ...models.llm_request import LlmRequest |
48 | 48 | from ...models.llm_response import LlmResponse |
| 49 | +from ._openai_schema import enforce_strict_openai_schema |
49 | 50 |
|
50 | 51 | logger = logging.getLogger("google_adk." + __name__) |
51 | 52 |
|
@@ -180,29 +181,6 @@ def _content_to_openai_messages( |
180 | 181 | return messages |
181 | 182 |
|
182 | 183 |
|
183 | | -def _enforce_strict_openai_schema(schema: dict[str, Any]) -> None: |
184 | | - """Recursively transforms a JSON schema for OpenAI strict structured outputs.""" |
185 | | - if not isinstance(schema, dict): |
186 | | - return |
187 | | - if "$ref" in schema: |
188 | | - for key in list(schema.keys()): |
189 | | - if key != "$ref": |
190 | | - del schema[key] |
191 | | - return |
192 | | - if schema.get("type") == "object" and "properties" in schema: |
193 | | - schema["additionalProperties"] = False |
194 | | - schema["required"] = sorted(schema["properties"].keys()) |
195 | | - for defn in schema.get("$defs", {}).values(): |
196 | | - _enforce_strict_openai_schema(defn) |
197 | | - for prop in schema.get("properties", {}).values(): |
198 | | - _enforce_strict_openai_schema(prop) |
199 | | - for key in ("anyOf", "oneOf", "allOf"): |
200 | | - for item in schema.get(key, []): |
201 | | - _enforce_strict_openai_schema(item) |
202 | | - if "items" in schema and isinstance(schema["items"], dict): |
203 | | - _enforce_strict_openai_schema(schema["items"]) |
204 | | - |
205 | | - |
206 | 184 | def _update_type_string(value: Any): |
207 | 185 | """Lowercases nested JSON schema type strings for OpenAI compatibility.""" |
208 | 186 | if isinstance(value, list): |
@@ -406,7 +384,7 @@ async def generate_content_async( |
406 | 384 | schema_name = str(schema_dict["title"]) |
407 | 385 |
|
408 | 386 | if schema_dict: |
409 | | - _enforce_strict_openai_schema(schema_dict) |
| 387 | + enforce_strict_openai_schema(schema_dict) |
410 | 388 | response_format = { |
411 | 389 | "type": "json_schema", |
412 | 390 | "json_schema": { |
|
0 commit comments