Skip to content

fix(streaming): wrap from_json in try/except in non-beta message streaming#1563

Open
rmotgi1227 wants to merge 1 commit into
anthropics:mainfrom
rmotgi1227:fix/streaming-tool-json-parse-error
Open

fix(streaming): wrap from_json in try/except in non-beta message streaming#1563
rmotgi1227 wants to merge 1 commit into
anthropics:mainfrom
rmotgi1227:fix/streaming-tool-json-parse-error

Conversation

@rmotgi1227
Copy link
Copy Markdown

Summary

The beta streaming path (_beta_messages.py) already wraps from_json in a try/except ValueError that surfaces a clear, actionable error when the model emits malformed partial JSON during tool use. The non-beta path (_messages.py) made the same from_json call with no error handling, so malformed JSON propagated as a raw jiter internal error (ValueError: expected ident at line 1 column 11) with no context about what went wrong or what to do.

Change

src/anthropic/lib/streaming/_messages.py — wrap the from_json(json_buf, partial_mode=True) call in try/except ValueError and re-raise with the raw JSON buffer and a retry suggestion, mirroring _beta_messages.py lines 500–510 exactly.

Before:

if json_buf:
    content.input = from_json(json_buf, partial_mode=True)

After:

if json_buf:
    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

The error message wording is copied verbatim from the beta path so error handling is consistent across both streaming code paths.

…aming

The beta streaming path (_beta_messages.py) already wraps from_json in a
try/except ValueError that surfaces a clear, actionable error message when
the model emits malformed partial JSON during tool use. The non-beta path
(_messages.py) made the same from_json call with no error handling, so
malformed JSON propagated as a raw jiter internal error with no context.

Mirror the beta pattern: catch ValueError and re-raise with the raw JSON
buffer and a retry suggestion included in the message.
@rmotgi1227 rmotgi1227 requested a review from a team as a code owner May 18, 2026 21:11
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.

1 participant