diff --git a/internal/gateway/anthropic.go b/internal/gateway/anthropic.go index 84d3b838b..0982d12a1 100644 --- a/internal/gateway/anthropic.go +++ b/internal/gateway/anthropic.go @@ -228,9 +228,16 @@ func readAdvised(body []byte, clientMarks, facts, advised bool) (*Request, error case "document": // a PDF, say, as the client sent it: dropped whole // before, it reached the model only when the request - // was relayed as sent (#5452) - if b.Source != nil { - msg.Parts = append(msg.Parts, Part{Kind: File, MediaType: b.Source.MediaType, Data: b.Source.Data, URL: b.Source.URL, block: true}) + // was relayed as sent (#5452). A text source carries + // the document's words, not base64: they go as text + switch { + case b.Source == nil: + case b.Source.Type == "text": + msg.Parts = append(msg.Parts, Part{Kind: Text, Text: b.Source.Data, block: true}) + case b.Source.Type == "base64" && b.Source.Data != "": + msg.Parts = append(msg.Parts, Part{Kind: File, MediaType: b.Source.MediaType, Data: b.Source.Data, block: true}) + case b.Source.URL != "": + msg.Parts = append(msg.Parts, Part{Kind: File, MediaType: b.Source.MediaType, URL: b.Source.URL, block: true}) } case "tool_use": msg.Parts = append(msg.Parts, Part{Kind: ToolCall, ID: anthropicInID(b.ID), Name: b.Name, Args: b.Input, block: true}) @@ -1022,23 +1029,16 @@ func anthropicBody(r *Request, model, host string, replay bool) []byte { rest = append(rest, b) break } - if p.Data != "" { - // the file itself, in Messages' own tongue (#5452) + if p.MediaType == "application/pdf" && p.Data != "" { + // the file itself, in Messages' own tongue (#5452): a + // base64 document takes a PDF only, so any other file + // is still told of rather than turned away with a 400 rest = append(rest, aBlock{Type: "document", Source: &struct { Type string `json:"type"` MediaType string `json:"media_type,omitempty"` Data string `json:"data,omitempty"` URL string `json:"url,omitempty"` }{Type: "base64", MediaType: p.MediaType, Data: p.Data}}) - } else if strings.HasPrefix(p.URL, "http") { - // a document by its URL — Anthropic's url source takes - // http(s), not a cloud-storage scheme (#5452) - rest = append(rest, aBlock{Type: "document", Source: &struct { - Type string `json:"type"` - MediaType string `json:"media_type,omitempty"` - Data string `json:"data,omitempty"` - URL string `json:"url,omitempty"` - }{Type: "url", URL: p.URL}}) } else { rest = append(rest, aBlock{Type: "text", Text: attachmentText(p)}) } diff --git a/internal/gateway/pdf_translation_test.go b/internal/gateway/pdf_translation_test.go index e12ce9444..6cd2d7bc9 100644 --- a/internal/gateway/pdf_translation_test.go +++ b/internal/gateway/pdf_translation_test.go @@ -112,3 +112,53 @@ func TestChatFileRidesToResponsesAndAnthropic(t *testing.T) { t.Fatalf("anthropic body carries the placeholder text:\n%s", out) } } + +// Only a PDF goes as a file: Anthropic's base64 document takes a PDF +// alone, so a .docx sent as one would turn the whole turn away with a +// 400 (#934). Any other file is still told of, on both upstreams. +func TestOnlyAPDFGoesAsAFile(t *testing.T) { + r, err := parseChat([]byte(`{"model":"m","messages":[{"role":"user","content":[{"type":"file","file":{"filename":"notes.csv","file_data":"data:text/csv;base64,YSxiCg=="}}]}]}`)) + if err != nil { + t.Fatal(err) + } + for name, out := range map[string][]byte{"responses": buildResponses(r, "m", "upstream.test", false), "anthropic": buildAnthropic(r, "m")} { + if strings.Contains(string(out), `"input_file"`) || strings.Contains(string(out), `"document"`) { + t.Fatalf("%s body sent a csv as a file:\n%s", name, out) + } + if !strings.Contains(string(out), "[attachment text/csv]") { + t.Fatalf("%s body lost the csv's note:\n%s", name, out) + } + } +} + +// A Responses input_file with file_data carries a filename, as OpenAI's +// shape has it. +func TestResponsesFileHasAFilename(t *testing.T) { + r, err := parseChat([]byte(`{"model":"m","messages":[{"role":"user","content":[{"type":"file","file":{"filename":"doc.pdf","file_data":"data:application/pdf;base64,` + pdfB64 + `"}}]}]}`)) + if err != nil { + t.Fatal(err) + } + if out := buildResponses(r, "m", "upstream.test", false); !strings.Contains(string(out), `"filename":"document.pdf"`) { + t.Fatalf("input_file without a filename:\n%s", out) + } +} + +// A document whose source is text carries its words, not base64: they go +// on as text, never as a PDF's inline data. One given only by URL is told +// of, not fetched by another vendor. +func TestAnthropicDocumentTextAndURLSources(t *testing.T) { + r, err := readAnthropic([]byte(`{"model":"m","max_tokens":64,"messages":[{"role":"user","content":[{"type":"document","source":{"type":"text","media_type":"text/plain","data":"the plain words"}},{"type":"document","source":{"type":"url","url":"https://example.invalid/a.pdf"}}]}]}`), false, false) + if err != nil { + t.Fatal(err) + } + out := string(buildResponses(r, "m", "upstream.test", false)) + if !strings.Contains(out, "the plain words") || strings.Contains(out, `"input_file"`) { + t.Fatalf("text document not carried as text:\n%s", out) + } + if !strings.Contains(out, "[attachment : https://example.invalid/a.pdf]") { + t.Fatalf("url document not told of:\n%s", out) + } + if a := string(buildAnthropic(r, "m")); strings.Contains(a, `"url":"https://example.invalid/a.pdf"`) && strings.Contains(a, `"document"`) { + t.Fatalf("url document sent as a document:\n%s", a) + } +} diff --git a/internal/gateway/responses.go b/internal/gateway/responses.go index c31be592e..31b5d4c01 100644 --- a/internal/gateway/responses.go +++ b/internal/gateway/responses.go @@ -1423,9 +1423,11 @@ func buildResponses(r *Request, model, host string, rejectTemp bool) []byte { } content = append(content, map[string]any{"type": t, "text": p.Text}) case File: - if m.Role != "assistant" && p.Data != "" { - // the file itself, in Responses' own tongue (#5452) - content = append(content, map[string]any{"type": "input_file", "file_data": "data:" + p.MediaType + ";base64," + p.Data}) + if m.Role != "assistant" && p.MediaType == "application/pdf" && p.Data != "" { + // the file itself, in Responses' own tongue (#5452): + // file_data goes with a filename, and a PDF is the + // file every Responses model reads + content = append(content, map[string]any{"type": "input_file", "filename": "document.pdf", "file_data": "data:" + p.MediaType + ";base64," + p.Data}) } else { // a file only referenced (a Gemini fileUri, another // host's file id) is told of: Responses' file_id is