Skip to content

[BUG] AssistantMessage error field not being properly populated #505

@ben-complement

Description

@ben-complement

Description

The AssistantMessage.error field is never populated when the Claude CLI returns error responses (e.g., authentication failures, rate limits, API errors). This makes it impossible for SDK users to programmatically detect and handle error conditions.

Root Cause

In message_parser.py, the error field is being read from the wrong location in the JSON structure:

# Wrong - looks inside message object                                                                                                                                                                                                                                              
error=data["message"].get("error")                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                   
# Correct - error is at the top level                                                                                                                                                                                                                                              
error=data.get("error")                                                                                                                                                                                                                                                            

CLI Output Examples

Invalid model name (error: "unknown"):

claude --output-format stream-json -p "hi" --print --verbose --model "invalid-model-name" | grep assistant | jq .                                                                                                                                                                  
{                                                                                                                                                                                                                                                                                  
  "type": "assistant",                                                                                                                                                                                                                                                             
  "message": {                                                                                                                                                                                                                                                                     
    "id": "ad53c326-dd50-4203-a91b-e963212193c4",                                                                                                                                                                                                                                  
    "container": null,                                                                                                                                                                                                                                                             
    "model": "<synthetic>",                                                                                                                                                                                                                                                        
    "role": "assistant",                                                                                                                                                                                                                                                           
    "stop_reason": "stop_sequence",                                                                                                                                                                                                                                                
    "stop_sequence": "",                                                                                                                                                                                                                                                           
    "type": "message",                                                                                                                                                                                                                                                             
    "usage": {                                                                                                                                                                                                                                                                     
      "input_tokens": 0,                                                                                                                                                                                                                                                           
      "output_tokens": 0,                                                                                                                                                                                                                                                          
      "cache_creation_input_tokens": 0,                                                                                                                                                                                                                                            
      "cache_read_input_tokens": 0,                                                                                                                                                                                                                                                
      "server_tool_use": {                                                                                                                                                                                                                                                         
        "web_search_requests": 0,                                                                                                                                                                                                                                                  
        "web_fetch_requests": 0                                                                                                                                                                                                                                                    
      },                                                                                                                                                                                                                                                                           
      "service_tier": null,                                                                                                                                                                                                                                                        
      "cache_creation": {                                                                                                                                                                                                                                                          
        "ephemeral_1h_input_tokens": 0,                                                                                                                                                                                                                                            
        "ephemeral_5m_input_tokens": 0                                                                                                                                                                                                                                             
      }                                                                                                                                                                                                                                                                            
    },                                                                                                                                                                                                                                                                             
    "content": [                                                                                                                                                                                                                                                                   
      {                                                                                                                                                                                                                                                                            
        "type": "text",                                                                                                                                                                                                                                                            
        "text": "API Error: 404 {\"type\":\"error\",\"error\":{\"type\":\"not_found_error\",\"message\":\"model: invalid-model-name\"},\"request_id\":\"req_011CXPSzXgANnbQwoKHgQthD\"}"                                                                                           
      }                                                                                                                                                                                                                                                                            
    ],                                                                                                                                                                                                                                                                             
    "context_management": null                                                                                                                                                                                                                                                     
  },                                                                                                                                                                                                                                                                               
  "parent_tool_use_id": null,                                                                                                                                                                                                                                                      
  "session_id": "92cb3340-de00-45d6-84c3-d5a2010a32e2",                                                                                                                                                                                                                            
  "uuid": "4ddbcd7b-ed40-4d48-adb8-b281cf3769b6",                                                                                                                                                                                                                                  
  "error": "unknown"                                                                                                                                                                                                                                                               
}                                                                                                                                                                                                                                                                                  

Invalid API key (error: "authentication_failed"):

ANTHROPIC_API_KEY="invalid-key" claude --output-format stream-json -p "hi" --print --verbose | grep assistant | jq .                                                                                                                                                               
{                                                                                                                                                                                                                                                                                  
  "type": "assistant",                                                                                                                                                                                                                                                             
  "message": {                                                                                                                                                                                                                                                                     
    "id": "1c65e36b-5819-408a-ac23-7ce20b8cadeb",                                                                                                                                                                                                                                  
    "container": null,                                                                                                                                                                                                                                                             
    "model": "<synthetic>",                                                                                                                                                                                                                                                        
    "role": "assistant",                                                                                                                                                                                                                                                           
    "stop_reason": "stop_sequence",                                                                                                                                                                                                                                                
    "stop_sequence": "",                                                                                                                                                                                                                                                           
    "type": "message",                                                                                                                                                                                                                                                             
    "usage": {                                                                                                                                                                                                                                                                     
      "input_tokens": 0,                                                                                                                                                                                                                                                           
      "output_tokens": 0,                                                                                                                                                                                                                                                          
      "cache_creation_input_tokens": 0,                                                                                                                                                                                                                                            
      "cache_read_input_tokens": 0,                                                                                                                                                                                                                                                
      "server_tool_use": {                                                                                                                                                                                                                                                         
        "web_search_requests": 0,                                                                                                                                                                                                                                                  
        "web_fetch_requests": 0                                                                                                                                                                                                                                                    
      },                                                                                                                                                                                                                                                                           
      "service_tier": null,                                                                                                                                                                                                                                                        
      "cache_creation": {                                                                                                                                                                                                                                                          
        "ephemeral_1h_input_tokens": 0,                                                                                                                                                                                                                                            
        "ephemeral_5m_input_tokens": 0                                                                                                                                                                                                                                             
      }                                                                                                                                                                                                                                                                            
    },                                                                                                                                                                                                                                                                             
    "content": [                                                                                                                                                                                                                                                                   
      {                                                                                                                                                                                                                                                                            
        "type": "text",                                                                                                                                                                                                                                                            
        "text": "Invalid API key · Fix external API key"                                                                                                                                                                                                                           
      }                                                                                                                                                                                                                                                                            
    ],                                                                                                                                                                                                                                                                             
    "context_management": null                                                                                                                                                                                                                                                     
  },                                                                                                                                                                                                                                                                               
  "parent_tool_use_id": null,                                                                                                                                                                                                                                                      
  "session_id": "9e89813f-cb7b-41fb-8a38-135f2707d675",                                                                                                                                                                                                                            
  "uuid": "9ade6dfc-fbbb-4d7e-9a21-515d3297463b",                                                                                                                                                                                                                                  
  "error": "authentication_failed"                                                                                                                                                                                                                                                 
}                                                                                                                                                                                                                                                                                  

As shown above, the error field is at the top level of the JSON object, not nested inside the message object.

Fix

Change message_parser.py to read the error field from the correct location:

error=data.get("error")                                                                                                                                                                                                                                                            

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions