Skip to content

fix(streaming): validate content_block_start index before appending#1690

Open
WalkingDreams798 wants to merge 1 commit into
anthropics:mainfrom
WalkingDreams798:fix/streaming-content-block-index
Open

fix(streaming): validate content_block_start index before appending#1690
WalkingDreams798 wants to merge 1 commit into
anthropics:mainfrom
WalkingDreams798:fix/streaming-content-block-index

Conversation

@WalkingDreams798

Copy link
Copy Markdown

What

accumulate_event (in both lib/streaming/_messages.py and _beta_messages.py) appended each content_block_start block to the snapshot without checking event.index — it silently relied on blocks arriving strictly in order. This was the long-standing # TODO: check index in both files.

If a content_block_start ever arrived with an out-of-order or skipped index, the block would land at the wrong position in current_snapshot.content, and subsequent content_block_delta / content_block_stop events (which index by event.index) would mutate the wrong block or raise an opaque IndexError.

Change

Validate that event.index == len(current_snapshot.content) before appending, and raise a clear RuntimeError on mismatch — consistent with the existing 'Unexpected event order, got ... before "message_start"' style already used in the same function.

Applied identically to the standard and beta accumulators, resolving both TODOs.

Tests

Added tests/lib/streaming/test_content_block_index.py covering, for both the standard and beta paths:

  • sequential indices append correctly
  • a mismatched index raises RuntimeError

Verification

  • uv run pytest tests/lib/streaming → 44 passed
  • uv run ruff check → all checks passed
  • uv run pyright (strict) on changed files → 0 errors, 0 warnings

`accumulate_event` appended each `content_block_start` block to the snapshot
without checking `event.index`, silently relying on blocks arriving in order
(this was the long-standing `# TODO: check index`). An out-of-order or skipped
index would corrupt the snapshot — later `content_block_delta`/`content_block_stop`
events index into the wrong block or raise an opaque IndexError.

Validate that `event.index == len(current_snapshot.content)` and raise a clear
RuntimeError on mismatch, matching the existing "Unexpected event order" style.
Applied to both the standard and beta accumulators, with unit tests covering
sequential appends and the mismatch error.
@WalkingDreams798 WalkingDreams798 requested a review from a team as a code owner June 19, 2026 18:10
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