Skip to content

fix(streaming): add helpful error message for malformed JSON in non-beta path#1326

Open
Vitalcheffe wants to merge 2 commits intoanthropics:mainfrom
Vitalcheffe:fix/streaming-json-parse-error-message
Open

fix(streaming): add helpful error message for malformed JSON in non-beta path#1326
Vitalcheffe wants to merge 2 commits intoanthropics:mainfrom
Vitalcheffe:fix/streaming-json-parse-error-message

Conversation

@Vitalcheffe
Copy link
Copy Markdown

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 an input_json_delta event, the user gets a raw ValueError: expected ident at line 1 column 11 with 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

  1. _messages.py: Wrapped the from_json(json_buf, partial_mode=True) call in a try: ... except ValueError as e: block. On error, raises a new ValueError with a descriptive message including the original error and the JSON that failed to parse, chained via from e.
  2. test_non_beta_partial_json.py: Added three regression tests:
    • test_valid_json_parses_correctly — ensures valid JSON still works
    • test_invalid_json_raises_helpful_error — verifies the error message is user-friendly
    • test_invalid_json_chained_from_original — verifies error chaining for debugging

Testing

  • ruff check passes on all files
  • Tests match existing patterns from test_partial_json.py (beta path)

Checklist

  • Tests added
  • Linter passes
  • No breaking change (same error type, just better message)

Closes #1265

…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.
@Vitalcheffe Vitalcheffe requested a review from a team as a code owner April 2, 2026 11:16
@Vitalcheffe
Copy link
Copy Markdown
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
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.

Streaming tool use: unhelpful error message when model emits malformed JSON (non-beta path missing try-except)

1 participant