@ai-sdk/anthropic 4.0.0-beta.40 → 4.0.0-beta.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -843,7 +843,7 @@ const computerTool = anthropic.tools.computer_20251124({
843
843
  toModelOutput({ output }) {
844
844
  return typeof output === 'string'
845
845
  ? [{ type: 'text', text: output }]
846
- : [{ type: 'image', data: output.data, mediaType: 'image/png' }];
846
+ : [{ type: 'file-data', data: output.data, mediaType: 'image/png' }];
847
847
  },
848
848
  });
849
849
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "4.0.0-beta.40",
3
+ "version": "4.0.0-beta.41",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -35,15 +35,15 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@ai-sdk/provider": "4.0.0-beta.13",
39
- "@ai-sdk/provider-utils": "5.0.0-beta.28"
38
+ "@ai-sdk/provider": "4.0.0-beta.14",
39
+ "@ai-sdk/provider-utils": "5.0.0-beta.29"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/node": "20.17.24",
43
43
  "tsup": "^8",
44
44
  "typescript": "5.8.3",
45
45
  "zod": "3.25.76",
46
- "@ai-sdk/test-server": "2.0.0-beta.2",
46
+ "@ai-sdk/test-server": "2.0.0-beta.3",
47
47
  "@vercel/ai-tsconfig": "0.0.0"
48
48
  },
