fix: add helpful error message for malformed tool JSON in non-beta streaming path#1293
Open
Krishnachaitanyakc wants to merge 1 commit intoanthropics:mainfrom
Open
Conversation
…reaming path The non-beta streaming accumulator (`_messages.py`) raises a raw `ValueError` from jiter when the model emits malformed JSON during an `input_json_delta` event (e.g. "expected ident at line 1 column 11"). This gives users no context about what went wrong or how to fix it. The beta path (`_beta_messages.py`) already wraps the same `from_json()` call with a try-except that produces a clear, actionable message including the original error and the raw JSON that failed to parse. This commit adds the same error handling to the non-beta path for parity, so users of `client.messages.stream()` get the same helpful error message as users of `client.beta.messages.stream()`. Fixes anthropics#1265
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
from_json()call in the non-beta streaming accumulator (_messages.py) with atry-except ValueErrorthat produces a clear, actionable error message — matching the existing behavior in the beta path (_beta_messages.py)Problem
When using
client.messages.stream()with tools, if the model emits malformed JSON during aninput_json_deltaevent, the user gets a raw internal parser error with no context:The beta path (
_beta_messages.py) already wraps the same call and produces a helpful message:The non-beta path was simply missing the same
try-except.Changes
src/anthropic/lib/streaming/_messages.py: Addedtry-except ValueErroraround thefrom_json(json_buf, partial_mode=True)call inaccumulate_event(), re-raising with a descriptive message that includes the original error and the raw JSON that failed to parseTest plan
tests/test_streaming.py)tests/lib/streaming/)Fixes #1265