fix(streaming): guard against out-of-bounds content index; fix BetaAsyncAbstractMemoryTool docstring#1570
Open
rmotgi1227 wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two fixes in this PR.
Fix 1 —
IndexErrorwhencontent_block_delta/stoparrives beforecontent_block_start(fixes #1192)Files:
src/anthropic/lib/streaming/_messages.py,src/anthropic/lib/streaming/_beta_messages.pyaccumulate_eventindexes directly intocurrent_snapshot.content[event.index]for bothcontent_block_deltaandcontent_block_stopevents. If the server sends a delta or stop for an index that hasn't been populated by a precedingcontent_block_start(server error, reconnect, or race condition), an unhandledIndexErrorpropagates to the caller.Add a bounds guard and return the snapshot unchanged in that case:
Applied to both
content_block_deltaandcontent_block_stopin both_messages.pyand_beta_messages.py(4 guards total).Fix 2 —
BetaAsyncAbstractMemoryTooldocstring is a copy-paste of the sync class (fixes #1290)File:
src/anthropic/lib/tools/_beta_builtin_memory_tool.pyThe docstring for
BetaAsyncAbstractMemoryToolwas a verbatim copy ofBetaAbstractMemoryTool's docstring, instructing users to:BetaAbstractMemoryTool(the sync class) ❌def view/def create(sync methods) ❌Anthropic()(sync client) ❌Following the example verbatim raises
TypeErrorat runtime. Updated to show the correct async class name,async defmethods,AsyncAnthropicclient, andasyncio.run()wrapper.