Skip to content

fix: add missing JSON error handling in non-beta streaming accumulator#1266

Open
AbdoKnbGit wants to merge 1 commit intoanthropics:mainfrom
AbdoKnbGit:fix/non-beta-streaming-json-error-handling
Open

fix: add missing JSON error handling in non-beta streaming accumulator#1266
AbdoKnbGit wants to merge 1 commit intoanthropics:mainfrom
AbdoKnbGit:fix/non-beta-streaming-json-error-handling

Conversation

@AbdoKnbGit
Copy link

Fixes #1265

The beta streaming accumulator (_beta_messages.py) wraps from_json() in a
try-except that gives users a clear, actionable error when the model emits
malformed tool input JSON. The non-beta accumulator (_messages.py) was missing
the same handler — causing users to see raw jiter errors with no context.

This PR adds the matching try-except to the non-beta path and a test mirroring
the existing beta test.


Changes

  • src/anthropic/lib/streaming/_messages.py — wrap from_json() at L480
    in a try-except with a helpful error message, matching _beta_messages.py L500-510
  • tests/lib/streaming/test_partial_json.py — add
    test_partial_json_with_invalid_json_non_beta, mirroring the existing beta test

Before / After

Before — non-beta path, malformed tool JSON:

ValueError: expected ident at line 1 column 11

No context. No mention of tool JSON. Looks like an SDK crash.

After — matches beta behavior:

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}

Test plan

  • New test test_partial_json_with_invalid_json_non_beta passes
  • All 26 streaming tests pass (25 existing + 1 new)
  • Confirmed with real API call against claude-haiku-4-5
  • Zero behavior change for valid JSON

@AbdoKnbGit AbdoKnbGit requested a review from a team as a code owner March 20, 2026 00:36
@AbdoKnbGit
Copy link
Author

AbdoKnbGit commented Mar 20, 2026

@claude @karpetrosyan hi can you review this

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