Skip to content

fix(streaming): add helpful error message for malformed tool JSON in non-beta path#1268

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

fix(streaming): add helpful error message for malformed tool JSON in non-beta path#1268
tao-to-python wants to merge 1 commit intoanthropics:mainfrom
tao-to-python:fix/streaming-tool-json-error-message

Conversation

@tao-to-python
Copy link

Description

Fixes unhelpful raw parser error when model emits malformed JSON during streaming tool use, as reported in #1265.

Problem

The beta path (_beta_messages.py) already wraps from_json() with a helpful try-except:

try:
    content.input = from_json(json_buf, partial_mode=...)
except ValueError as e:
    raise ValueError(
        f"Unable to parse tool parameter JSON from model. "
        f"Please retry your request or adjust your prompt. "
        f"Error: {e}. JSON: {json_buf.decode('utf-8')}"
    ) from e

The non-beta path (_messages.py) was missing this wrapper, causing raw errors like:

ValueError: expected ident at line 1 column 11

No context about what JSON was received or what to do.

Solution

Add the same try-except wrapper to the non-beta accumulate_event() in _messages.py.

Changes

File Change
src/anthropic/lib/streaming/_messages.py Wrap from_json() with helpful ValueError

Fixes #1265

…non-beta path

The beta streaming path (_beta_messages.py) wraps from_json() with a
try-except that provides context about the error. The non-beta path
(_messages.py) was missing this, causing raw parser errors like:

  ValueError: expected ident at line 1 column 11

Add the same try-except wrapper to the non-beta accumulate_event()
so users get an actionable message with the malformed JSON included.

Fixes anthropics#1265
@tao-to-python tao-to-python requested a review from a team as a code owner March 20, 2026 02:21
@AbdoKnbGit
Copy link

already fix that #1266

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)

2 participants