feat: PDF attachments as Anthropic document blocks
Reuses llm.ImageContent/session.ImageData with MimeType "application/pdf" rather than adding a new field: images already flow tool result -> session entry -> reload -> provider end to end, so a MIME check at the provider edge is the smallest change. - Anthropic provider emits a document block (source.type=base64, media_type=application/pdf) for a PDF attachment in a tool result or a user message; images are unaffected. Cache control now also applies to document blocks. - OpenAI provider replaces a PDF attachment with a text placeholder instead of an invalid image_url data URI; images still emitted. Gemini already accepts application/pdf inline and needs no change.
This commit is contained in:
1 parent
a1f34cce4c
commit
62f3424f30
6 files changed
+377
-3
No files matched your search
@@ -14,6 +14,13 @@ import (
|
||||
"github.com/sausheong/harness/llm"
|
||||
)
|
||||
|
||||
// pdfAttachmentPlaceholder replaces a PDF attachment (llm.ImageContent with
|
||||
// MimeType "application/pdf") in outgoing messages: OpenAI's image_url part
|
||||
// only accepts image MIME types, so sending a PDF that way would either be
|
||||
// silently ignored or rejected by the API. Gemini accepts application/pdf
|
||||
// inline natively and is not routed through this builder.
|
||||
const pdfAttachmentPlaceholder = "[PDF attachment omitted: this provider can't read PDFs]"
|
||||
|
||||
// logOpenAIError unwraps the go-openai SDK error into a structured slog.WARN
|
||||
// record so we can see exactly why LiteLLM/OpenAI rejected the request — the
|
||||
// SDK's flat error string drops Param, Type, Code, and the raw response body
|
||||
@@ -129,6 +136,13 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
|
||||
if len(m.Images) > 0 {
|
||||
var parts []openai.ChatMessagePart
|
||||
for _, img := range m.Images {
|
||||
if img.MimeType == "application/pdf" {
|
||||
parts = append(parts, openai.ChatMessagePart{
|
||||
Type: openai.ChatMessagePartTypeText,
|
||||
Text: pdfAttachmentPlaceholder,
|
||||
})
|
||||
continue
|
||||
}
|
||||
encoded := base64.StdEncoding.EncodeToString(img.Data)
|
||||
dataURI := fmt.Sprintf("data:%s;base64,%s", img.MimeType, encoded)
|
||||
parts = append(parts, openai.ChatMessagePart{
|
||||
@@ -151,6 +165,13 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
|
||||
} else if len(m.Images) > 0 {
|
||||
var parts []openai.ChatMessagePart
|
||||
for _, img := range m.Images {
|
||||
if img.MimeType == "application/pdf" {
|
||||
parts = append(parts, openai.ChatMessagePart{
|
||||
Type: openai.ChatMessagePartTypeText,
|
||||
Text: pdfAttachmentPlaceholder,
|
||||
})
|
||||
continue
|
||||
}
|
||||
encoded := base64.StdEncoding.EncodeToString(img.Data)
|
||||
dataURI := fmt.Sprintf("data:%s;base64,%s", img.MimeType, encoded)
|
||||
parts = append(parts, openai.ChatMessagePart{
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
@@ -74,6 +75,75 @@ func TestOpenAIChatStreamPartsBeatString(t *testing.T) {
|
||||
require.Equal(t, "new", captured.Messages[0].Content)
|
||||
}
|
||||
|
||||
// TestOpenAIChatStream_PDFToolResultOmittedWithPlaceholder verifies that a
|
||||
// PDF attachment on a tool result is never emitted as an image_url data
|
||||
// URI (which would be invalid — image_url expects an image MIME type) and
|
||||
// that the placeholder text takes its place; a real image attachment in
|
||||
// the same result is still emitted.
|
||||
func TestOpenAIChatStream_PDFToolResultOmittedWithPlaceholder(t *testing.T) {
|
||||
pngBytes := []byte{0x89, 0x50, 0x4E, 0x47}
|
||||
pdfBytes := []byte("%PDF-1.4 fake")
|
||||
captured := captureOpenAIRequest(t, llm.ChatRequest{
|
||||
Messages: []llm.Message{
|
||||
{
|
||||
Role: "user",
|
||||
ToolCallID: "T1",
|
||||
Content: "see attached",
|
||||
Images: []llm.ImageContent{
|
||||
{MimeType: "image/png", Data: pngBytes},
|
||||
{MimeType: "application/pdf", Data: pdfBytes},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
require.NotNil(t, captured)
|
||||
|
||||
var sawImage, sawPlaceholder bool
|
||||
for _, m := range captured.Messages {
|
||||
for _, part := range m.MultiContent {
|
||||
if part.Type == openai.ChatMessagePartTypeImageURL && part.ImageURL != nil {
|
||||
require.NotContains(t, part.ImageURL.URL, "application/pdf", "a PDF must never be sent as image_url")
|
||||
sawImage = true
|
||||
}
|
||||
if part.Type == openai.ChatMessagePartTypeText && strings.Contains(part.Text, pdfAttachmentPlaceholder) {
|
||||
sawPlaceholder = true
|
||||
}
|
||||
}
|
||||
}
|
||||
require.True(t, sawImage, "the PNG attachment must still be emitted as image_url")
|
||||
require.True(t, sawPlaceholder, "the PDF attachment must be replaced with the placeholder text")
|
||||
}
|
||||
|
||||
// TestOpenAIChatStream_PDFUserMessageOmittedWithPlaceholder covers the
|
||||
// plain user-message image branch (not a tool result).
|
||||
func TestOpenAIChatStream_PDFUserMessageOmittedWithPlaceholder(t *testing.T) {
|
||||
pdfBytes := []byte("%PDF-1.4 fake")
|
||||
captured := captureOpenAIRequest(t, llm.ChatRequest{
|
||||
Messages: []llm.Message{
|
||||
{
|
||||
Role: "user",
|
||||
Content: "read this",
|
||||
Images: []llm.ImageContent{{MimeType: "application/pdf", Data: pdfBytes}},
|
||||
},
|
||||
},
|
||||
})
|
||||
require.NotNil(t, captured)
|
||||
|
||||
var sawImageURL, sawPlaceholder bool
|
||||
for _, m := range captured.Messages {
|
||||
for _, part := range m.MultiContent {
|
||||
if part.Type == openai.ChatMessagePartTypeImageURL {
|
||||
sawImageURL = true
|
||||
}
|
||||
if part.Type == openai.ChatMessagePartTypeText && strings.Contains(part.Text, pdfAttachmentPlaceholder) {
|
||||
sawPlaceholder = true
|
||||
}
|
||||
}
|
||||
}
|
||||
require.False(t, sawImageURL, "a PDF must never be sent as image_url")
|
||||
require.True(t, sawPlaceholder, "the PDF attachment must be replaced with the placeholder text")
|
||||
}
|
||||
|
||||
func TestEmitToolCalls_OrderedByIndex(t *testing.T) {
|
||||
mk := func(id, name string) *pendingTC {
|
||||
p := &pendingTC{id: id, name: name}
|
||||
|
||||
Reference in new issue
Block a user