fix: handle null response.output in parse_response#3349
Conversation
The chatgpt.com Codex backend sometimes sends response.output: null in the consolidated response.completed event. The previous code iterated response.output without checking for None, causing TypeError. Add a fallback to empty list when response.output is None. Fixes openai#3325
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: dadb254510
ℹ️ 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 streamed output when final payload omits it
When the streaming response.completed event has event.response.output == None (the scenario described by this fix), this fallback makes parse_response() build a final ParsedResponse with output=[]. In the streaming path, ResponseStreamState.accumulate_event() calls parse_response(response=event.response) for response.completed instead of parsing the accumulated snapshot, so get_final_response() and the emitted completed event lose all output items/text/tool calls that arrived via earlier response.output_item.added and delta events. This avoids the crash but turns valid streamed responses into empty final responses; the fallback should preserve the accumulated snapshot or otherwise not replace missing final output with an empty list.
Useful? React with 👍 / 👎.
What breaks
parse_response()insrc/openai/lib/_parsing/_responses.pycrashes withTypeErrorwhenresponse.outputisNone.The chatgpt.com Codex backend (
https://chatgpt.com/backend-api/codex) sometimes sendsresponse.output: nullin the consolidatedresponse.completedevent — even when validresponse.output_item.doneevents were streamed earlier. The SDK then raisesTypeError: 'NoneType' object is not iterableinsideparse_response().Fixes #3325
Root cause
Line 61 does
for output in response.output:without checking forNone. When the Codex backend sendsresponse.output: null, iterating overNonecrashes.Fix
Add a fallback to empty list when
response.outputisNone:This is safe because:
response.outputis a valid list, it iterates normallyresponse.outputisNone, it iterates over nothing (no output items to parse)