diff --git a/anthropic.go b/anthropic.go index 79c248f..c361553 100644 --- a/anthropic.go +++ b/anthropic.go @@ -65,7 +65,17 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest { Data: msg.ImageBase64, })) } - msgs = append(msgs, m) + + // if this has the same role as the previous message, we can append it to the previous message + // as anthropic expects alternating assistant and user roles + + if len(msgs) > 0 && msgs[len(msgs)-1].Role == role { + m2 := &msgs[len(msgs)-1] + + m2.Content = append(m2.Content, m.Content...) + } else { + msgs = append(msgs, m) + } } }