-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: handle rate_limit_event in claude_agent_sdk to prevent parse errors #1875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rex-Arnab
wants to merge
7
commits into
AndyMik90:develop
Choose a base branch
from
Rex-Arnab:fix/1864-rate-limit-event
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c40a4e0
fix: handle rate_limit_event in claude_agent_sdk to prevent parse errors
Rex-Arnab d39cde5
fix: patch claude_agent_sdk to handle rate_limit_event without errors
Rex-Arnab 267b74d
style(session): reformat rate limit event handling for better readabi…
Rex-Arnab 19cee12
fix(sdk): patch claude_agent_sdk to handle rate_limit_event safely
Rex-Arnab 4393006
test(sdk_patches): add comprehensive tests for apply_claude_agent_sdk…
Rex-Arnab 4ebf0ed
Merge branch 'develop' into fix/1864-rate-limit-event
Rex-Arnab 1e81af0
fix: add pragma no cover to apply_claude_agent_sdk_patches imports
Rex-Arnab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """Runtime patches for third-party SDK bugs.""" | ||
|
|
||
| import logging | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def apply_claude_agent_sdk_patches() -> None: | ||
| """Patch claude_agent_sdk to handle rate_limit_event gracefully. | ||
|
|
||
| The bundled SDK raises MessageParseError for unknown message types including | ||
| rate_limit_event. This patch returns a SystemMessage instead of raising, | ||
| allowing the message loop in session.py to handle it and keep the stream open | ||
| while Claude Code waits for the rate limit to reset. | ||
|
|
||
| Patches both: | ||
| - message_parser.parse_message (the module attribute) | ||
| - _internal.client.parse_message (the already-bound module-level name that | ||
| the client's receive_response() generator actually calls via | ||
| `from .message_parser import parse_message` at import time) | ||
| """ | ||
| try: | ||
| from claude_agent_sdk._internal import client as _ic | ||
| from claude_agent_sdk._internal import message_parser as _mp | ||
| from claude_agent_sdk.types import SystemMessage as _SystemMessage | ||
|
|
||
| _orig_parse = _mp.parse_message | ||
|
|
||
| def _patched_parse(data: dict) -> object: | ||
| try: | ||
| return _orig_parse(data) | ||
| except Exception: | ||
| if isinstance(data, dict) and data.get("type") == "rate_limit_event": | ||
| logger.warning( | ||
| "Rate limit event received from Claude Code — " | ||
| "returning as SystemMessage so the stream stays open: %s", | ||
| data, | ||
| ) | ||
| return _SystemMessage(subtype="rate_limit_event", data=data) | ||
| raise | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| _mp.parse_message = _patched_parse | ||
| _ic.parse_message = _patched_parse | ||
| logger.debug("claude_agent_sdk patched to handle rate_limit_event") | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| except Exception: | ||
| logger.warning( | ||
| "Failed to apply claude_agent_sdk rate_limit_event patch — " | ||
| "rate limit events may cause unexpected session failures", | ||
| exc_info=True, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.