Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/anthropic/lib/streaming/_beta_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ def accumulate_event(
),
)
elif event.type == "content_block_delta":
if event.index >= len(current_snapshot.content):
return current_snapshot
content = current_snapshot.content[event.index]
if event.delta.type == "text_delta":
if content.type == "text":
Expand Down Expand Up @@ -530,6 +532,8 @@ def accumulate_event(
if TYPE_CHECKING: # type: ignore[unreachable]
assert_never(event.delta)
elif event.type == "content_block_stop":
if event.index >= len(current_snapshot.content):
return current_snapshot
content_block = current_snapshot.content[event.index]
if content_block.type == "text" and is_given(output_format):
content_block.parsed_output = parse_text(content_block.text, output_format)
Expand Down
4 changes: 4 additions & 0 deletions src/anthropic/lib/streaming/_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ def accumulate_event(
),
)
elif event.type == "content_block_delta":
if event.index >= len(current_snapshot.content):
return current_snapshot
content = current_snapshot.content[event.index]
if event.delta.type == "text_delta":
if content.type == "text":
Expand Down Expand Up @@ -497,6 +499,8 @@ def accumulate_event(
if TYPE_CHECKING: # type: ignore[unreachable]
assert_never(event.delta)
elif event.type == "content_block_stop":
if event.index >= len(current_snapshot.content):
return current_snapshot
content_block = current_snapshot.content[event.index]
if content_block.type == "text" and is_given(output_format):
content_block.parsed_output = parse_text(content_block.text, output_format)
Expand Down
28 changes: 17 additions & 11 deletions src/anthropic/lib/tools/_beta_builtin_memory_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def clear_all_memory(self) -> BetaFunctionToolResultType:


class BetaAsyncAbstractMemoryTool(BetaAsyncBuiltinFunctionTool):
"""Abstract base class for memory tool implementations.
"""Abstract base class for async memory tool implementations.

This class provides the interface for implementing a custom memory backend for Claude.

Expand All @@ -175,25 +175,31 @@ class BetaAsyncAbstractMemoryTool(BetaAsyncBuiltinFunctionTool):
Example usage:

```py
class MyMemoryTool(BetaAbstractMemoryTool):
def view(self, command: BetaMemoryTool20250818ViewCommand) -> BetaFunctionToolResultType:
import asyncio
from anthropic import AsyncAnthropic

class MyMemoryTool(BetaAsyncAbstractMemoryTool):
async def view(self, command: BetaMemoryTool20250818ViewCommand) -> BetaFunctionToolResultType:
...
return "view result"

def create(self, command: BetaMemoryTool20250818CreateCommand) -> BetaFunctionToolResultType:
async def create(self, command: BetaMemoryTool20250818CreateCommand) -> BetaFunctionToolResultType:
...
return "created successfully"

# ... implement other abstract methods


client = Anthropic()
memory_tool = MyMemoryTool()
message = client.beta.messages.run_tools(
model="claude-sonnet-4-5",
messages=[{"role": "user", "content": "Remember that I like coffee"}],
tools=[memory_tool],
).until_done()
async def main() -> None:
client = AsyncAnthropic()
memory_tool = MyMemoryTool()
message = await client.beta.messages.run_tools(
model="claude-sonnet-4-5",
messages=[{"role": "user", "content": "Remember that I like coffee"}],
tools=[memory_tool],
).until_done()

asyncio.run(main())
```
"""

Expand Down