Skip to content

fix: add try/except for tool JSON parsing in non-beta streaming path#1292

Open
mmporong wants to merge 1 commit intoanthropics:mainfrom
mmporong:fix/streaming-tool-json-error-handling
Open

fix: add try/except for tool JSON parsing in non-beta streaming path#1292
mmporong wants to merge 1 commit intoanthropics:mainfrom
mmporong:fix/streaming-tool-json-error-handling

Conversation

@mmporong
Copy link
Copy Markdown

Summary

The beta streaming path (_beta_messages.py) already wraps from_json() in a try/except that provides a helpful error message including the raw JSON bytes. The non-beta path (_messages.py) was missing this protection, causing a bare ValueError with no context when the model emits malformed partial JSON during tool use streaming.

This PR ports the same error handling from the beta path to the non-beta path.

Before

ValueError: invalid JSON

After

ValueError: Unable to parse tool parameter JSON from model. Please retry your request or adjust your prompt. Error: invalid JSON. JSON: {"name": "get_weather", "input": {"loc

Changes

  • src/anthropic/lib/streaming/_messages.py (line 480): wrap from_json() call in try/except ValueError, matching the existing pattern in _beta_messages.py (line 500)

Fixes #1265

The beta streaming path (_beta_messages.py) already wraps from_json()
in a try/except that provides a helpful error message including the
raw JSON bytes. The non-beta path (_messages.py) was missing this
protection, causing a bare ValueError with no context when the model
emits malformed partial JSON during tool use streaming.

Fixes anthropics#1265
@mmporong mmporong requested a review from a team as a code owner March 25, 2026 02:06
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