Skip to content

fix: raise MessageParseError for non-dict nested payloads - #1571

Open
doge-th wants to merge 1 commit into
anthropics:mainfrom
doge-th:fix/parse-message-nested-typeerror
Open

doge-th wants to merge 1 commit into
anthropics:mainfrom
doge-th:fix/parse-message-nested-typeerror

Conversation

@doge-th

@doge-th doge-th commented Nov 22, 2026

Copy link
Copy Markdown

Summary

Fixes #1508.

parse_message's user, assistant, result and rate_limit_event branches catch only KeyError, but a present-yet-non-dict nested payload (message, deferred_tool_use, rate_limit_info) fails at subscripting with TypeError first — so the raw exception escaped the documented MessageParseError contract (docstring :60-61) and callers catching ClaudeSDKError missed it, killing the message stream without a .data payload.

Each of the four branches gets its own except TypeError handler raising MessageParseError("Invalid nested payload in ...", data), following the #1058 precedent for the sibling content-shape case. Missing-field handling is untouched.

Test plan

  • test_non_dict_nested_payloads_raise_message_parse_error: 9 malformed frames (string/list/None message, truthy non-dict deferred_tool_use, string/None rate_limit_info) all raise MessageParseError with "Invalid nested payload", catchable as ClaudeSDKError and carrying the frame in .data.
  • Verified the test fails on unpatched main (raw TypeError) and passes with the fix.
  • pytest tests/: 1756 passed, 6 skipped. ruff check, mypy src/ scripts/ clean.

parse_message's user, assistant, result and rate_limit_event branches
caught only KeyError, but a present-yet-non-dict nested payload
(message, deferred_tool_use, rate_limit_info) fails at subscripting
with TypeError first, so the raw exception escaped the documented
MessageParseError contract and callers catching ClaudeSDKError missed
it.

Add a TypeError handler to each branch that raises MessageParseError
with the offending frame in .data, following the anthropics#1058 precedent for
the sibling content-shape case.

Fixes anthropics#1508

This branch has not been deployed

No deployments
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.

parse_message raises raw TypeError instead of MessageParseError on non-dict nested payloads

1 participant