Skip to content

fix(amazon-bedrock): support document files in tool results#15361

Closed
pragnyanramtha wants to merge 8 commits into
vercel:mainfrom
pragnyanramtha:pragnyan/issue-15319-bedrock-tool-result-documents
Closed

fix(amazon-bedrock): support document files in tool results#15361
pragnyanramtha wants to merge 8 commits into
vercel:mainfrom
pragnyanramtha:pragnyan/issue-15319-bedrock-tool-result-documents

Conversation

@pragnyanramtha
Copy link
Copy Markdown
Contributor

Background

Fixes #15319.

Amazon Bedrock supports document entries inside ToolResultBlock.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

  • Convert supported inline non-image tool-result file parts to Bedrock document blocks.
  • Keep existing image conversion behavior and continue rejecting URL/reference file data.
  • Add tests for inline PDF and text document tool-result content.
  • Add a patch changeset for @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-lockfile
  • pnpm --filter @ai-sdk/amazon-bedrock... build
  • pnpm exec vitest --config vitest.node.config.js --run src/convert-to-amazon-bedrock-chat-messages.test.ts
  • pnpm exec vitest --config vitest.edge.config.js --run src/convert-to-amazon-bedrock-chat-messages.test.ts
  • pnpm --filter @ai-sdk/amazon-bedrock type-check
  • pnpm 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.ts
  • git diff --check

Checklist

  • All commits are signed (PRs with unsigned commits cannot be merged)
  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Allow Amazon Bedrock tool-result file parts to emit document content blocks for supported non-image inline files.

Fixes vercel#15319.
@pragnyanramtha pragnyanramtha marked this pull request as ready for review May 16, 2026 04:45
Copilot AI review requested due to automatic review settings May 16, 2026 04:45
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Collaborator

@aayush-kapoor aayush-kapoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[@ai-sdk/amazon-bedrock] file content in tool results rejects PDFs and other documents

3 participants