From a627160334414baa81b9bffc7535a60151bbeb4e Mon Sep 17 00:00:00 2001 From: Nikolas Lazaris Date: Tue, 14 Oct 2025 14:33:42 -0400 Subject: [PATCH 1/6] feature(provider/anthropic): Add support for url-based pdf files in tool result --- ...nvert-to-anthropic-messages-prompt.test.ts | 75 ++++++++++++++++++- .../convert-to-anthropic-messages-prompt.ts | 20 +++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts index e612d102efb1..4f6fbf56e292 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts @@ -491,7 +491,7 @@ describe('tool messages', () => { `); }); - it('should handle tool result with PDF content', async () => { + it('should handle tool result with base64 PDF content', async () => { const result = await convertToAnthropicMessagesPrompt({ prompt: [ { @@ -563,6 +563,79 @@ describe('tool messages', () => { } `); }); + + + it('should handle tool result with url-based PDF content', async () => { + const result = await convertToAnthropicMessagesPrompt({ + prompt: [ + { + role: 'tool', + content: [ + { + type: 'tool-result', + toolName: 'image-generator', + toolCallId: 'image-gen-1', + output: { + type: 'content', + value: [ + { + type: 'media', + data: 'https://example.com/document.pdf', + mediaType: 'application/pdf', + }, + { + type: 'text', + text: 'Document loaded successfully', + }, + ], + }, + }, + ], + }, + ], + sendReasoning: true, + warnings: [], + }); + + expect(result).toMatchInlineSnapshot(` + { + "betas": Set { + "pdfs-2024-09-25", + }, + "prompt": { + "messages": [ + { + "content": [ + { + "cache_control": undefined, + "content": [ + { + "cache_control": undefined, + "source": { + "type": "url", + "url": "https://example.com/document.pdf", + }, + "type": "document", + }, + { + "cache_control": undefined, + "text": "Document loaded successfully", + "type": "text", + }, + ], + "is_error": undefined, + "tool_use_id": "image-gen-1", + "type": "tool_result", + }, + ], + "role": "user", + }, + ], + "system": undefined, + }, + } + `); + }); }); describe('assistant messages', () => { diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts index 4b4c15baddf8..4d617766d8e6 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts @@ -46,6 +46,15 @@ function convertToString(data: LanguageModelV3DataContent): string { }); } +function isValidUrl(string: string): boolean { + try { + new URL(string); + return true; + } catch { + return false; + } +} + export async function convertToAnthropicMessagesPrompt({ prompt, sendReasoning, @@ -282,6 +291,17 @@ export async function convertToAnthropicMessagesPrompt({ if (contentPart.mediaType === 'application/pdf') { betas.add('pdfs-2024-09-25'); + if (isValidUrl(contentPart.data)) { + return { + type: 'document', + source: { + type: 'url', + url: contentPart.data, + }, + cache_control: undefined + }; + } + return { type: 'document', source: { From 577e63ee875baab70da28260ae2fd50d0ba13a67 Mon Sep 17 00:00:00 2001 From: Nikolas Lazaris Date: Tue, 14 Oct 2025 14:44:06 -0400 Subject: [PATCH 2/6] update examples --- ...s => anthropic-pdf-tool-results-base64.ts} | 0 .../anthropic-pdf-tool-results-url.ts | 50 +++++++++++++++++++ 2 files changed, 50 insertions(+) rename examples/ai-core/src/generate-text/{anthropic-pdf-tool-results.ts => anthropic-pdf-tool-results-base64.ts} (100%) create mode 100644 examples/ai-core/src/generate-text/anthropic-pdf-tool-results-url.ts diff --git a/examples/ai-core/src/generate-text/anthropic-pdf-tool-results.ts b/examples/ai-core/src/generate-text/anthropic-pdf-tool-results-base64.ts similarity index 100% rename from examples/ai-core/src/generate-text/anthropic-pdf-tool-results.ts rename to examples/ai-core/src/generate-text/anthropic-pdf-tool-results-base64.ts diff --git a/examples/ai-core/src/generate-text/anthropic-pdf-tool-results-url.ts b/examples/ai-core/src/generate-text/anthropic-pdf-tool-results-url.ts new file mode 100644 index 000000000000..3c8be263971d --- /dev/null +++ b/examples/ai-core/src/generate-text/anthropic-pdf-tool-results-url.ts @@ -0,0 +1,50 @@ +import { generateText, stepCountIs, tool } from 'ai'; +import { run } from '../lib/run'; +import { z } from 'zod'; +import { anthropic } from '@ai-sdk/anthropic'; + +run(async () => { + const readPDFDocument = tool({ + description: `Read and return a PDF document`, + inputSchema: z.object({}), + execute: async () => { + try { + return { + success: true, + description: 'Successfully loaded PDF document', + pdfUrl: 'https://arxiv.org/pdf/2001.08361', // Scaling Laws Paper + }; + } catch (error) { + throw new Error(`Failed to analyze PDF: ${error}`); + } + }, + toModelOutput(result) { + return { + type: 'content', + value: [ + { + type: 'text', + text: result.description, + }, + { + type: 'media', + data: result.pdfUrl, + mediaType: 'application/pdf', + }, + ], + }; + }, + }); + + const result = await generateText({ + model: anthropic('claude-sonnet-4-0'), + prompt: + 'Please read the pdf document using the tool provided and return the summary of that pdf', + tools: { + readPDFDocument, + }, + stopWhen: stepCountIs(4), + }); + + console.log(`Assisstant response : ${JSON.stringify(result.text, null, 2)}`); +}); From 04069fb56d66aea87cae4d9e6cdc0c89cd5a71ca Mon Sep 17 00:00:00 2001 From: Nikolas Lazaris Date: Tue, 14 Oct 2025 14:45:53 -0400 Subject: [PATCH 3/6] Add changeset --- .changeset/chilly-bees-sin.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/chilly-bees-sin.md diff --git a/.changeset/chilly-bees-sin.md b/.changeset/chilly-bees-sin.md new file mode 100644 index 000000000000..7af27a68c1ab --- /dev/null +++ b/.changeset/chilly-bees-sin.md @@ -0,0 +1,6 @@ +--- +'@ai-sdk/anthropic': patch +'@example/ai-core': patch +--- + +Adds url-based pdf support for tool results From 5d9154677e2aa9c4e7a556da92ea234cec08a862 Mon Sep 17 00:00:00 2001 From: Nikolas Lazaris Date: Tue, 14 Oct 2025 14:53:03 -0400 Subject: [PATCH 4/6] run prettier fix --- .../anthropic/src/convert-to-anthropic-messages-prompt.test.ts | 1 - packages/anthropic/src/convert-to-anthropic-messages-prompt.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts index 4f6fbf56e292..781291c4d30b 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts @@ -564,7 +564,6 @@ describe('tool messages', () => { `); }); - it('should handle tool result with url-based PDF content', async () => { const result = await convertToAnthropicMessagesPrompt({ prompt: [ diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts index 4d617766d8e6..8db2eb38b318 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts @@ -298,7 +298,7 @@ export async function convertToAnthropicMessagesPrompt({ type: 'url', url: contentPart.data, }, - cache_control: undefined + cache_control: undefined, }; } From dea91346554ef52430b8cd06afcd2f9f1acb0eb4 Mon Sep 17 00:00:00 2001 From: Nikolas Lazaris Date: Thu, 23 Oct 2025 00:13:10 -0400 Subject: [PATCH 5/6] update pkg --- .../src/convert-to-anthropic-messages-prompt.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts index 8db2eb38b318..04cb4dce0bfe 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts @@ -277,6 +277,16 @@ export async function convertToAnthropicMessagesPrompt({ }; case 'media': { if (contentPart.mediaType.startsWith('image/')) { + if (isValidUrl(contentPart.data)) { + return { + type: 'image', + source: { + type: 'url', + url: contentPart.data, + }, + cache_control: undefined, + }; + } return { type: 'image', source: { From 57e98f5e1fb1b73ba83d90f41bd907a5cb8d7bc2 Mon Sep 17 00:00:00 2001 From: Nikolas Lazaris Date: Thu, 23 Oct 2025 00:14:27 -0400 Subject: [PATCH 6/6] change pkg name --- packages/anthropic/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/anthropic/package.json b/packages/anthropic/package.json index 2a60f8e7cf41..80ec4a724000 100644 --- a/packages/anthropic/package.json +++ b/packages/anthropic/package.json @@ -1,5 +1,5 @@ { - "name": "@ai-sdk/anthropic", + "name": "@flatfile/anthropic", "version": "3.0.0-beta.24", "license": "Apache-2.0", "sideEffects": false,