Skip to content

fix(streaming): replace IndexError with descriptive RuntimeError for out-of-bounds content_block_delta#1273

Open
tao-to-python wants to merge 1 commit intoanthropics:mainfrom
tao-to-python:fix/streaming-index-error-bounds-check
Open

fix(streaming): replace IndexError with descriptive RuntimeError for out-of-bounds content_block_delta#1273
tao-to-python wants to merge 1 commit intoanthropics:mainfrom
tao-to-python:fix/streaming-index-error-bounds-check

Conversation

@tao-to-python
Copy link

Description

Improves error diagnostics for intermittent IndexError during streaming, as reported in #1192.

Problem

In accumulate_event(), current_snapshot.content[event.index] can raise an opaque IndexError when the index exceeds the content list length (e.g. due to a missing content_block_start event or a race condition).

The raw IndexError provides no context about what happened.

Solution

Add bounds checking before the index access with a descriptive error:

if event.index >= len(current_snapshot.content):
    raise RuntimeError(
        f"Received content_block_delta for index {event.index}, "
        f"but snapshot only has {len(current_snapshot.content)} content block(s). "
        f"This may indicate a missing content_block_start event."
    )

Applied to both _messages.py and _beta_messages.py.

Fixes #1192

…out-of-bounds content_block_delta

When content_block_delta arrives with an index that exceeds the
content list length (e.g. due to a missing content_block_start event),
an opaque IndexError is raised with no context.

Add bounds checking with a descriptive error message in both beta
and non-beta streaming accumulators.

Fixes anthropics#1192
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.

IndexError when streaming

1 participant