fix: parse_response crashes on null output from Codex backend stream#3533
fix: parse_response crashes on null output from Codex backend stream#3533thiagobarbosa wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a TypeError in parse_response() when the Codex backend stream returns a Response with output: null by treating None output as an empty list, and adds regression test coverage for that case.
Changes:
- Guard
parse_response()againstresponse.output is Noneby iterating overresponse.output or []. - Add a unit test asserting
output=Noneis parsed as[]without raising.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/openai/lib/_parsing/_responses.py | Prevents iteration over None by defaulting missing/None output to an empty list. |
| tests/lib/responses/test_responses.py | Adds a regression test to ensure parse_response() handles output=None and returns parsed.output == []. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 22b98e9963
ℹ️ 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 stream output on null completion
When this parser is used from the streaming path, response.completed is parsed from event.response while the actual output items have already been accumulated on the stream snapshot. If the backend sends output: null on the completion event after normal response.output_item.*/delta events, this fallback now returns a successful parsed response with output == []; get_final_response() and the wrapped completed event then lose all generated content instead of returning the accumulated response. Please have the stream completion path reuse the snapshot output when the completed response output is None.
Useful? React with 👍 / 👎.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Fix #3313
Fix a crash in
parse_responsewhen aResponsefrom the Codex backend stream has anull(None)outputfield instead of a list.Before this fix, any consumer receiving a
Responsewith anulloutput(observed from the Codex backend stream) would hit aTypeError: 'NoneType' object is not iterableinsideparse_response. The fix treats a missing/Noneoutput the same as an empty list, matching how other optional list fields are typically handled in this SDK.src/openai/lib/_parsing/_responses.py: iterate overresponse.output or []instead ofresponse.output, so aNoneoutput no longer raises aTypeErrorand is treated as an empty output list. This file lives undersrc/openai/lib/, which perCONTRIBUTING.mdis excluded from code generation, so it's safe to edit by hand.tests/lib/responses/test_responses.py: addtest_parse_response_handles_null_output, which constructs aResponsewithoutput=Noneand assertsparse_response(...)returnsparsed.output == []instead of raising.