Skip to content

Fix: IndexError when streaming with multiple content blocks (#1192)#1251

Open
Adityakk9031 wants to merge 1 commit intoanthropics:mainfrom
Adityakk9031:#1192
Open

Fix: IndexError when streaming with multiple content blocks (#1192)#1251
Adityakk9031 wants to merge 1 commit intoanthropics:mainfrom
Adityakk9031:#1192

Conversation

@Adityakk9031
Copy link

@Adityakk9031 Adityakk9031 commented Mar 17, 2026

Fix-#1192
Problem
An IndexError: list index out of range is raised at current_snapshot.content[event.index] inside both
accumulate_event
(line 465) and
build_events
(line 407) when streaming responses with multiple content blocks.

The root cause is content_block_start blindly calling .append() without validating event.index against the current list length — a known gap flagged by a # TODO: check index comment at that line. If the server sends a non-contiguous index, the list is shorter than expected, and any subsequent content_block_delta or content_block_stop for that index crashes.

Fix
5 changes in
src/anthropic/lib/streaming/_messages.py
:

content_block_start — resolve the TODO: validate event.index; if ahead of the list, pad with empty placeholder blocks and emit a RuntimeWarning.
content_block_delta in
accumulate_event
— early-return if event.index >= len(content).
content_block_stop in
accumulate_event
— same guard.
content_block_delta in
build_events
— same guard.
content_block_stop in
build_events
— same guard.

@Adityakk9031 Adityakk9031 requested a review from a team as a code owner March 17, 2026 15:41
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.

1 participant