fix(amazon-bedrock): support document files in tool results#15361
Closed
pragnyanramtha wants to merge 8 commits into
Closed
fix(amazon-bedrock): support document files in tool results#15361pragnyanramtha wants to merge 8 commits into
pragnyanramtha wants to merge 8 commits into
Conversation
Allow Amazon Bedrock tool-result file parts to emit document content blocks for supported non-image inline files. Fixes vercel#15319.
…9-bedrock-tool-result-documents
…9-bedrock-tool-result-documents
…-15319-bedrock-tool-result-documents
5 tasks
Collaborator
aayush-kapoor
left a comment
There was a problem hiding this comment.
marking as closed in favor of #15534; needed some signed commits. added you as a co-author
aayush-kapoor
added a commit
that referenced
this pull request
May 27, 2026
## Background reported in #15319 alternate to #15361 with all signed commits + reproduction the `tool-result case 'file'` branch only allows image media types, everything else throws `UnsupportedFunctionalityError`. PDFs and other documents are unsupported, even though Bedrock's Converse API accepts documents. ## Summary tool-result file handling now mirrors user-message file handling: - image tool-result files still convert to Bedrock image blocks - non-image tool-result files now convert to Bedrock document blocks ## Manual Verification <details> <summary>repro:</summary> ```ts import { amazonBedrock } from '@ai-sdk/amazon-bedrock'; import { generateText, tool } from 'ai'; import { run } from '../../lib/run'; import { z } from 'zod'; function createPdfData(text: string): Uint8Array { const stream = `BT /F1 24 Tf 72 720 Td (${text}) Tj ET`; const objects = [ '<< /Type /Catalog /Pages 2 0 R >>', '<< /Type /Pages /Kids [3 0 R] /Count 1 >>', '<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>', '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>', `<< /Length ${stream.length} >> stream ${stream} endstream`, ]; let pdf = '%PDF-1.4\n'; const offsets = [0]; for (const [index, object] of objects.entries()) { offsets.push(pdf.length); pdf += `${index + 1} 0 obj\n${object}\nendobj\n`; } const xrefOffset = pdf.length; pdf += `xref 0 ${objects.length + 1} 0000000000 65535 f ${offsets .slice(1) .map(offset => `${offset.toString().padStart(10, '0')} 00000 n `) .join('\n')} trailer << /Size ${objects.length + 1} /Root 1 0 R >> startxref ${xrefOffset} %%EOF`; return new TextEncoder().encode(pdf); } const pdfData = createPdfData('This PDF was returned by a tool result.'); run(async () => { const result = await generateText({ model: amazonBedrock('us.anthropic.claude-sonnet-4-5-20250929-v1:0'), tools: { readDocument: tool({ description: 'Read a PDF document', inputSchema: z.object({}), execute: async () => ({ ok: true }), }), }, messages: [ { role: 'user', content: 'Read the PDF returned by the tool.', }, { role: 'assistant', content: [ { type: 'tool-call', toolCallId: 'call-read-document', toolName: 'readDocument', input: {}, }, ], }, { role: 'tool', content: [ { type: 'tool-result', toolCallId: 'call-read-document', toolName: 'readDocument', output: { type: 'content', value: [ { type: 'file', data: { type: 'data', data: pdfData }, mediaType: 'application/pdf', filename: 'repro.pdf', }, ], }, }, ], }, { role: 'user', content: 'Summarize the PDF.', }, ], }); console.log(result.text); }); ``` </details> ## Checklist - [x] All commits are signed (PRs with unsigned commits cannot be merged) - [x] Tests have been added / updated (for bug fixes / features) - [ ] Documentation has been added / updated (for bug fixes / features) - [x] A _patch_ changeset for relevant packages has been added (for bug fixes / features - run `pnpm changeset` in the project root) - [x] I have reviewed this pull request (self-review) ## Related Issues fixes #15319 Co-authored-by: Pragnyan Ramtha <pragnyanramtha@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
Fixes #15319.
Amazon Bedrock supports
documententries insideToolResultBlock.content, but the AI SDK converter only allowed image file parts in tool results. Supported inline non-image file parts were rejected before they could be sent to Bedrock.Summary
documentblocks.@ai-sdk/amazon-bedrock.Manual Verification
Not run against live Bedrock; this change is limited to converter output shape and is covered by unit/type checks.
Validation
pnpm install --frozen-lockfilepnpm --filter @ai-sdk/amazon-bedrock... buildpnpm exec vitest --config vitest.node.config.js --run src/convert-to-amazon-bedrock-chat-messages.test.tspnpm exec vitest --config vitest.edge.config.js --run src/convert-to-amazon-bedrock-chat-messages.test.tspnpm --filter @ai-sdk/amazon-bedrock type-checkpnpm exec ultracite check packages/amazon-bedrock/src/amazon-bedrock-api-types.ts packages/amazon-bedrock/src/convert-to-amazon-bedrock-chat-messages.ts packages/amazon-bedrock/src/convert-to-amazon-bedrock-chat-messages.test.tsgit diff --checkChecklist
pnpm changesetin the project root)