Skip to content

fix: defer parsing incomplete streamed text#3378

Open
he-yufeng wants to merge 1 commit into
openai:mainfrom
he-yufeng:fix/defer-incomplete-stream-parse
Open

fix: defer parsing incomplete streamed text#3378
he-yufeng wants to merge 1 commit into
openai:mainfrom
he-yufeng:fix/defer-incomplete-stream-parse

Conversation

@he-yufeng
Copy link
Copy Markdown

Fixes #3263.

Summary

  • defer JSON parse failures from response.output_text.done so an incomplete structured stream can reach the terminal response.incomplete event
  • keep schema validation errors on complete-but-invalid structured text unchanged
  • add stream state coverage for incomplete JSON, schema validation, and completed-response parsing

To verify

  • PYTHONPATH=src python -m pytest tests/lib/responses/test_responses.py -q
  • PYTHONPATH=src python -m ruff check src/openai/lib/streaming/responses/_responses.py tests/lib/responses/test_responses.py
  • PYTHONPATH=src python -m ruff format --check src/openai/lib/streaming/responses/_responses.py tests/lib/responses/test_responses.py
  • git diff --check

@he-yufeng he-yufeng requested a review from a team as a code owner June 7, 2026 18:29
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d60d06b1b6

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +386 to +387
def _is_json_parse_error(exc: pydantic.ValidationError) -> bool:
return any("json" in str(error.get("type", "")).lower() for error in exc.errors())
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Distinguish top-level JSON parse failures

When a structured model contains a JSON-validated field, e.g. a Pydantic Json[...] field, Pydantic reports invalid field contents with a json_* error type even though the top-level response text parsed successfully. This helper treats those schema validation failures as truncation and returns None from response.output_text.done, so an incomplete stream with complete-but-invalid structured text can silently skip the validation error that this change intended to preserve. Check that the JSON error is for the top-level parse (for example by inspecting the error location) before suppressing it.

Useful? React with 👍 / 👎.

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