Skip to content

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

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

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

Conversation

@Krishnachaitanyakc
Copy link
Copy Markdown

Summary

  • Wraps the bare from_json() call in the non-beta streaming accumulator (_messages.py) with a try-except ValueError that produces a clear, actionable error message — matching the existing behavior in the beta path (_beta_messages.py)

Problem

When using client.messages.stream() with tools, if the model emits malformed JSON during an input_json_delta event, the user gets a raw internal parser error with no context:

ValueError: expected ident at line 1 column 11

The beta path (_beta_messages.py) already wraps the same call and produces a helpful message:

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: {"city": INVALID_VALUE}

The non-beta path was simply missing the same try-except.

Changes

  • src/anthropic/lib/streaming/_messages.py: Added try-except ValueError around the from_json(json_buf, partial_mode=True) call in accumulate_event(), re-raising with a descriptive message that includes the original error and the raw JSON that failed to parse

Test plan

  • All 21 existing streaming tests pass (tests/test_streaming.py)
  • All 25 streaming lib tests pass (tests/lib/streaming/)
  • Zero behavior change for valid JSON — the try-except only activates on malformed input
  • Verified the error message format matches the beta path exactly

Fixes #1265

…reaming path

The non-beta streaming accumulator (`_messages.py`) raises a raw
`ValueError` from jiter when the model emits malformed JSON during an
`input_json_delta` event (e.g. "expected ident at line 1 column 11").
This gives users no context about what went wrong or how to fix it.

The beta path (`_beta_messages.py`) already wraps the same `from_json()`
call with a try-except that produces a clear, actionable message
including the original error and the raw JSON that failed to parse.

This commit adds the same error handling to the non-beta path for
parity, so users of `client.messages.stream()` get the same helpful
error message as users of `client.beta.messages.stream()`.

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