Skip to content

IndexError in accumulate_event during Responses API streaming #2852

@natanr123

Description

@natanr123

Bug Report

Description

IndexError: list index out of range occurs inside the OpenAI SDK during streaming of Responses API events. The error originates in openai/lib/streaming/responses/_responses.py line 344 in accumulate_event, called from handle_event (line 248) and __stream__ (line 156).

This is not caused by application code — it happens internally within the SDK's stream accumulator.

Stack Trace

File "/app/.venv/lib/python3.13/site-packages/openai/lib/streaming/responses/_responses.py", line 151, in __aiter__
File "/app/.venv/lib/python3.13/site-packages/openai/lib/streaming/responses/_responses.py", line 156, in __stream__
File "/app/.venv/lib/python3.13/site-packages/openai/lib/streaming/responses/_responses.py", line 248, in handle_event
File "/app/.venv/lib/python3.13/site-packages/openai/lib/streaming/responses/_responses.py", line 344, in accumulate_event
    IndexError: list index out of range

Reproduction Context

  • SDK version: openai==2.16.0
  • Python version: 3.13
  • API: Responses API with streaming (stream=True)
  • Model: gpt-5-nano
  • Features used: tools=[{"type": "web_search"}], structured output with text_format (Pydantic model)
  • Streaming method: AsyncOpenAI().responses.stream() and resume via responses.stream() with starting_after

Code Pattern

async with client.responses.stream(...) as stream:
    async for event in stream:  # IndexError thrown here intermittently
        ...

The error occurs intermittently — most streams complete fine, but occasionally the accumulator hits an index that doesn't exist in its internal list.

Workaround

Wrapping the stream iteration in a try/except IndexError to catch and log the error, since the stream has typically delivered the important data by the time this occurs.

Notes

A similar class of bug exists in the Assistants API streaming path (_assistants.py in accumulate_event) — see #1736 and #1882. This issue is for the newer Responses API streaming path (responses/_responses.py).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions