Skip to content

Commit

Permalink
anthropic: Add improved handling of other error event types
Browse files Browse the repository at this point in the history
  • Loading branch information
tmc committed Sep 12, 2024
1 parent 1975058 commit 4335df9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions llms/anthropic/anthropicllm.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func generateMessagesContent(ctx context.Context, o *LLM, messages []llms.Messag
}
return nil, fmt.Errorf("anthropic: failed to create message: %w", err)
}
if result == nil {
return nil, ErrEmptyResponse
}

choices := make([]*llms.ContentChoice, len(result.Content))
for i, content := range result.Content {
Expand Down
6 changes: 4 additions & 2 deletions llms/anthropic/internal/anthropicclient/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (m *MessageResponsePayload) UnmarshalJSON(data []byte) error {
}
m.Content = append(m.Content, tuc)
default:
return fmt.Errorf("unknown content type: %s", typeStruct.Type)
return fmt.Errorf("unknown content type: %s\n%v", typeStruct.Type, string(raw))
}
}

Expand Down Expand Up @@ -268,8 +268,10 @@ func processStreamEvent(ctx context.Context, event map[string]interface{}, paylo
eventChan <- MessageEvent{Response: &response, Err: nil}
case "ping":
// Nothing to do here
case "error":
eventChan <- MessageEvent{Response: nil, Err: fmt.Errorf("received error event: %v", event)}
default:
log.Printf("unknown event type: %s", eventType)
log.Printf("unknown event type: %s - %v", eventType, event)
}
return response, nil
}
Expand Down

0 comments on commit 4335df9

Please sign in to comment.