Skip to content

feat(model/claude): translate file/PDF parts to Anthropic document blocks - #919

Open
gutopolewacz wants to merge 1 commit into
cloudwego:mainfrom
gutopolewacz:feat/claude-document-pdf
Open

feat(model/claude): translate file/PDF parts to Anthropic document blocks#919
gutopolewacz wants to merge 1 commit into
cloudwego:mainfrom
gutopolewacz:feat/claude-document-pdf

Conversation

@gutopolewacz

Copy link
Copy Markdown

Refs #918

What

Adds translation of schema.ChatMessagePartTypeFileURL (PDF) content parts to the Anthropic Messages document block in components/model/claude.

Why

Currently a PDF file part errors with anthropic message type not supported: file. This blocks the multimodal read_file flow (the adk/middlewares/filesystem middleware with UseMultiModalRead: true over a MultiModalReader backend such as adk/backend/local): a PDF arrives as a FileContentPartTypePDF part, is converted by the schema layer to MessageInputPart{Type: ChatMessagePartTypeFileURL, File}, and then fails at the Claude translation layer — so a PDF read cannot be delivered to the model as a native document.

How

  • convSchemaMessage (user-input multi content) and convToolMultiContent (tool result) gain a case schema.ChatMessagePartTypeFileURL.
  • Two helpers, pdfDocumentSource and newPDFDocumentBlock, build the DocumentBlockParam from the file part (URL or raw base64).
  • Validation mirrors the existing image case: a URL takes precedence over base64; base64 requires a MIMEType and rejects a data: prefix. An application/pdf guard is added because the base64 document source is PDF-only.
  • Documents are supported inside tool results via ToolResultBlockParamContentUnion.OfDocument, so the tool path (the multimodal read_file case) works.

Tests

Added Test_convSchemaMessage_Document, mirroring Test_convSchemaMessage_MultiContent for the document path: base64 success, URL success, data:-prefix error, missing-MIME error, non-PDF-MIME error, missing-source error, and the tool-result path (base64 + nil-file).

Compatibility

Purely additive; existing text/image behavior is unchanged. No new dependency and no SDK bump — DocumentBlockParam, Base64PDFSourceParam, URLPDFSourceParam, and ToolResultBlockParamContentUnion.OfDocument are all present in the currently used anthropic-sdk-go.

…ocks

A schema.ChatMessagePartTypeFileURL content part (e.g. a PDF returned by the
filesystem middleware's multimodal read_file over a MultiModalReader backend)
had no translation and fell into the default branch, failing with
"anthropic message type not supported: file".

Add a case for ChatMessagePartTypeFileURL to both convSchemaMessage
(user-input multi content) and convToolMultiContent (tool result), mapping a
PDF file part to an Anthropic document block (Base64PDFSource or URLPDFSource).
The handling mirrors the existing image case (URL vs raw base64, MIMEType
required for base64, reject a "data:" prefix), with an application/pdf guard
because the base64 document source is PDF-only. Documents are supported inside
tool results via ToolResultBlockParamContentUnion.OfDocument, so the multimodal
read_file path can now deliver a PDF to the model natively.

No new dependency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant