Skip to content

fix(streaming): catch parse errors for incomplete responses#3268

Open
xodn348 wants to merge 1 commit into
openai:mainfrom
xodn348:fix/issue-3263
Open

fix(streaming): catch parse errors for incomplete responses#3268
xodn348 wants to merge 1 commit into
openai:mainfrom
xodn348:fix/issue-3263

Conversation

@xodn348
Copy link
Copy Markdown

@xodn348 xodn348 commented May 19, 2026

Fixes #3263

What

When using client.responses.stream() with text_format=PydanticModel, the SDK eagerly parses output text on the response.output_text.done event. If the response is truncated (e.g. by max_output_tokens), this raises a pydantic.ValidationError on the incomplete JSON, preventing the subsequent response.incomplete event from reaching the caller.

This PR catches pydantic.ValidationError and json.JSONDecodeError during structured output parsing in the streaming handler and sets parsed=None instead. This is type-safe since ResponseTextDoneEvent.parsed is already Optional[TextFormatT].

Verification

  • Build: pass
  • Tests: pass (7 passed, including 2 new tests)
  • Lint: pass (ruff)
  • Type check: pass (pyright, 0 errors)

)

When using structured output with streaming (text_format=PydanticModel),
the SDK parses output text on the response.output_text.done event.
If the response is incomplete (e.g. truncated by max_output_tokens),
this raises a Pydantic ValidationError on the incomplete JSON, masking
the subsequent response.incomplete event from the caller.

Catch pydantic.ValidationError and json.JSONDecodeError during parsing
and set parsed=None instead. This is type-safe since ResponseTextDoneEvent
already declares parsed as Optional[TextFormatT].

Fixes openai#3263

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@xodn348 xodn348 requested a review from a team as a code owner May 19, 2026 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Responses streaming structured output parses incomplete JSON before terminal incomplete status

1 participant