From 160dfd690051b67b668f8316ca441d96dfa156bc Mon Sep 17 00:00:00 2001 From: Scott Boudreaux <121303252+Scottcjn@users.noreply.github.com> Date: Thu, 26 Mar 2026 10:21:49 -0500 Subject: [PATCH] fix: add helpful error message for malformed tool JSON in non-beta streaming The beta path (_beta_messages.py) wraps from_json() with a try-except that provides an actionable error message including the raw JSON. The non-beta path (_messages.py) was missing this wrapper, causing raw ValueError with no context ("expected ident at line 1 column 11"). Fixes #1265 --- src/anthropic/lib/streaming/_messages.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/anthropic/lib/streaming/_messages.py b/src/anthropic/lib/streaming/_messages.py index b6b5f538..b09eaac0 100644 --- a/src/anthropic/lib/streaming/_messages.py +++ b/src/anthropic/lib/streaming/_messages.py @@ -477,7 +477,14 @@ 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. " + f"Please retry your request or adjust your prompt. " + f"Error: {e}. JSON: {json_buf.decode('utf-8')}" + ) from e setattr(content, JSON_BUF_PROPERTY, json_buf) elif event.delta.type == "citations_delta":