diff --git a/backend/internal/pkg/apicompat/anthropic_image_sources_test.go b/backend/internal/pkg/apicompat/anthropic_image_sources_test.go new file mode 100644 index 0000000000..326b9fff7b --- /dev/null +++ b/backend/internal/pkg/apicompat/anthropic_image_sources_test.go @@ -0,0 +1,86 @@ +package apicompat + +import ( + "encoding/json" + "fmt" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestAnthropicRequestBridges_ImageSources(t *testing.T) { + const imageURL = "https://example.com/screenshot.png?version=1&format=png" + const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jkXcAAAAASUVORK5CYII=" + for _, source := range []struct { + name string + raw string + want string + }{ + {"url", `{"type":"url","url":"` + imageURL + `"}`, imageURL}, + {"base64_control", `{"type":"base64","media_type":"image/png","data":"` + png + `"}`, "data:image/png;base64," + png}, + } { + image := `{"type":"image","source":` + source.raw + `}` + for _, context := range []struct { + name string + messages string + }{ + {"user_image", `[{"role":"user","content":[` + image + `,{"type":"text","text":"Describe this image."}]}]`}, + {"tool_result_image", `[ + {"role":"user","content":"Inspect the screenshot."}, + {"role":"assistant","content":[{"type":"tool_use","id":"toolu_image","name":"inspect","input":{}}]}, + {"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_image","content":[` + image + `,{"type":"text","text":"The screenshot is attached."}]}]} + ]`}, + } { + raw := fmt.Sprintf(`{"model":"gpt-4o","max_tokens":1024,"tools":[{"name":"inspect","input_schema":{"type":"object","properties":{}}}],"messages":%s}`, context.messages) + t.Run(source.name+"/"+context.name+"/chat_completions", func(t *testing.T) { + var req AnthropicRequest + require.NoError(t, json.Unmarshal([]byte(raw), &req)) + out, err := AnthropicToChatCompletionsRequest(&req) + require.NoError(t, err) + wire, err := json.Marshal(out) + require.NoError(t, err) + var upstream ChatCompletionsRequest + require.NoError(t, json.Unmarshal(wire, &upstream)) + var got []string + for _, msg := range upstream.Messages { + var parts []ChatContentPart + if json.Unmarshal(msg.Content, &parts) != nil { + continue + } + for _, part := range parts { + if part.Type == "image_url" && part.ImageURL != nil { + got = append(got, part.ImageURL.URL) + } + } + } + require.Equal(t, []string{source.want}, got, "the image must reach the upstream as image_url") + }) + t.Run(source.name+"/"+context.name+"/responses", func(t *testing.T) { + var req AnthropicRequest + require.NoError(t, json.Unmarshal([]byte(raw), &req)) + out, err := AnthropicToResponses(&req) + require.NoError(t, err) + wire, err := json.Marshal(out) + require.NoError(t, err) + var upstream ResponsesRequest + require.NoError(t, json.Unmarshal(wire, &upstream)) + var items []ResponsesInputItem + require.NoError(t, json.Unmarshal(upstream.Input, &items)) + var got []string + for _, item := range items { + if len(item.Content) == 0 { + continue + } + var parts []ResponsesContentPart + require.NoError(t, json.Unmarshal(item.Content, &parts)) + for _, part := range parts { + if part.Type == "input_image" { + got = append(got, part.ImageURL) + } + } + } + require.Equal(t, []string{source.want}, got, "the image must reach the upstream as input_image") + }) + } + } +} diff --git a/backend/internal/pkg/apicompat/anthropic_to_responses.go b/backend/internal/pkg/apicompat/anthropic_to_responses.go index a54ca94133..f3bdd9fef8 100644 --- a/backend/internal/pkg/apicompat/anthropic_to_responses.go +++ b/backend/internal/pkg/apicompat/anthropic_to_responses.go @@ -359,10 +359,16 @@ func fromResponsesCallID(id string) string { return id } -// anthropicImageToDataURI converts an AnthropicImageSource to a data URI string. -// Returns "" if the source is nil or has no data. +// anthropicImageToDataURI returns a URL source unchanged or encodes base64 data +// as a data URI. Returns "" if the source is nil or has no image data. func anthropicImageToDataURI(src *AnthropicImageSource) string { - if src == nil || src.Data == "" { + if src == nil { + return "" + } + if src.Type == "url" { + return src.URL + } + if src.Data == "" { return "" } mediaType := src.MediaType diff --git a/backend/internal/pkg/apicompat/types.go b/backend/internal/pkg/apicompat/types.go index 956101bd73..f93a51de7c 100644 --- a/backend/internal/pkg/apicompat/types.go +++ b/backend/internal/pkg/apicompat/types.go @@ -110,9 +110,10 @@ func (b AnthropicContentBlock) MarshalJSON() ([]byte, error) { // AnthropicImageSource describes the source data for an image content block. type AnthropicImageSource struct { - Type string `json:"type"` // "base64" + Type string `json:"type"` // "base64" or "url" MediaType string `json:"media_type"` Data string `json:"data"` + URL string `json:"url,omitempty"` } // AnthropicTool describes a tool available to the model.