Python Agent SDK: MessageParseError on rate_limit_event from CLI 2.1.45+
Bug Description
The Python Agent SDK (0.1.38+) raises MessageParseError: Unknown message type: rate_limit_event when iterating async responses. This is because CLI 2.1.45 introduced SDKRateLimitEvent as an informational lifecycle message, but the SDK's message_parser.py was not updated to recognize it.
Reproduction
from claude_agent_sdk import query, ClaudeAgentOptions
options = ClaudeAgentOptions(model="claude-sonnet-4-6", allowed_tools=[], max_turns=1)
async for msg in query(prompt="Hello", options=options):
print(type(msg).__name__) # crashes with MessageParseError
Versions
claude-agent-sdk0.1.39 (latest on PyPI)- Bundled CLI: 2.1.49
- First appeared: SDK 0.1.38 / CLI 2.1.45
Root Cause
message_parser.py has a fixed set of recognized types (UserMessage, AssistantMessage, SystemMessage, ResultMessage, StreamEvent). The CLI now emits rate_limit_event JSON messages that the parser cannot handle.
SDK 0.1.37 (bundled CLI 2.1.44) works fine — the event didn't exist yet.
Impact
ask_json_nativewith structured output survives becauseToolUseBlockarrives before the crash- Plain text queries fail because the iterator crashes before all
TextBlockcontent is collected - Streaming crashes mid-stream
Suggested Fix
Either:
- Add
SDKRateLimitEventas a recognized message type with a corresponding dataclass (preferred — consumers can use the rate limit info) - Skip unknown message types with a warning log instead of raising
MessageParseError
Option 2 is more forward-compatible — future CLI message types won't break existing SDK versions.
Workaround
Wrap the async iterator to catch and skip unknown types:
async def _safe_stream(async_iter):
while True:
try:
msg = await async_iter.__anext__()
yield msg
except StopAsyncIteration:
break
except Exception as e:
if "Unknown message type" in str(e):
continue
raiseThis issue has 4 comments on GitHub. Read the full discussion on GitHub ↗