From 244dc6e02f6ae6e289564ee74d5a87549ace037b Mon Sep 17 00:00:00 2001 From: Christian Sidak Date: Sun, 5 Apr 2026 20:59:20 -0700 Subject: [PATCH 1/2] Fix BetaAsyncAbstractMemoryTool docstring to use async examples The docstring was copy-pasted from the sync BetaAbstractMemoryTool without updating for async usage. This fixes: - Base class: BetaAbstractMemoryTool -> BetaAsyncAbstractMemoryTool - Method definitions: def -> async def - Client: Anthropic() -> AsyncAnthropic() - Wrap usage in async def main() / asyncio.run(main()) Fixes #1290 --- .../lib/tools/_beta_builtin_memory_tool.py | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/anthropic/lib/tools/_beta_builtin_memory_tool.py b/src/anthropic/lib/tools/_beta_builtin_memory_tool.py index edb948a2..be0a2e18 100644 --- a/src/anthropic/lib/tools/_beta_builtin_memory_tool.py +++ b/src/anthropic/lib/tools/_beta_builtin_memory_tool.py @@ -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(): + 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()) ``` """ From 28df4fed20310cb2fb661033cccb009521bc167f Mon Sep 17 00:00:00 2001 From: Christian Sidak Date: Sun, 5 Apr 2026 20:59:26 -0700 Subject: [PATCH 2/2] Fix unhelpful error message for malformed JSON in streaming tool use Wrap the from_json() call in the non-beta streaming path with the same try-except that already exists in the beta path, so users get a clear error message indicating the issue is malformed tool parameter JSON from the model, rather than a raw jiter parser error with no context. Fixes #1265 --- src/anthropic/lib/streaming/_messages.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/anthropic/lib/streaming/_messages.py b/src/anthropic/lib/streaming/_messages.py index b6b5f538..539eae2a 100644 --- a/src/anthropic/lib/streaming/_messages.py +++ b/src/anthropic/lib/streaming/_messages.py @@ -477,7 +477,12 @@ def accumulate_event( json_buf += bytes(event.delta.partial_json, "utf-8") if json_buf: - content.input = from_json(json_buf, partial_mode=True) + try: + content.input = from_json(json_buf, partial_mode=True) + except ValueError as e: + raise ValueError( + f"Unable to parse tool parameter JSON from model. Please retry your request or adjust your prompt. Error: {e}. JSON: {json_buf.decode('utf-8')}" + ) from e setattr(content, JSON_BUF_PROPERTY, json_buf) elif event.delta.type == "citations_delta":