Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/good-dancers-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ai-sdk/anthropic': minor
'@ai-sdk/provider': minor
---

Support for container uploads for claude
31 changes: 31 additions & 0 deletions content/providers/01-ai-sdk-providers/05-anthropic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,37 @@ const result = await generateText({
});
```

#### Container Uploads

You can also add files for code execution using container uploads. For example,

```ts
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const codeExecutionTool = anthropic.tools.codeExecution_20250522();
const result = await generateText({
model: anthropic('claude-opus-4-20250514'),
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'Analyze this csv file',
},
{
type: 'container_upload',
file_id: 'file-123',
},
],
},
],
tools: {
code_execution: codeExecutionTool,
},
});
```

#### Error Handling

Code execution errors are handled differently depending on whether you're using streaming or non-streaming:
Expand Down
6 changes: 6 additions & 0 deletions packages/anthropic/src/anthropic-api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface AnthropicUserMessage {
| AnthropicImageContent
| AnthropicDocumentContent
| AnthropicToolResultContent
| AnthropicContainerUploadContent
>;
}

Expand All @@ -40,6 +41,11 @@ export interface AnthropicTextContent {
cache_control: AnthropicCacheControl | undefined;
}

export interface AnthropicContainerUploadContent {
type: 'container_upload';
file_id: string;
}

export interface AnthropicThinkingContent {
type: 'thinking';
thinking: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { LanguageModelV2CallWarning } from '@ai-sdk/provider';
import { describe, expect, it } from 'vitest';
import { convertToAnthropicMessagesPrompt } from './convert-to-anthropic-messages-prompt';

describe('system messages', () => {
Expand Down Expand Up @@ -1612,4 +1612,34 @@ describe('citations', () => {
}
`);
});

it('should convert container upload message parts into anthropic container uploads', async () => {
const result = await convertToAnthropicMessagesPrompt({
prompt: [
{
role: 'user',
content: [
{
type: 'container_upload',
file_id: 'file-123',
},
],
},
],
sendReasoning: false,
warnings: [],
});

expect(result).toEqual({
prompt: {
messages: [
{
role: 'user',
content: [{ type: 'container_upload', file_id: 'file-123' }],
},
],
},
betas: new Set(),
});
});
});
10 changes: 9 additions & 1 deletion packages/anthropic/src/convert-to-anthropic-messages-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
import { anthropicReasoningMetadataSchema } from './anthropic-messages-language-model';
import { anthropicFilePartProviderOptions } from './anthropic-messages-options';
import { getCacheControl } from './get-cache-control';
import { webSearch_20250305OutputSchema } from './tool/web-search_20250305';
import { codeExecution_20250522OutputSchema } from './tool/code-execution_20250522';
import { webSearch_20250305OutputSchema } from './tool/web-search_20250305';

function convertToString(data: LanguageModelV2DataContent): string {
if (typeof data === 'string') {
Expand Down Expand Up @@ -227,6 +227,14 @@ export async function convertToAnthropicMessagesPrompt({

break;
}

case 'container_upload': {
anthropicContent.push({
type: 'container_upload',
file_id: part.file_id,
});
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export type LanguageModelV2Message =
}
| {
role: 'user';
content: Array<LanguageModelV2TextPart | LanguageModelV2FilePart>;
content: Array<
| LanguageModelV2TextPart
| LanguageModelV2FilePart
| LanguageModelV2ContainerUploadPart
>;
}
| {
role: 'assistant';
Expand Down Expand Up @@ -68,6 +72,25 @@ The text content.
providerOptions?: SharedV2ProviderOptions;
}

/**
Container uploads content part. Supported by Anthropic API.
*/
export interface LanguageModelV2ContainerUploadPart {
type: 'container_upload';

/**
* The file id
*/
file_id: string;

/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV2ProviderOptions;
}

/**
Reasoning content part of a prompt. It contains a string of reasoning text.
*/
Expand Down