AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Responses API Streaming With Nested Pydantic Schema Produces Invalid Strict JSON Schema Due To Missing $Defs Hardening

When streaming with use_responses_api=True and with_structured_output(...), nested Pydantic models generate an invalid strict schema: $defs entries lack additionalProperties=false and required all fields, causing OpenAI 400. Non-streaming works because SDK builds schema from responses.parse.

highConfidence 93%LangChainAffected Vlangchain-Openai 1.3.2Affected Vlangchain-Core 1.4.7

Origin Analysis

langchain_openai's streaming branch overrides the caller's strict=False to true and manually converts Pydantic schema to dict; langchain_core's strict schema conversion (_recursive_set_additional_properties_false and _convert_to_strict_schema) does not recurse into $defs, so nested models referenced via $ref are not hardened for strict mode.
1. Define nested Pydantic models Step and Plan where Plan has list[Step]. 2. Configure ChatOpenAI(model='gpt-4.1', api_key='dummy', use_responses_api=True). 3. Call model.with_structured_output(Plan, method='json_schema', strict=False). 4. Invoke inside astream_events() or astream_log() so streaming path is used. 5. Observe 400 invalid_json_schema: additionalProperties required to be false for $defs.Step.

Fixing Code Block

Edge Case Audit

Requires coordinated upgrade of langchain-core and langchain-openai; if only one is updated, strict=True streaming may still send invalid schema. Rolling back after upgrade may reintroduce the 400. If response_format is a Pydantic class and strict is not passed in kwargs, strict may default to false even if previously hardcoded true; ensure all calls pass explicit strict if strict behavior is required. Self-referential schemas are safe because $refs are not followed during conversion, but custom non-Pydantic schemas with unusual inline structures should be tested.

Ecosystem Topology