gateway: only a PDF goes as a translated file, with a filename (#5452)
Review of the commit before. It wrote a Messages `document` and a Responses `input_file` for a file of any type. Anthropic's base64 document takes only a PDF, so a .docx or .csv that used to go as the "[attachment …]" note would now get a 400 for the whole turn, the #934 risk. Both encoders now write the file only for application/pdf with data. Any other file is still described in the note. - The Responses input_file carries "filename": "document.pdf" beside file_data, as OpenAI's shape has it. Part has no field of its own for a file name: Name is a tool's, and is read in many places. - A Messages document by URL is described in the note, not sent as a url source. A file reference from another vendor (Gemini's fileUri is an https Files-API link) is not something Anthropic should fetch. - On the read side, a `document` with a text source carries the document's words, so it becomes a text part rather than base64 inline data. A base64 source is a file. A url source is a file reference. Anything else (a file_id source) is left out, as before. Run on mica: TestOnlyAPDFGoesAsAFile, TestResponsesFileHasAFilename and TestAnthropicDocumentTextAndURLSources fail at the commit before (a csv sent as input_file, no filename, the text source not carried as text) and pass here. The patch's own tests (pdf_translation_test.go, image_input_test.go) fail with the product files at origin/maind987cc7d5(input_file and document dropped, the Chat file became the note) and pass here. Not tried: a real PDF through a ChatGPT API account or Anthropic upstream. Batch record (#5205, #5197, #5452), all on mote: - Full suite (go test -tags nogui -count=1 -timeout 45m ./...) at the same four changes ond987cc7d5: every package ok except two. internal/cli's TestEveryRouteHasACommandOrIsNamed (POST /api/page-away) was fixed on main by69946f43c. internal/plugin's TestGatewayHookSlowRecallUsesConfiguredBudget is #6019. - Rebased ona63923e3d: go vet -tags nogui ./... on darwin, linux and windows ok. internal/gateway, claudetui, cli and plugin all ok. - Rebased on8769a5819, the pushed base: vet of gateway and claudetui on the three OSes ok, and the gateway -run 'Codex|PDF|Document|InputFile|ChatFile|Image|…' tests ok. That run was on a mote busy with other sessions' work (load ~120). - Windows (xiaomi-gpu): see #5205's commit.
This commit is contained in:
@@ -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)})
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user