Repository navigation
fix(openai): send the user's types.Schema output_schema to Chat Completions - #8006
Yasser-Ameur wants to merge 4 commits into
Conversation
…etions
OpenAILlm checked isinstance(schema, BaseModel) before anything else, and
types.Schema is a pydantic model, so a types.Schema response_schema was
turned into schema.__class__.model_json_schema(): the JSON schema of the
Schema class itself. After the strict pass the request carried
{"$ref": "#/$defs/Schema"} under the name "Schema" and the user's schema
was never enforced.
Convert a types.Schema with model_dump and lowercase its types before the
BaseModel branch, as OpenAIResponsesLlm._schema_to_dict and LiteLlm
already do.
BlueRaddish
left a comment
There was a problem hiding this comment.
The new array-schema case still cannot be used as a strict Structured Output on the default OpenAI Chat Completions endpoint. It now sends the user's array instead of the types.Schema meta-schema, but the resulting root schema is {"type": "array", "items": {"type": "string"}} with strict: true. The official Structured Outputs contract requires an object root; arrays are supported as nested properties. A types.Schema(type=STRING) has the same remaining issue.
At head b51b5f3cef40ee0cb0021e9f31b29bb726467b16, I captured the actual OpenAI SDK's serialized request to https://api.openai.com/v1/chat/completions through a no-network httpx.MockTransport. The array input produces:
{"type":"json_schema","json_schema":{"name":"response","strict":true,"schema":{"items":{"type":"string"},"type":"array"}}}Could this branch reject unsupported root shapes locally for the default OpenAI backend, or provide an explicit object-wrapper adaptation with matching response handling? The regression should distinguish a supported object containing an array from an unsupported top-level array, rather than treating a captured raw-array payload as a successful strict-mode request. An explicit OpenAI-compatible backend may have a different contract, so this need not impose the OpenAI restriction on every custom base_url.
I checked the exact complete base/head generate_content_async methods and schema helpers, with the unchanged _openai_common module, real supported google-genai==2.32.0, openai==2.33.0, and actual SDK serialization. Four object-root controls (including a nested array, BaseModel, and dict schema) produced object roots; a custom-host array control was kept separate. The PR base 946ca77224d5b3e1bf7c5a1dd0a39e9b17c3e655 already sends the wrong meta-schema, so this is an incomplete-fix/provider-contract finding, not a newly introduced working-to-failing regression.
Scope: model/request holders isolate unrelated ADK initialization, and response conversion is an identity boundary after real SDK parsing. No full ADK/Runner/owning suite or live provider/server request was run; remote rejection is inferred from the documented schema contract, not claimed as a measured API response.
| schema_dict = {} | ||
|
|
||
| if isinstance(schema, type) and issubclass(schema, BaseModel): | ||
| if isinstance(schema, types.Schema): |
There was a problem hiding this comment.
one more gap on the way to parity with OpenAIResponsesLlm: that model reads config.response_schema or config.response_json_schema, but this path only checks response_schema, so GenerateContentConfig(response_json_schema={...}) sends no response_format at all on Chat Completions. and a dict title like "Weather Report" goes out as the json_schema name unsanitized, which OpenAI rejects (^[a-zA-Z0-9_-]+$), while Responses already sanitizes it:
@@ -430,8 +431,14 @@ class OpenAILlm(BaseLlm):
response_format = None
- if llm_request.config and llm_request.config.response_schema:
- schema = llm_request.config.response_schema
+ if llm_request.config and (
+ llm_request.config.response_schema
+ or llm_request.config.response_json_schema
+ ):
+ schema = (
+ llm_request.config.response_schema
+ or llm_request.config.response_json_schema
+ )
schema_name = "response"
@@ -449,7 +456,11 @@ class OpenAILlm(BaseLlm):
if "title" in schema_dict:
- schema_name = str(schema_dict["title"])
+ # OpenAI requires the json_schema name to match ^[a-zA-Z0-9_-]+$.
+ schema_name = (
+ re.sub(r"[^a-zA-Z0-9_-]", "_", str(schema_dict["title"]))
+ or "response"
+ )pushed the fix plus a test (and the re import) here, should cherry pick clean on your branch: its-amann@35faaa5
There was a problem hiding this comment.
Both are real gaps: response_json_schema is not read on this path, and a dict title is sent as the schema name without sanitizing. I kept them out of this PR so it stays on the types.Schema fix, and they fit a follow-up once this one is in.
|
@BlueRaddish you are right about the array root. The test now uses a |
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
output_schemagiven astypes.Schemareaches Chat Completions as that schema, in strict mode, like it does onOpenAIResponsesLlmandLiteLlm.OpenAILlmsends the JSON schema of thetypes.Schemaclass itself (its 7 KB meta schema), namedSchema. After the strict pass the request carries{"$ref": "#/$defs/Schema"}, so the user's schema is never enforced.response_formatbuilder inintegrations/openai/_openai_llm.pytestsisinstance(schema, BaseModel)before anytypes.Schemacheck.types.Schemais a pydantic model, so it takes that branch andschema.__class__.model_json_schema()describes the class, not the value.OpenAILlmwith a mockedchat.completions.createcapturing kwargs,response_schema=types.Schema(type=ARRAY, items=Schema(type=STRING)); inspectresponse_format.Solution: check for a
types.Schemainstance first and convert it withmodel_dump(by_alias=True, exclude_none=True, mode="json")pluslowercase_schema_types, the same conversionOpenAIResponsesLlm._schema_to_dictandLiteLlmuse, then run the existing strict pass. TheBaseModeland dict paths are unchanged.Testing Plan
Unit Tests:
test_response_schema_genai_schema_becomes_json_schema_formatintest_openai_llm.py, parametrized over an array schema and an object schema; both cases fail on main.pytest tests/unittests/integrations/openai:293 passed.Manual End-to-End (E2E) Tests:
No live OpenAI call was made. A script with a mocked client was run on main and on this branch, capturing the sent
response_format.Before:
name: Schema,schema: {"$ref": "#/$defs/Schema"}.After:
name: response,schema: {"items": {"type": "string"}, "type": "array"}.Checklist
Additional context
A
list[...]output_schemastill sends noresponse_formaton this path; that is a separate gap, andLiteLlmhas the same one.