Skip to content

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

Merged
aayush-kapoor merged 2 commits into
mainfrom
aayush/bedrock-bug
May 27, 2026
Merged

fix(amazon-bedrock): support document files in tool results#15534
aayush-kapoor merged 2 commits into
mainfrom
aayush/bedrock-bug

Conversation

@aayush-kapoor
Copy link
Copy Markdown
Collaborator

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

repro:
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);
});

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)

Related Issues

fixes #15319

Co-authored-by: Pragnyan Ramtha pragnyanramtha@gmail.com

@aayush-kapoor aayush-kapoor added the backport Admins only: add this label to a pull request in order to backport it to the prior version label May 21, 2026
) {
throw new UnsupportedFunctionalityError({
functionality: `media type: ${contentPart.mediaType}`,
toolResultContent = await Promise.all(
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

it needs an await since for handling docs/files in amazon bedrock, we use shouldEnableCitations which is async in nature

@aayush-kapoor aayush-kapoor merged commit 9d5a299 into main May 27, 2026
52 checks passed
@aayush-kapoor aayush-kapoor deleted the aayush/bedrock-bug branch May 27, 2026 17:09
github-actions Bot added a commit that referenced this pull request May 27, 2026
@github-actions github-actions Bot removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label May 27, 2026
@github-actions
Copy link
Copy Markdown
Contributor

⚠️ Backport to release-v6.0 created but has conflicts: #15626

aayush-kapoor added a commit that referenced this pull request May 27, 2026
…#15626)

This is an automated backport of #15534 to the release-v6.0 branch. FYI
@aayush-kapoor
This backport has conflicts that need to be resolved manually.

### `git cherry-pick` output

```
Auto-merging packages/amazon-bedrock/src/bedrock-api-types.ts
CONFLICT (content): Merge conflict in packages/amazon-bedrock/src/bedrock-api-types.ts
Auto-merging packages/amazon-bedrock/src/convert-to-bedrock-chat-messages.test.ts
Auto-merging packages/amazon-bedrock/src/convert-to-bedrock-chat-messages.ts
CONFLICT (content): Merge conflict in packages/amazon-bedrock/src/convert-to-bedrock-chat-messages.ts
error: could not apply 9d5a299... fix(amazon-bedrock): support document files in tool results (#15534)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
```

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Aayush Kapoor <aayushkapoor34@gmail.com>
Co-authored-by: Aayush Kapoor <83492835+aayush-kapoor@users.noreply.github.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

2 participants