Skip to content

Fix unhelpful error message for malformed JSON in streaming tool use#1335

Open
Christian-Sidak wants to merge 2 commits intoanthropics:mainfrom
Christian-Sidak:fix/async-memory-tool-docstring
Open

Fix unhelpful error message for malformed JSON in streaming tool use#1335
Christian-Sidak wants to merge 2 commits intoanthropics:mainfrom
Christian-Sidak:fix/async-memory-tool-docstring

Conversation

@Christian-Sidak
Copy link
Copy Markdown

Summary

  • Wraps the bare from_json() call in _messages.py with the same try-except ValueError that already exists in _beta_messages.py, so users get a clear, actionable error message when the model emits malformed tool parameter JSON during streaming

Problem

When using client.messages.stream() with tools, if the model emits malformed JSON in an input_json_delta event, users see a raw parser error like ValueError: expected ident at line 1 column 11 with no context about what went wrong or what to do.

The beta path (_beta_messages.py) already handles this with a helpful message. The non-beta path (_messages.py) was missing the same handling.

Fix

6-line change: wrap the from_json call at line 480 of _messages.py with the same try-except pattern from _beta_messages.py. Zero behavior change for valid JSON.

Fixes #1265

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 anthropics#1290
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 anthropics#1265
@Christian-Sidak Christian-Sidak requested a review from a team as a code owner April 6, 2026 03:59
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.

Streaming tool use: unhelpful error message when model emits malformed JSON (non-beta path missing try-except)

1 participant