fix: handle rate_limit_event message type#589
fix: handle rate_limit_event message type#589dotuananh0712 wants to merge 1 commit intoanthropics:mainfrom
Conversation
Add RateLimitEvent dataclass and parser support for rate_limit_event messages from Claude Code CLI. This prevents MessageParseError from crashing the receive_messages() generator when rate limit events occur. The fix adds: 1. RateLimitEvent type to types.py 2. Handler for 'rate_limit_event' case in message_parser.py Fixes anthropics#583
|
Great work on implementing Part 1 of the fix! 🙌 Adding the RateLimitEvent dataclass and parser case looks solid. 💡 Suggestion: Add Forward Compatibility (Part 2)I noticed this PR addresses Part 1 (adding rate_limit_event support), but the issue also suggested Part 2: making unknown message types non-fatal for forward compatibility. Why This MattersCurrently, the code still has the strict allowlist that crashes on unknown types. This means any future unknown message type from the CLI will still crash the SDK. The issue reporter emphasized this is particularly severe because it makes the SDK fragile against CLI updates. Suggested AdditionConsider adding error handling in client.py receive_messages() to catch MessageParseError and skip unknown types with a warning log. This would handle all future unknown message types gracefully and prevent crashes from CLI updates. Happy to submit a PR for this part if you'd like, or feel free to add it yourself! |
|
Hi @gspeter-max I've created a follow-up PR (#593) that adds forward compatibility by catching MessageParseError in the message loop and logging a warning instead of crashing. This way the SDK will gracefully handle any future unknown message types from the CLI without requiring code changes |
|
@dotuananh0712 okay, now I think it ready for merge and solve the issue , wait for maintainers response |
Summary
Add support for
rate_limit_eventmessages from Claude Code CLI. This preventsMessageParseErrorfrom crashing thereceive_messages()generator when rate limit events occur.Changes
types.py: Added
RateLimitEventdataclass with fields:type: The message typesession_id: Session identifierlimit_type: Type of limit (e.g., "turns", "tokens")limit: The limit valueremaining: Remaining quotareset_at: When the rate limit resetsmessage_parser.py: Added handler for
rate_limit_eventcase in the match statement, parsing the event data into aRateLimitEventobject.Problem
The CLI emits
rate_limit_eventmessages which weren't recognized by the SDK's message parser. This causedMessageParseError("Unknown message type: rate_limit_event")to be raised inside the async generator, terminating it entirely.Fixes
Fixes #583
This PR was contributed by an AI assistant (OpenCode).