Skip to content

fix: add helpful error message for malformed tool JSON in non-beta streaming#1300

Open
Scottcjn wants to merge 1 commit intoanthropics:mainfrom
Scottcjn:fix/streaming-json-error-message
Open

fix: add helpful error message for malformed tool JSON in non-beta streaming#1300
Scottcjn wants to merge 1 commit intoanthropics:mainfrom
Scottcjn:fix/streaming-json-error-message

Conversation

@Scottcjn
Copy link
Copy Markdown

Summary

The beta streaming path (_beta_messages.py) wraps from_json() with a try-except that provides an actionable error message when the model emits malformed JSON during input_json_delta events. The non-beta path (_messages.py) was missing this wrapper.

Before: Users see a raw ValueError with no context:

ValueError: expected ident at line 1 column 11

After: Users see a clear message with the raw JSON for debugging:

ValueError: Unable to parse tool parameter JSON from model. Please retry your request or adjust your prompt. Error: expected ident at line 1 column 11. JSON: {"name": ...}

Changes

  • src/anthropic/lib/streaming/_messages.py: Added try-except around from_json() matching the beta path pattern

Test plan

  • Verified non-beta path now raises ValueError with context message
  • Verified existing streaming tests still pass
  • Matches exact pattern from _beta_messages.py line 508-514

Fixes #1265

…reaming

The beta path (_beta_messages.py) wraps from_json() with a try-except
that provides an actionable error message including the raw JSON.
The non-beta path (_messages.py) was missing this wrapper, causing
raw ValueError with no context ("expected ident at line 1 column 11").

Fixes anthropics#1265
@Scottcjn Scottcjn requested a review from a team as a code owner March 26, 2026 15:22
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