Skip to content

responses.stream: parse_response crashes with TypeError when response.output is None #3459

Description

@nicha16

Describe the bug

parse_response in src/openai/lib/_parsing/_responses.py iterates response.output without guarding against None:

# src/openai/lib/_parsing/_responses.py
def parse_response(...):
    output_list: List[...] = []
    for output in response.output:   # <-- TypeError if response.output is None

When a streaming Responses API call accumulates a final Response whose output is None, the stream state machine crashes with an unhelpful TypeError: 'NoneType' object is not iterable deep inside the SDK — it surfaces from accumulate_event, not as a catchable API error, so it takes down the whole stream rather than yielding an empty/degraded result.

This is still present on main as of today (also reproduces on 2.32.0).

To Reproduce

Using the streaming Responses API where the completed response has output=None (observed with a backend that returns a reasoning-only / incomplete response with a null output array):

with client.responses.stream(model=..., input=[...]) as stream:
    for event in stream:      # raises during event accumulation
        ...
    final = stream.get_final_response()

Traceback:

File ".../openai/lib/streaming/responses/_responses.py", line 360, in accumulate_event
    self._completed_response = parse_response(
                               ^^^^^^^^^^^^^^^
File ".../openai/lib/_parsing/_responses.py", line 61, in parse_response
    for output in response.output:
TypeError: 'NoneType' object is not iterable

Expected behavior

parse_response should defensively handle response.output is None and treat it as an empty output list, so a null/absent output yields a ParsedResponse with no output items instead of crashing the stream. output being null can occur for incomplete/failed/reasoning-only responses and via proxy/aggregator backends; the SDK hard-crashing here is difficult to handle downstream because the error originates inside the streaming accumulator.

Suggested fix

One line:

-    for output in response.output:
+    for output in (response.output or []):

Happy to open a PR if that's the preferred path.

Environment

  • openai-python: 2.32.0 (unguarded loop confirmed still on main)
  • Python: 3.11
  • API surface: responses.stream(...) (streaming Responses API)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions