Skip to content

Commit 17fe490

Browse files
committed
0.1.38 bugfix
1 parent 5ca9644 commit 17fe490

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

backend/providers/claude/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@
55
for use with the ChitChats multi-provider abstraction layer.
66
"""
77

8+
# Monkey-patch the SDK's message parser to handle rate_limit_event gracefully.
9+
# The SDK raises MessageParseError for unknown message types like rate_limit_event,
10+
# which kills the entire response stream. This patch converts them to SystemMessage instead.
11+
import claude_agent_sdk._internal.client as _cl # noqa: E402
12+
import claude_agent_sdk._internal.message_parser as _mp # noqa: E402
13+
from claude_agent_sdk.types import SystemMessage as _SystemMessage # noqa: E402
14+
15+
_original_parse_message = _mp.parse_message
16+
17+
18+
def _patched_parse_message(data):
19+
if isinstance(data, dict) and data.get("type") == "rate_limit_event":
20+
return _SystemMessage(subtype="rate_limit", data=data)
21+
return _original_parse_message(data)
22+
23+
24+
_mp.parse_message = _patched_parse_message
25+
_cl.parse_message = _patched_parse_message # type: ignore[attr-defined] # Patch the already-imported reference
26+
827
from .client import ClaudeClient
928
from .parser import ClaudeStreamParser
1029
from .provider import ClaudeClientPool, ClaudeProvider

backend/providers/claude/parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def parse_message(
8383

8484
# Extract session_id from SystemMessage
8585
if isinstance(message, SystemMessage):
86-
if isinstance(message.data, dict) and "session_id" in message.data:
86+
if message.subtype == "rate_limit":
87+
logger.warning(f"Rate limited by API (concurrent usage?): {message.data}")
88+
elif isinstance(message.data, dict) and "session_id" in message.data:
8789
new_session_id = message.data["session_id"]
8890
logger.debug(f"Extracted session_id: {new_session_id}")
8991

0 commit comments

Comments
 (0)