Skip to content

fix(parser): raise MessageParseError on non-dict nested payloads (#1508) - #1556

Open
reginaldalfret wants to merge 1 commit into
anthropics:mainfrom
reginaldalfret:fix/message-parser-nested-payload-errors
Open

reginaldalfret wants to merge 1 commit into
anthropics:mainfrom
reginaldalfret:fix/message-parser-nested-payload-errors

Conversation

@reginaldalfret

Copy link
Copy Markdown

Fixes #1508.

Problem

parse_message previously raised raw TypeError exceptions (e.g. string indices must be integers, not 'str', 'NoneType' object is not subscriptable) instead of MessageParseError when nested payloads were present but not dictionaries:

  • user.message
  • assistant.message
  • result.deferred_tool_use
  • rate_limit_event.rate_limit_info

Because each branch only caught KeyError, subscripting non-dict objects escaped into caller code. Callers handling ClaudeSDKError / MessageParseError failed to catch these errors, resulting in unexpected stream termination without access to .data.

Solution

  1. Added explicit isinstance(..., dict) checks for nested payload structures (user.message, assistant.message, result.deferred_tool_use, rate_limit_event.rate_limit_info).
  2. Added except TypeError as e: handlers alongside except KeyError as e: across all message parsing branches.
  3. Preserved input payload in exc.data.
  4. Added 19 parameterized test cases in tests/test_message_parser.py testing strings, lists, integers, booleans, and None for each nested payload type.

Verification

  • pytest tests/test_message_parser.py: 112 passed in 1.12s.
  • ruff check: All checks passed.
  • mypy src/claude_agent_sdk/_internal/message_parser.py: 0 errors.

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