Skip to content

fix: handle null response.output in parse_response#3361

Open
xlyoung wants to merge 1 commit into
openai:mainfrom
xlyoung:fix/parse-response-null-output
Open

fix: handle null response.output in parse_response#3361
xlyoung wants to merge 1 commit into
openai:mainfrom
xlyoung:fix/parse-response-null-output

Conversation

@xlyoung
Copy link
Copy Markdown

@xlyoung xlyoung commented Jun 4, 2026

Problem

parse_response() in src/openai/lib/_parsing/_responses.py iterates response.output without checking for None. The chatgpt.com Codex backend sometimes sends response.output: null in the consolidated response.completed event, even when valid response.output_item.done events were streamed earlier in the same response.

This causes TypeError: 'NoneType' object is not iterable, killing the entire stream before the consumer can read the deltas.

Fix

Use (response.output or []) to handle null output gracefully:

# Before
for output in response.output:

# After  
for output in (response.output or []):

Testing

  • Import test passes
  • No existing tests for parse_response to verify against
  • The fix is a defensive null check that doesn't change behavior when output is present

Fixes #3325

The parse_response() function iterates response.output without checking
for None. The chatgpt.com Codex backend sometimes sends response.output:
null in the consolidated response.completed event, even when valid
response.output_item.done events were streamed earlier.

This causes TypeError: 'NoneType' object is not iterable, killing the
entire stream before the consumer can read the deltas.

Fix: use (response.output or []) to handle null output gracefully.

Fixes openai#3325
@xlyoung xlyoung requested a review from a team as a code owner June 4, 2026 03:46
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4a9b095c0a

ℹ️ 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 []):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve streamed output when completed payload is null

When the streaming backend sends response.completed.response.output: null after already sending response.output_item.* and text/function deltas, this turns the completed response into output=[]. In ResponseStreamState.accumulate_event, the final response.completed event and get_final_response() are built from parse_response(event.response), not from the accumulated snapshot, so consumers no longer crash but silently lose all streamed output and parsed content in exactly the scenario this change is meant to handle. The null case should fall back to the accumulated snapshot/output rather than an empty list.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parse_response crashes with TypeError when response.output is null in response.completed event (chatgpt.com Codex backend)

1 participant