fix: handle null response output in parser#3362
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f3adae0750
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| output_list: List[ParsedResponseOutputItem[TextFormatT]] = [] | ||
|
|
||
| for output in response.output: | ||
| for output in response.output or []: |
There was a problem hiding this comment.
Preserve streamed output when completed event omits it
When the Responses stream has already accumulated output items and the final response.completed event carries output: null (the case this change is trying to tolerate), this line converts the final response's output to []. I checked ResponseStreamState.accumulate_event: on response.completed it sets _completed_response = parse_response(... event.response ...) (src/openai/lib/streaming/responses/_responses.py:359-364), and get_final_response() returns that object, not the accumulated snapshot. As a result, streamed calls with text/tool output but a null final output no longer raise, but response.completed.response, get_final_response().output, and output_text are empty, silently dropping the data sent by the earlier stream events.
Useful? React with 👍 / 👎.
Changes being requested
Fixes #3325.
parse_responsenow treats a nullresponse.outputas an empty output list before iterating, avoiding aTypeErrorwhen a completed response event hasoutput: null. Added a regression test that constructs that response shape and verifies parsing returnsoutput=[].AI-assisted: yes.
Additional context & links
Verification run locally:
.venv/bin/python -m pytest -o addopts='' tests/lib/responses/test_responses.py -q-> 6 passed.venv/bin/ruff format --check src/openai/lib/_parsing/_responses.py tests/lib/responses/test_responses.py-> 2 files already formatted.venv/bin/ruff check src/openai/lib/_parsing/_responses.py tests/lib/responses/test_responses.py-> all checks passed.venv/bin/mypy src/openai/lib/_parsing/_responses.py-> success, no issues found in 1 source filegit diff --check-> passedNot run: full
./scripts/test; the repository docs note most tests require the mock server, and this change is covered by the focused responses parser test file.