fix(streaming): wrap from_json in try/except in non-beta message streaming#1563
Open
rmotgi1227 wants to merge 1 commit into
Open
fix(streaming): wrap from_json in try/except in non-beta message streaming#1563rmotgi1227 wants to merge 1 commit into
rmotgi1227 wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The beta streaming path (
_beta_messages.py) already wrapsfrom_jsonin atry/except ValueErrorthat surfaces a clear, actionable error when the model emits malformed partial JSON during tool use. The non-beta path (_messages.py) made the samefrom_jsoncall with no error handling, so malformed JSON propagated as a rawjiterinternal 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 thefrom_json(json_buf, partial_mode=True)call intry/except ValueErrorand re-raise with the raw JSON buffer and a retry suggestion, mirroring_beta_messages.pylines 500–510 exactly.Before:
After:
The error message wording is copied verbatim from the beta path so error handling is consistent across both streaming code paths.