From f8900e7fe5aa9ced7214cabec0c2323e28f62e3d Mon Sep 17 00:00:00 2001 From: "2285166171@qq.com" <2285166171@qq.com> Date: Fri, 20 Mar 2026 10:21:17 +0800 Subject: [PATCH] fix(streaming): add helpful error message for malformed tool JSON in 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 #1265 --- src/anthropic/lib/streaming/_messages.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/anthropic/lib/streaming/_messages.py b/src/anthropic/lib/streaming/_messages.py index b6b5f538..539eae2a 100644 --- a/src/anthropic/lib/streaming/_messages.py +++ b/src/anthropic/lib/streaming/_messages.py @@ -477,7 +477,12 @@ def accumulate_event( json_buf += bytes(event.delta.partial_json, "utf-8") if json_buf: - content.input = from_json(json_buf, partial_mode=True) + try: + content.input = from_json(json_buf, partial_mode=True) + except ValueError as e: + raise ValueError( + f"Unable to parse tool parameter JSON from model. Please retry your request or adjust your prompt. Error: {e}. JSON: {json_buf.decode('utf-8')}" + ) from e setattr(content, JSON_BUF_PROPERTY, json_buf) elif event.delta.type == "citations_delta":