fix(streaming): add helpful error message for malformed JSON in non-beta path#1326
Open
Vitalcheffe wants to merge 2 commits intoanthropics:mainfrom
Open
fix(streaming): add helpful error message for malformed JSON in non-beta path#1326Vitalcheffe wants to merge 2 commits intoanthropics:mainfrom
Vitalcheffe wants to merge 2 commits intoanthropics:mainfrom
Conversation
…eta path Closes anthropics#1265 - Wrap from_json() call in try-except in _messages.py accumulate_event - Error message now mirrors the beta path: includes the original error and the JSON that failed to parse - Added regression tests for valid JSON, invalid JSON, and error chaining Impact: Users of client.messages.stream() with tools now get an actionable error message instead of a raw jiter ValueError when the model emits malformed JSON during input_json_delta events.
Author
|
I’m aware _messages.py may be auto-generated via Stainless. If that’s the case, I’m happy to discuss where the fix should live upstream, or whether there’s a manual override mechanism for exception handling in the generator config. Open to guidance on the preferred approach. |
…beta path - Adds try-except around from_json() in _messages.py accumulate_event - Error message matches beta path format exactly - Covers both sync and async streaming paths - Regression tests added for valid, invalid, and chained error cases
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.
What
Wraps the
from_json()call in the non-beta streaming path with a try-except, providing the same helpful error message that already exists in the beta path.Why
When using
client.messages.stream()with tools, if the model emits malformed JSON during aninput_json_deltaevent, the user gets a rawValueError: expected ident at line 1 column 11with zero context. The beta path (_beta_messages.py) already handles this gracefully with a clear message. The non-beta path (_messages.py) was simply missing the same error handling.See: #1265
How
_messages.py: Wrapped thefrom_json(json_buf, partial_mode=True)call in atry: ... except ValueError as e:block. On error, raises a newValueErrorwith a descriptive message including the original error and the JSON that failed to parse, chained viafrom e.test_non_beta_partial_json.py: Added three regression tests:test_valid_json_parses_correctly— ensures valid JSON still workstest_invalid_json_raises_helpful_error— verifies the error message is user-friendlytest_invalid_json_chained_from_original— verifies error chaining for debuggingTesting
ruff checkpasses on all filestest_partial_json.py(beta path)Checklist
Closes #1265