Skip to content

Conversation

@dicksontsai
Copy link
Collaborator

Summary

Fixes #505

The AssistantMessage.error field was never being populated because the message parser was reading from the wrong location in the JSON structure.

Problem

The CLI outputs error information at the top level of the JSON, not inside the message object:

{
  "type": "assistant",
  "message": { "content": [...], "model": "..." },
  "session_id": "...",
  "uuid": "...",
  "error": "unknown"  // <-- error is HERE at top level
}

The previous fix (PR #405) incorrectly read from data["message"].get("error") instead of data.get("error").

Solution

Changed the parser to read error from the correct location:

# Before (wrong)
error=data["message"].get("error"),

# After (correct)
error=data.get("error"),

Testing

  • Added 3 new test cases for error field parsing:
    • test_parse_assistant_message_with_error - general errors
    • test_parse_assistant_message_with_rate_limit_error - rate limit detection
    • test_parse_assistant_message_with_auth_error - authentication errors
  • All existing tests pass
  • Passed ruff linting
  • Passed mypy type checking

Usage Example

After this fix, applications can properly detect and handle errors:

async for message in client.receive_response():
    if isinstance(message, AssistantMessage):
        if message.error == "rate_limit":
            print("Rate limit hit! Implementing backoff...")
        elif message.error == "authentication_failed":
            print("Invalid API key")
        elif message.error:
            print(f"API error: {message.error}")

The error field in CLI output is at the top level of the JSON, not
inside the message object. The previous fix (PR #405) incorrectly
read from data["message"].get("error") instead of data.get("error").

CLI output structure shows error at top level:
```json
{
  "type": "assistant",
  "message": { ... },
  "error": "unknown"  // error is at top level
}
```

Fixes #505

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

[BUG] AssistantMessage error field not being properly populated

2 participants