@@ -666,11 +666,17 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
666
666
},
667
667
}), nil
668
668
}
669
-
670
669
stream , err := c .c .CreateChatCompletionStream (ctx , request , headers , retryOpts ... )
671
670
if err != nil {
672
671
if errors .Is (err , context .Canceled ) {
673
- err = nil
672
+ return types.CompletionMessage {
673
+ Content : []types.ContentPart {
674
+ {
675
+ Text : "User aborted the chat before model could respond" ,
676
+ },
677
+ },
678
+ Role : types .CompletionMessageRoleTypeAssistant ,
679
+ }, nil
674
680
}
675
681
return types.CompletionMessage {}, err
676
682
}
@@ -683,6 +689,11 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
683
689
for {
684
690
response , err := stream .Recv ()
685
691
if errors .Is (err , io .EOF ) || errors .Is (err , context .Canceled ) {
692
+ if len (partialMessage .Content ) > 0 && partialMessage .Content [0 ].Text == "" {
693
+ // Place a text holder if LLM doesn't respond or user cancel the stream before it can produce any response.
694
+ // In anthropic models it will yield an error about non-empty message for assistant message
695
+ partialMessage .Content [0 ].Text = "User aborted the chat or chat finished before LLM can respond"
696
+ }
686
697
// If the stream is finished, either because we got an EOF or the context was canceled,
687
698
// then we're done. The cache won't save the response if the context was canceled.
688
699
return partialMessage , c .cache .Store (ctx , c .cacheKey (request ), partialMessage )
0 commit comments