49
49
  "peerDependencies": {
@@ -1,5 +1,9 @@
1
- import { JSONSchema7 } from '@ai-sdk/provider';
2
- import { InferSchema, lazySchema, zodSchema } from '@ai-sdk/provider-utils';
1
+ import type { JSONSchema7 } from '@ai-sdk/provider';
2
+ import {
3
+ lazySchema,
4
+ zodSchema,
5
+ type InferSchema,
6
+ } from '@ai-sdk/provider-utils';
3
7
  import { z } from 'zod/v4';
4
8
 
5
9
  export type AnthropicPrompt = {
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  createJsonErrorResponseHandler,
3
- InferSchema,
4
3
  lazySchema,
5
4
  zodSchema,
5
+ type InferSchema,
6
6
  } from '@ai-sdk/provider-utils';
7
7
  import { z } from 'zod/v4';
8
8
 
@@ -1,16 +1,16 @@
1
- import {
1
+ import type {
2
2
  FilesV4,
3
3
  FilesV4UploadFileCallOptions,
4
4
  FilesV4UploadFileResult,
5
5
  } from '@ai-sdk/provider';
6
6
  import {
7
7
  combineHeaders,
8
- convertBase64ToUint8Array,
8
+ convertInlineFileDataToUint8Array,
9
9
  createJsonResponseHandler,
10
- FetchFunction,
11
10
  lazySchema,
12
11
  postFormDataToApi,
13
12
  zodSchema,
13
+ type FetchFunction,
14
14
  } from '@ai-sdk/provider-utils';
15
15
  import { z } from 'zod/v4';
16
16
  import { anthropicFailedResponseHandler } from './anthropic-error';
@@ -50,8 +50,7 @@ export class AnthropicFiles implements FilesV4 {
50
50
  mediaType,
51
51
  filename,
52
52
  }: FilesV4UploadFileCallOptions): Promise<FilesV4UploadFileResult> {
53
- const fileBytes =
54
- data instanceof Uint8Array ? data : convertBase64ToUint8Array(data);
53
+ const fileBytes = convertInlineFileDataToUint8Array(data);
55
54
 
56
55
  const blob = new Blob([fileBytes], { type: mediaType });
57
56
 
@@ -1,61 +1,61 @@
1
1
  import {
2
2
  APICallError,
3
- JSONObject,
4
- LanguageModelV4,
5
- LanguageModelV4CallOptions,
6
- LanguageModelV4Content,
7
- LanguageModelV4FinishReason,
8
- LanguageModelV4FunctionTool,
9
- LanguageModelV4GenerateResult,
10
- LanguageModelV4Prompt,
11
- LanguageModelV4Source,
12
- LanguageModelV4StreamPart,
13
- LanguageModelV4StreamResult,
14
- LanguageModelV4ToolCall,
15
- SharedV4ProviderMetadata,
16
- SharedV4Warning,
3
+ type JSONObject,
4
+ type LanguageModelV4,
5
+ type LanguageModelV4CallOptions,
6
+ type LanguageModelV4Content,
7
+ type LanguageModelV4FinishReason,
8
+ type LanguageModelV4FunctionTool,
9
+ type LanguageModelV4GenerateResult,
10
+ type LanguageModelV4Prompt,
11
+ type LanguageModelV4Source,
12
+ type LanguageModelV4StreamPart,
13
+ type LanguageModelV4StreamResult,
14
+ type LanguageModelV4ToolCall,
15
+ type SharedV4ProviderMetadata,
16
+ type SharedV4Warning,
17
17
  } from '@ai-sdk/provider';
18
18
  import {
19
19
  combineHeaders,
20
20
  createEventSourceResponseHandler,
21
21
  createJsonResponseHandler,
22
22
  createToolNameMapping,
23
- FetchFunction,
24
23
  generateId,
25
- InferSchema,
26
24
  isCustomReasoning,
27
25
  mapReasoningToProviderBudget,
28
26
  mapReasoningToProviderEffort,
29
27
  parseProviderOptions,
30
- ParseResult,
31
28
  postJsonToApi,
32
- Resolvable,
33
29
  resolve,
34
30
  resolveProviderReference,
35
31
  serializeModelOptions,
36
32
  WORKFLOW_SERIALIZE,
37
33
  WORKFLOW_DESERIALIZE,
34
+ type FetchFunction,
35
+ type InferSchema,
36
+ type ParseResult,
37
+ type Resolvable,
38
38
  } from '@ai-sdk/provider-utils';
39
39
  import { anthropicFailedResponseHandler } from './anthropic-error';
40
- import { AnthropicMessageMetadata } from './anthropic-message-metadata';
40
+ import type { AnthropicMessageMetadata } from './anthropic-message-metadata';
41
41
  import {
42
- AnthropicContainer,
43
42
  anthropicChunkSchema,
44
43
  anthropicResponseSchema,
45
- AnthropicReasoningMetadata,
46
- AnthropicResponseContextManagement,
47
- AnthropicTool,
48
- Citation,
44
+ type AnthropicContainer,
45
+ type AnthropicReasoningMetadata,
46
+ type AnthropicResponseContextManagement,
47
+ type AnthropicTool,
48
+ type Citation,
49
49
  } from './anthropic-api';
50
50
  import {
51
- AnthropicModelId,
52
- AnthropicLanguageModelOptions,
53
51
  anthropicLanguageModelOptions,
52
+ type AnthropicModelId,
53
+ type AnthropicLanguageModelOptions,
54
54
  } from './anthropic-options';
55
55
  import { prepareTools } from './anthropic-prepare-tools';
56
56
  import {
57
- AnthropicUsage,
58
57
  convertAnthropicUsage,
58
+ type AnthropicUsage,
59
59
  } from './convert-anthropic-usage';
60
60
  import { convertToAnthropicPrompt } from './convert-to-anthropic-prompt';
61
61
  import { CacheControlValidator } from './get-cache-control';
@@ -1,4 +1,4 @@
1
- import { JSONObject } from '@ai-sdk/provider';
1
+ import type { JSONObject } from '@ai-sdk/provider';
2
2
 
3
3
  /**
4
4
  * Represents a single iteration in the usage breakdown.
@@ -1,9 +1,9 @@
1
1
  import {
2
- LanguageModelV4CallOptions,
3
- SharedV4Warning,
4
2
  UnsupportedFunctionalityError,
3
+ type LanguageModelV4CallOptions,
4
+ type SharedV4Warning,
5
5
  } from '@ai-sdk/provider';
6
- import { AnthropicTool, AnthropicToolChoice } from './anthropic-api';
6
+ import type { AnthropicTool, AnthropicToolChoice } from './anthropic-api';
7
7
  import { CacheControlValidator } from './get-cache-control';
8
8
  import { textEditor_20250728ArgsSchema } from './tool/text-editor_20250728';
9
9
  import { webSearch_20260209ArgsSchema } from './tool/web-search_20260209';
@@ -1,22 +1,22 @@
1
1
  import {
2
- FilesV4,
3
2
  InvalidArgumentError,
4
- LanguageModelV4,
5
3
  NoSuchModelError,
6
- ProviderV4,
7
- SkillsV4,
4
+ type FilesV4,
5
+ type LanguageModelV4,
6
+ type ProviderV4,
7
+ type SkillsV4,
8
8
  } from '@ai-sdk/provider';
9
9
  import {
10
- FetchFunction,
11
10
  generateId,
12
11
  loadApiKey,
13
12
  loadOptionalSetting,
14
13
  withoutTrailingSlash,
15
14
  withUserAgentSuffix,
15
+ type FetchFunction,
16
16
  } from '@ai-sdk/provider-utils';
17
17
  import { AnthropicFiles } from './anthropic-files';
18
18
  import { AnthropicLanguageModel } from './anthropic-language-model';
19
- import { AnthropicModelId } from './anthropic-options';
19
+ import type { AnthropicModelId } from './anthropic-options';
20
20
  import { anthropicTools } from './anthropic-tools';
21
21
  import { AnthropicSkills } from './skills/anthropic-skills';
22
22
  import { VERSION } from './version';
@@ -1,4 +1,4 @@
1
- import { JSONObject, LanguageModelV4Usage } from '@ai-sdk/provider';
1
+ import type { JSONObject, LanguageModelV4Usage } from '@ai-sdk/provider';
2
2
 
3
3
  /**
4
4
  * Represents a single iteration in the usage breakdown.
@@ -1,28 +1,28 @@
1
1
  import {
2
- SharedV4Warning,
3
- LanguageModelV4DataContent,
4
- LanguageModelV4Message,
5
- LanguageModelV4Prompt,
6
- SharedV4ProviderMetadata,
7
2
  UnsupportedFunctionalityError,
3
+ type SharedV4Warning,
4
+ type LanguageModelV4Message,
5
+ type LanguageModelV4Prompt,
6
+ type SharedV4ProviderMetadata,
8
7
  } from '@ai-sdk/provider';
9
8
  import {
10
9
  convertBase64ToUint8Array,
11
10
  convertToBase64,
12
- isProviderReference,
11
+ getTopLevelMediaType,
13
12
  parseProviderOptions,
13
+ resolveFullMediaType,
14
14
  resolveProviderReference,
15
15
  validateTypes,
16
16
  isNonNullable,
17
- ToolNameMapping,
17
+ type ToolNameMapping,
18
18
  } from '@ai-sdk/provider-utils';
19
19
  import {
20
- AnthropicAssistantMessage,
21
- AnthropicPrompt,
22
20
  anthropicReasoningMetadataSchema,
23
- AnthropicToolResultContent,
24
- AnthropicUserMessage,
25
- AnthropicWebFetchToolResultContent,
21
+ type AnthropicAssistantMessage,
22
+ type AnthropicPrompt,
23
+ type AnthropicToolResultContent,
24
+ type AnthropicUserMessage,
25
+ type AnthropicWebFetchToolResultContent,
26
26
  } from './anthropic-api';
27
27
  import { anthropicFilePartProviderOptions } from './anthropic-options';
28
28
  import { CacheControlValidator } from './get-cache-control';
@@ -33,41 +33,11 @@ import { toolSearchRegex_20251119OutputSchema as toolSearchOutputSchema } from '
33
33
  import { webFetch_20250910OutputSchema } from './tool/web-fetch-20250910';
34
34
  import { webSearch_20250305OutputSchema } from './tool/web-search_20250305';
35
35
 
36
- function convertToString(data: LanguageModelV4DataContent): string {
36
+ function convertBytesDataToString(data: Uint8Array | string): string {
37
37
  if (typeof data === 'string') {
38
38
  return new TextDecoder().decode(convertBase64ToUint8Array(data));
39
39
  }
40
-
41
- if (data instanceof Uint8Array) {
42
- return new TextDecoder().decode(data);
43
- }
44
-
45
- if (data instanceof URL) {
46
- throw new UnsupportedFunctionalityError({
47
- functionality: 'URL-based text documents are not supported for citations',
48
- });
49
- }
50
-
51
- throw new UnsupportedFunctionalityError({
52
- functionality: `unsupported data type for text documents: ${typeof data}`,
53
- });
54
- }
55
-
56
- /**
57
- * Checks if data is a URL (either a URL object or a URL string).
58
- */
59
- function isUrlData(
60
- data: LanguageModelV4DataContent,
61
- ): data is URL | (string & { __brand: 'url-string' }) {
62
- return data instanceof URL || isUrlString(data);
63
- }
64
-
65
- function isUrlString(data: LanguageModelV4DataContent): boolean {
66
- return typeof data === 'string' && /^https?:\/\//i.test(data);
67
- }
68
-
69
- function getUrlString(data: LanguageModelV4DataContent): string {
70
- return data instanceof URL ? data.toString() : (data as string);
40
+ return new TextDecoder().decode(data);
71
41
  }
72
42
 
73
43
  export async function convertToAnthropicPrompt({
@@ -185,106 +155,154 @@ export async function convertToAnthropicPrompt({
185
155
  }
186
156
 
187
157
  case 'file': {
188
- if (isProviderReference(part.data)) {
189
- const fileId = resolveProviderReference({
190
- reference: part.data,
191
- provider: 'anthropic',
192
- });
193
- betas.add('files-api-2025-04-14');
194
-
195
- if (part.mediaType.startsWith('image/')) {
196
- anthropicContent.push({
197
- type: 'image',
198
- source: { type: 'file', file_id: fileId },
199
- cache_control: cacheControl,
158
+ switch (part.data.type) {
159
+ case 'reference': {
160
+ const fileId = resolveProviderReference({
161
+ reference: part.data.reference,
162
+ provider: 'anthropic',
200
163
  });
201
- } else {
164
+ betas.add('files-api-2025-04-14');
165
+
166
+ if (getTopLevelMediaType(part.mediaType) === 'image') {
167
+ anthropicContent.push({
168
+ type: 'image',
169
+ source: { type: 'file', file_id: fileId },
170
+ cache_control: cacheControl,
171
+ });
172
+ } else {
173
+ anthropicContent.push({
174
+ type: 'document',
175
+ source: { type: 'file', file_id: fileId },
176
+ cache_control: cacheControl,
177
+ });
178
+ }
179
+ break;
180
+ }
181
+ case 'text': {
182
+ const enableCitations = await shouldEnableCitations(
183
+ part.providerOptions,
184
+ );
185
+
186
+ const metadata = await getDocumentMetadata(
187
+ part.providerOptions,
188
+ );
189
+
202
190
  anthropicContent.push({
203
191
  type: 'document',
204
- source: { type: 'file', file_id: fileId },
192
+ source: {
193
+ type: 'text',
194
+ media_type: 'text/plain',
195
+ data: part.data.text,
196
+ },
197
+ title: metadata.title ?? part.filename,
198
+ ...(metadata.context && {
199
+ context: metadata.context,
200
+ }),
201
+ ...(enableCitations && {
202
+ citations: { enabled: true },
203
+ }),
205
204
  cache_control: cacheControl,
206
205
  });
206
+ break;
207
+ }
208
+ case 'url':
209
+ case 'data': {
210
+ const topLevel = getTopLevelMediaType(part.mediaType);
211
+ if (topLevel === 'image') {
212
+ anthropicContent.push({
213
+ type: 'image',
214
+ source:
215
+ part.data.type === 'url'
216
+ ? {
217
+ type: 'url',
218
+ url: part.data.url.toString(),
219
+ }
220
+ : {
221
+ type: 'base64',
222
+ media_type: resolveFullMediaType({ part }),
223
+ data: convertToBase64(part.data.data),
224
+ },
225
+ cache_control: cacheControl,
226
+ });
227
+ } else if (
228
+ topLevel === 'application' &&
229
+ (part.data.type === 'url'
230
+ ? part.mediaType === 'application/pdf'
231
+ : resolveFullMediaType({ part }) ===
232
+ 'application/pdf')
233
+ ) {
234
+ betas.add('pdfs-2024-09-25');
235
+
236
+ const enableCitations = await shouldEnableCitations(
237
+ part.providerOptions,
238
+ );
239
+
240
+ const metadata = await getDocumentMetadata(
241
+ part.providerOptions,
242
+ );
243
+
244
+ anthropicContent.push({
245
+ type: 'document',
246
+ source:
247
+ part.data.type === 'url'
248
+ ? {
249
+ type: 'url',
250
+ url: part.data.url.toString(),
251
+ }
252
+ : {
253
+ type: 'base64',
254
+ media_type: 'application/pdf',
255
+ data: convertToBase64(part.data.data),
256
+ },
257
+ title: metadata.title ?? part.filename,
258
+ ...(metadata.context && {
259
+ context: metadata.context,
260
+ }),
261
+ ...(enableCitations && {
262
+ citations: { enabled: true },
263
+ }),
264
+ cache_control: cacheControl,
265
+ });
266
+ } else if (part.mediaType === 'text/plain') {
267
+ const enableCitations = await shouldEnableCitations(
268
+ part.providerOptions,
269
+ );
270
+
271
+ const metadata = await getDocumentMetadata(
272
+ part.providerOptions,
273
+ );
274
+
275
+ anthropicContent.push({
276
+ type: 'document',
277
+ source:
278
+ part.data.type === 'url'
279
+ ? {
280
+ type: 'url',
281
+ url: part.data.url.toString(),
282
+ }
283
+ : {
284
+ type: 'text',
285
+ media_type: 'text/plain',
286
+ data: convertBytesDataToString(
287
+ part.data.data,
288
+ ),
289
+ },
290
+ title: metadata.title ?? part.filename,
291
+ ...(metadata.context && {
292
+ context: metadata.context,
293
+ }),
294
+ ...(enableCitations && {
295
+ citations: { enabled: true },
296
+ }),
297
+ cache_control: cacheControl,
298
+ });
299
+ } else {
300
+ throw new UnsupportedFunctionalityError({
301
+ functionality: `media type: ${part.mediaType}`,
302
+ });
303
+ }
304
+ break;
207
305
  }
208
- } else if (part.mediaType.startsWith('image/')) {
209
- anthropicContent.push({
210
- type: 'image',
211
- source: isUrlData(part.data)
212
- ? {
213
- type: 'url',
214
- url: getUrlString(part.data),
215
- }
216
- : {
217
- type: 'base64',
218
- media_type:
219
- part.mediaType === 'image/*'
220
- ? 'image/jpeg'
221
- : part.mediaType,
222
- data: convertToBase64(part.data),
223
- },
224
- cache_control: cacheControl,
225
- });
226
- } else if (part.mediaType === 'application/pdf') {
227
- betas.add('pdfs-2024-09-25');
228
-
229
- const enableCitations = await shouldEnableCitations(
230
- part.providerOptions,
231
- );
232
-
233
- const metadata = await getDocumentMetadata(
234
- part.providerOptions,
235
- );
236
-
237
- anthropicContent.push({
238
- type: 'document',
239
- source: isUrlData(part.data)
240
- ? {
241
- type: 'url',
242
- url: getUrlString(part.data),
243
- }
244
- : {
245
- type: 'base64',
246
- media_type: 'application/pdf',
247
- data: convertToBase64(part.data),
248
- },
249
- title: metadata.title ?? part.filename,
250
- ...(metadata.context && { context: metadata.context }),
251
- ...(enableCitations && {
252
- citations: { enabled: true },
253
- }),
254
- cache_control: cacheControl,
255
- });
256
- } else if (part.mediaType === 'text/plain') {
257
- const enableCitations = await shouldEnableCitations(
258
- part.providerOptions,
259
- );
260
-
261
- const metadata = await getDocumentMetadata(
262
- part.providerOptions,
263
- );
264
-
265
- anthropicContent.push({
266
- type: 'document',
267
- source: isUrlData(part.data)
268
- ? {
269
- type: 'url',
270
- url: getUrlString(part.data),
271
- }
272
- : {
273
- type: 'text',
274
- media_type: 'text/plain',
275
- data: convertToString(part.data),
276
- },
277
- title: metadata.title ?? part.filename,
278
- ...(metadata.context && { context: metadata.context }),
279
- ...(enableCitations && {
280
- citations: { enabled: true },
281
- }),
282
- cache_control: cacheControl,
283
- });
284
- } else {
285
- throw new UnsupportedFunctionalityError({
286
- functionality: `media type: ${part.mediaType}`,
287
- });
288
306
  }
289
307
 
290
308
  break;
@@ -1,5 +1,5 @@
1
- import { JSONObject } from '@ai-sdk/provider';
2
- import { AnthropicMessageMetadata } from './anthropic-message-metadata';
1
+ import type { JSONObject } from '@ai-sdk/provider';
2
+ import type { AnthropicMessageMetadata } from './anthropic-message-metadata';
3
3
 
4
4
  /**
5
5
  * Sets the Anthropic container ID in the provider options based on
@@ -1,5 +1,8 @@
1
- import { SharedV4Warning, SharedV4ProviderMetadata } from '@ai-sdk/provider';
2
- import { AnthropicCacheControl } from './anthropic-api';
1
+ import type {
2
+ SharedV4Warning,
3
+ SharedV4ProviderMetadata,
4
+ } from '@ai-sdk/provider';
5
+ import type { AnthropicCacheControl } from './anthropic-api';
3
6
 
4
7
  // Anthropic allows a maximum of 4 cache breakpoints per request
5
8
  const MAX_CACHE_BREAKPOINTS = 4;
@@ -1,4 +1,4 @@
1
- import { LanguageModelV4FinishReason } from '@ai-sdk/provider';
1
+ import type { LanguageModelV4FinishReason } from '@ai-sdk/provider';
2
2
 
3
3
  /**
4
4
  * @see https://docs.anthropic.com/en/api/messages#response-stop-reason
@@ -1,13 +1,13 @@
1
- import { SkillsV4, SharedV4Warning } from '@ai-sdk/provider';
1
+ import type { SkillsV4, SharedV4Warning } from '@ai-sdk/provider';
2
2
  import {
3
3
  combineHeaders,
4
- convertBase64ToUint8Array,
4
+ convertInlineFileDataToUint8Array,
5
5
  createJsonResponseHandler,
6
- FetchFunction,
7
6
  getFromApi,
8
7
  postFormDataToApi,
9
- Resolvable,
10
8
  resolve,
9
+ type FetchFunction,
10
+ type Resolvable,
11
11
  } from '@ai-sdk/provider-utils';
12
12
  import { anthropicFailedResponseHandler } from '../anthropic-error';
13
13
  import {
@@ -76,11 +76,7 @@ export class AnthropicSkills implements SkillsV4 {
76
76
  }
77
77
 
78
78
  for (const file of params.files) {
79
- const content =
80
- typeof file.content === 'string'
81
- ? convertBase64ToUint8Array(file.content)
82
- : file.content;
83
-
79
+ const content = convertInlineFileDataToUint8Array(file.data);
84
80
  formData.append('files[]', new Blob([content]), file.path);
85
81
  }
86
82
 
@@ -1,6 +1,9 @@
1
- import { createProviderDefinedToolFactory } from '@ai-sdk/provider-utils';
1
+ import {
2
+ createProviderDefinedToolFactory,
3
+ lazySchema,
4
+ zodSchema,
5
+ } from '@ai-sdk/provider-utils';
2
6
  import { z } from 'zod/v4';
3
- import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
4
7
 
5
8
  export const textEditor_20250728ArgsSchema = lazySchema(() =>
6
9
  zodSchema(