Skip to content

fix(streaming): guard against out-of-bounds content index; fix BetaAsyncAbstractMemoryTool docstring#1570

Open
rmotgi1227 wants to merge 1 commit into
anthropics:mainfrom
rmotgi1227:fix/streaming-index-bounds-and-async-docstring
Open

fix(streaming): guard against out-of-bounds content index; fix BetaAsyncAbstractMemoryTool docstring#1570
rmotgi1227 wants to merge 1 commit into
anthropics:mainfrom
rmotgi1227:fix/streaming-index-bounds-and-async-docstring

Conversation

@rmotgi1227
Copy link
Copy Markdown

Two fixes in this PR.


Fix 1 — IndexError when content_block_delta/stop arrives before content_block_start (fixes #1192)

Files: src/anthropic/lib/streaming/_messages.py, src/anthropic/lib/streaming/_beta_messages.py

accumulate_event indexes directly into current_snapshot.content[event.index] for both content_block_delta and content_block_stop events. If the server sends a delta or stop for an index that hasn't been populated by a preceding content_block_start (server error, reconnect, or race condition), an unhandled IndexError propagates to the caller.

Add a bounds guard and return the snapshot unchanged in that case:

# BEFORE
elif event.type == "content_block_delta":
    content = current_snapshot.content[event.index]

# AFTER
elif event.type == "content_block_delta":
    if event.index >= len(current_snapshot.content):
        return current_snapshot
    content = current_snapshot.content[event.index]

Applied to both content_block_delta and content_block_stop in both _messages.py and _beta_messages.py (4 guards total).


Fix 2 — BetaAsyncAbstractMemoryTool docstring is a copy-paste of the sync class (fixes #1290)

File: src/anthropic/lib/tools/_beta_builtin_memory_tool.py

The docstring for BetaAsyncAbstractMemoryTool was a verbatim copy of BetaAbstractMemoryTool's docstring, instructing users to:

  • Subclass BetaAbstractMemoryTool (the sync class) ❌
  • Use def view / def create (sync methods) ❌
  • Use Anthropic() (sync client) ❌

Following the example verbatim raises TypeError at runtime. Updated to show the correct async class name, async def methods, AsyncAnthropic client, and asyncio.run() wrapper.

…te_event; fix BetaAsyncAbstractMemoryTool docstring

Two fixes:

1. _messages.py + _beta_messages.py (fixes anthropics#1192): accumulate_event indexes
   directly into current_snapshot.content[event.index] for both
   content_block_delta and content_block_stop events. If the server
   sends a delta/stop for an index that has not yet appeared in a
   content_block_start (server bug, reconnect, or race), this raises
   an unhandled IndexError. Add a bounds check and return the snapshot
   unchanged in that case — identical guard applied to both streaming
   files.

2. _beta_builtin_memory_tool.py (fixes anthropics#1290): BetaAsyncAbstractMemoryTool
   docstring was a verbatim copy of BetaAbstractMemoryTool, telling users
   to subclass the sync class, use sync def methods, and use the sync
   Anthropic() client. Updated to show the correct async subclass name,
   async def methods, AsyncAnthropic client, and asyncio.run() wrapper.
@rmotgi1227 rmotgi1227 requested a review from a team as a code owner May 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.

BetaAsyncAbstractMemoryTool docstring contains sync-only example (copy-paste from sync class) IndexError when streaming

1 participant