Skip to content

Raise MessageParseError (not TypeError) on malformed field types#988

Closed
Zeffut wants to merge 1 commit into
anthropics:mainfrom
Zeffut:fix/message-parser-rate-limit-typeerror
Closed

Raise MessageParseError (not TypeError) on malformed field types#988
Zeffut wants to merge 1 commit into
anthropics:mainfrom
Zeffut:fix/message-parser-rate-limit-typeerror

Conversation

@Zeffut
Copy link
Copy Markdown

@Zeffut Zeffut commented May 23, 2026

Summary

parse_message in _internal/message_parser.py documents that "malformed input" should raise MessageParseError, but the six case branches only catch KeyError. A payload with a field of the wrong type (e.g. rate_limit_event with rate_limit_info=None, or any user/assistant/system/result/stream_event message where a sub-field is missing structure) leaks a bare TypeError. Because the parser is called inside the consumer's read loop, that escapes the loop and silently drops every subsequent message on the stream.

Change

  • Added a sibling except TypeError to each of the six case branches, raising MessageParseError("Malformed field in <type> message: ...", data=payload).
  • Kept the existing KeyError → "Missing required field..." messages verbatim so existing test assertions remain valid.
  • Added three regression tests in tests/test_message_parser.py covering rate_limit_info ∈ {None, str, int} and user/assistant message=None.

Verification

  • pytest tests/test_message_parser.py: 57 pass (54 prior + 3 new)
  • pytest tests/: 753 pass, 3 skipped
  • ruff check, ruff format --check, mypy src/: clean

…oads

parse_message documents that malformed messages raise MessageParseError, but
each case-branch's try/except only catches KeyError. When a field is present
but of the wrong type (e.g. rate_limit_event with rate_limit_info=None, or
user/assistant with message=None), the subsequent indexing raises TypeError,
which escapes the parser and crashes the read loop -- losing every subsequent
message in the stream.

Add a TypeError clause alongside the existing KeyError clause in the six case
branches that index sub-fields. Existing "Missing required field..." wording
is preserved for KeyError (backward-compatible with existing tests); the new
clause emits "Malformed field..." through the same MessageParseError type and
carries the original payload.

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 23, 2026 21:32
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR hardens message parsing so malformed/legacy CLI payloads raise MessageParseError (with original data attached) instead of leaking TypeError and potentially crashing the parser loop.

Changes:

  • Added regression tests for malformed rate_limit_event payloads and non-dict message fields for user/assistant.
  • Updated parse_message to catch TypeError in multiple message-type branches and re-raise as MessageParseError.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/test_message_parser.py Adds test coverage to ensure malformed payloads raise MessageParseError rather than TypeError.
src/claude_agent_sdk/_internal/message_parser.py Converts TypeError during parsing into MessageParseError across supported message types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +253 to +256
except TypeError as e:
raise MessageParseError(
f"Malformed field in system message: {e}", data
) from e
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point on test coverage parity. The three added regression tests target the originally reported rate_limit_event regression plus the two most commonly malformed branches (user/assistant, since their message field is what CLI payloads vary on most). The system/result/stream_event branches are wrapped symmetrically for defense-in-depth (the same TypeError -> MessageParseError shape, with original data attached), so behaviorally they are covered by the same contract the existing tests assert. Happy to extend coverage to those branches too if the maintainers prefer one test per branch — the 753-pass suite stays green either way.

Comment on lines +125 to +128
except TypeError as e:
raise MessageParseError(
f"Malformed field in user message: {e}", data
) from e
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair concern on TypeError breadth. The scope here is intentionally narrow: each except TypeError sits inside the same small try block that already catches KeyError for the same parsing step, so it only wraps the dict subscripting / unpacking of CLI payload fields — not arbitrary downstream logic. This mirrors the existing KeyError -> MessageParseError pattern in the file (symmetric handling for the two ways a malformed payload can fail). Pre-validating shapes per branch would work too, but it would expand the diff considerably and duplicate checks the dataclass constructors already perform. Re-raising as MessageParseError with the original data preserves both the original TypeError (via 'from e') and the malformed payload for debugging, so genuine programmer errors remain inspectable in tracebacks.

@Zeffut
Copy link
Copy Markdown
Author

Zeffut commented May 24, 2026

Closing — this PR was opened as part of an automated triage experiment and does not reflect a real user-reported issue I personally validated. Apologies for the noise.

@Zeffut Zeffut closed this May 24, 2026
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.

2 participants