@@ -666,11 +666,17 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
666666 },
667667 }), nil
668668 }
669-
670669 stream , err := c .c .CreateChatCompletionStream (ctx , request , headers , retryOpts ... )
671670 if err != nil {
672671 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
674680 }
675681 return types.CompletionMessage {}, err
676682 }
@@ -683,6 +689,11 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
683689 for {
684690 response , err := stream .Recv ()
685691 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+ }
686697 // If the stream is finished, either because we got an EOF or the context was canceled,
687698 // then we're done. The cache won't save the response if the context was canceled.
688699 return partialMessage , c .cache .Store (ctx , c .cacheKey (request ), partialMessage )
0 commit comments