Handle null response output during parsing#3330
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c5de32017b
ℹ️ 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".
| output_list: List[ParsedResponseOutputItem[TextFormatT]] = [] | ||
|
|
||
| for output in response.output: | ||
| for output in response.output or []: |
There was a problem hiding this comment.
Preserve accumulated output when completed payload is null
When a streaming backend sends response.completed with output: null after earlier response.output_item.* events, this fallback parses the final payload as output=[]. The streaming accumulator sets _completed_response by calling parse_response(event.response, ...) on completion (src/openai/lib/streaming/responses/_responses.py:359-364), so ResponseCompletedEvent.response and get_final_response() lose all message/tool output that was already accumulated in the snapshot instead of letting consumers inspect it. In this null-output case, the parser/stream state should preserve the accumulated snapshot output rather than replacing it with an empty list.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in d9b78c4d: ResponseStreamState now preserves the accumulated snapshot output when the final response.completed payload has output: null, and the new streaming regression test covers the created/output_item/content_part/text_delta/completed sequence.
Summary
Fixes #3325.
parse_response()currently assumesresponse.outputis always iterable. Some streaming backends can send a finalresponse.completedpayload withoutput: nullafter emitting valid output-item events, which causes a bareTypeErrorin the response stream accumulator.This treats a null
outputthe same as an empty output list during parsing, so stream consumers can still complete and inspect the final response instead of crashing in the parser.Tests
uv run python -m pytest -o addopts="" tests/lib/responses/test_responses.py -quv run ruff check src/openai/lib/_parsing/_responses.py tests/lib/responses/test_responses.pyuv run ruff format --check src/openai/lib/_parsing/_responses.py tests/lib/responses/test_responses.pyuv run pyright src/openai/lib/_parsing/_responses.py tests/lib/responses/test_responses.py