Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions go/adk/pkg/models/anthropic_adk.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ func genaiContentsToAnthropicMessages(contents []*genai.Content, config *genai.G
Content: toolResultBlocks,
})
} else {
// Regular user message
var contentBlocks []anthropic.ContentBlockParamUnion

// Add images first
Expand All @@ -272,9 +271,13 @@ func genaiContentsToAnthropicMessages(contents []*genai.Content, config *genai.G
contentBlocks = append(contentBlocks, anthropic.NewTextBlock(strings.Join(textParts, "\n")))
}

msgRole := anthropic.MessageParamRoleUser
if role == "model" || role == "assistant" {
msgRole = anthropic.MessageParamRoleAssistant
}
if len(contentBlocks) > 0 {
messages = append(messages, anthropic.MessageParam{
Role: anthropic.MessageParamRoleUser,
Role: msgRole,
Content: contentBlocks,
})
}
Expand Down
17 changes: 17 additions & 0 deletions go/adk/pkg/models/anthropic_adk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ func TestAnthropicUsageToGenai(t *testing.T) {
}
}

func TestGenaiContentsToAnthropicMessagesKeepsAssistantTextRole(t *testing.T) {
contents := []*genai.Content{
{Role: "user", Parts: []*genai.Part{{Text: "hi"}}},
{Role: "model", Parts: []*genai.Part{{Text: "hello, how can I help?"}}},
{Role: "user", Parts: []*genai.Part{{Text: "list the pods"}}},
{Role: "assistant", Parts: []*genai.Part{{Text: "there are no pods"}}},
}

messages, _ := genaiContentsToAnthropicMessages(contents, nil)

require.Len(t, messages, 4)
assert.Equal(t, anthropic.MessageParamRoleUser, messages[0].Role)
assert.Equal(t, anthropic.MessageParamRoleAssistant, messages[1].Role)
assert.Equal(t, anthropic.MessageParamRoleUser, messages[2].Role)
assert.Equal(t, anthropic.MessageParamRoleAssistant, messages[3].Role)
}

// anthropicTestServer serves canned Messages API responses and records the
// request body the SDK sent.
func anthropicTestServer(t *testing.T, handler func(w http.ResponseWriter)) (*AnthropicModel, *[]byte) {
Expand Down