Skip to content

fix: improve error message for malformed tool use JSON in streaming#1308

Open
IgnazioDS wants to merge 1 commit intoanthropics:mainfrom
IgnazioDS:fix/streaming-tool-use-error-message
Open

fix: improve error message for malformed tool use JSON in streaming#1308
IgnazioDS wants to merge 1 commit intoanthropics:mainfrom
IgnazioDS:fix/streaming-tool-use-error-message

Conversation

@IgnazioDS
Copy link
Copy Markdown

Summary

  • Add try-except error handling around from_json() in _messages.py to match the existing pattern in _beta_messages.py
  • When the model emits malformed JSON for tool use input during streaming, the error now includes the raw JSON content and guidance instead of a bare deserialization traceback

Problem

The non-beta streaming path (client.messages.stream()) crashes with an unhelpful error when the model produces invalid JSON for tool input. The beta path already handles this gracefully with a descriptive message. See #1265.

Test plan

  • Verify normal streaming tool use still works
  • Verify malformed JSON produces the improved error message
  • Verify behavior matches beta path

Fixes #1265

Mirror the try-except error handling from _beta_messages.py into
_messages.py so that malformed JSON in streaming tool use input
produces a helpful error message with the raw JSON content instead
of a bare deserialization traceback.

Fixes anthropics#1265
@IgnazioDS IgnazioDS requested a review from a team as a code owner March 28, 2026 12:54
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