fix: defer parsing incomplete streamed text#3378
Conversation
There was a problem hiding this comment.
💡 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".
| def _is_json_parse_error(exc: pydantic.ValidationError) -> bool: | ||
| return any("json" in str(error.get("type", "")).lower() for error in exc.errors()) |
There was a problem hiding this comment.
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 👍 / 👎.
Fixes #3263.
Summary
response.output_text.doneso an incomplete structured stream can reach the terminalresponse.incompleteeventTo verify
PYTHONPATH=src python -m pytest tests/lib/responses/test_responses.py -qPYTHONPATH=src python -m ruff check src/openai/lib/streaming/responses/_responses.py tests/lib/responses/test_responses.pyPYTHONPATH=src python -m ruff format --check src/openai/lib/streaming/responses/_responses.py tests/lib/responses/test_responses.pygit diff --check