diff --git a/src/anthropic/lib/streaming/_beta_messages.py b/src/anthropic/lib/streaming/_beta_messages.py index c1447a8d..49d43170 100644 --- a/src/anthropic/lib/streaming/_beta_messages.py +++ b/src/anthropic/lib/streaming/_beta_messages.py @@ -482,6 +482,12 @@ def accumulate_event( ), ) elif event.type == "content_block_delta": + 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." + ) content = current_snapshot.content[event.index] if event.delta.type == "text_delta": if content.type == "text": diff --git a/src/anthropic/lib/streaming/_messages.py b/src/anthropic/lib/streaming/_messages.py index b6b5f538..8027a9e5 100644 --- a/src/anthropic/lib/streaming/_messages.py +++ b/src/anthropic/lib/streaming/_messages.py @@ -462,6 +462,12 @@ def accumulate_event( ), ) elif event.type == "content_block_delta": + 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." + ) content = current_snapshot.content[event.index] if event.delta.type == "text_delta": if content.type == "text":