@ai-sdk/anthropic 3.0.72 → 3.0.74
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.
- package/CHANGELOG.md +17 -0
- package/dist/index.js +288 -143
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +157 -5
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +281 -136
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +156 -4
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/anthropic-error.ts +1 -1
- package/src/anthropic-message-metadata.ts +1 -1
- package/src/anthropic-messages-api.ts +6 -2
- package/src/anthropic-messages-language-model.ts +28 -27
- package/src/anthropic-prepare-tools.ts +6 -3
- package/src/anthropic-provider.ts +4 -4
- package/src/convert-anthropic-messages-usage.ts +1 -1
- package/src/convert-to-anthropic-messages-prompt.ts +11 -11
- package/src/forward-anthropic-container-id-from-last-step.ts +2 -2
- package/src/get-cache-control.ts +5 -2
- package/src/map-anthropic-stop-reason.ts +1 -1
- package/src/sanitize-json-schema.ts +203 -0
- package/src/tool/text-editor_20250728.ts +5 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/anthropic",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.74",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -36,15 +36,15 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@ai-sdk/provider": "3.0.
|
|
40
|
-
"@ai-sdk/provider-utils": "4.0.
|
|
39
|
+
"@ai-sdk/provider": "3.0.10",
|
|
40
|
+
"@ai-sdk/provider-utils": "4.0.26"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "20.17.24",
|
|
44
44
|
"tsup": "^8",
|
|
45
45
|
"typescript": "5.8.3",
|
|
46
46
|
"zod": "3.25.76",
|
|
47
|
-
"@ai-sdk/test-server": "1.0.
|
|
47
|
+
"@ai-sdk/test-server": "1.0.5",
|
|
48
48
|
"@vercel/ai-tsconfig": "0.0.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
package/src/anthropic-error.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import { JSONSchema7 } from '@ai-sdk/provider';
|
|
2
|
-
import {
|
|
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 AnthropicMessagesPrompt = {
|
|
@@ -1,57 +1,58 @@
|
|
|
1
1
|
import {
|
|
2
2
|
APICallError,
|
|
3
|
-
JSONObject,
|
|
4
|
-
LanguageModelV3,
|
|
5
|
-
LanguageModelV3CallOptions,
|
|
6
|
-
LanguageModelV3Content,
|
|
7
|
-
LanguageModelV3FinishReason,
|
|
8
|
-
LanguageModelV3FunctionTool,
|
|
9
|
-
LanguageModelV3GenerateResult,
|
|
10
|
-
LanguageModelV3Prompt,
|
|
11
|
-
LanguageModelV3Source,
|
|
12
|
-
LanguageModelV3StreamPart,
|
|
13
|
-
LanguageModelV3StreamResult,
|
|
14
|
-
LanguageModelV3ToolCall,
|
|
15
|
-
SharedV3ProviderMetadata,
|
|
16
|
-
SharedV3Warning,
|
|
3
|
+
type JSONObject,
|
|
4
|
+
type LanguageModelV3,
|
|
5
|
+
type LanguageModelV3CallOptions,
|
|
6
|
+
type LanguageModelV3Content,
|
|
7
|
+
type LanguageModelV3FinishReason,
|
|
8
|
+
type LanguageModelV3FunctionTool,
|
|
9
|
+
type LanguageModelV3GenerateResult,
|
|
10
|
+
type LanguageModelV3Prompt,
|
|
11
|
+
type LanguageModelV3Source,
|
|
12
|
+
type LanguageModelV3StreamPart,
|
|
13
|
+
type LanguageModelV3StreamResult,
|
|
14
|
+
type LanguageModelV3ToolCall,
|
|
15
|
+
type SharedV3ProviderMetadata,
|
|
16
|
+
type SharedV3Warning,
|
|
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
|
parseProviderOptions,
|
|
27
|
-
ParseResult,
|
|
28
25
|
postJsonToApi,
|
|
29
|
-
Resolvable,
|
|
30
26
|
resolve,
|
|
27
|
+
type FetchFunction,
|
|
28
|
+
type InferSchema,
|
|
29
|
+
type ParseResult,
|
|
30
|
+
type Resolvable,
|
|
31
31
|
} from '@ai-sdk/provider-utils';
|
|
32
32
|
import { anthropicFailedResponseHandler } from './anthropic-error';
|
|
33
|
-
import { AnthropicMessageMetadata } from './anthropic-message-metadata';
|
|
33
|
+
import type { AnthropicMessageMetadata } from './anthropic-message-metadata';
|
|
34
34
|
import {
|
|
35
|
-
AnthropicContainer,
|
|
36
35
|
anthropicMessagesChunkSchema,
|
|
37
36
|
anthropicMessagesResponseSchema,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
type AnthropicContainer,
|
|
38
|
+
type AnthropicReasoningMetadata,
|
|
39
|
+
type AnthropicResponseContextManagement,
|
|
40
|
+
type AnthropicTool,
|
|
41
|
+
type Citation,
|
|
42
42
|
} from './anthropic-messages-api';
|
|
43
43
|
import {
|
|
44
|
-
AnthropicMessagesModelId,
|
|
45
44
|
anthropicLanguageModelOptions,
|
|
45
|
+
type AnthropicMessagesModelId,
|
|
46
46
|
} from './anthropic-messages-options';
|
|
47
47
|
import { prepareTools } from './anthropic-prepare-tools';
|
|
48
48
|
import {
|
|
49
|
-
AnthropicMessagesUsage,
|
|
50
49
|
convertAnthropicMessagesUsage,
|
|
50
|
+
type AnthropicMessagesUsage,
|
|
51
51
|
} from './convert-anthropic-messages-usage';
|
|
52
52
|
import { convertToAnthropicMessagesPrompt } from './convert-to-anthropic-messages-prompt';
|
|
53
53
|
import { CacheControlValidator } from './get-cache-control';
|
|
54
54
|
import { mapAnthropicStopReason } from './map-anthropic-stop-reason';
|
|
55
|
+
import { sanitizeJsonSchema } from './sanitize-json-schema';
|
|
55
56
|
|
|
56
57
|
function createCitationSource(
|
|
57
58
|
citation: Citation,
|
|
@@ -422,7 +423,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
422
423
|
responseFormat.schema != null && {
|
|
423
424
|
format: {
|
|
424
425
|
type: 'json_schema',
|
|
425
|
-
schema: responseFormat.schema,
|
|
426
|
+
schema: sanitizeJsonSchema(responseFormat.schema),
|
|
426
427
|
},
|
|
427
428
|
}),
|
|
428
429
|
},
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
|
-
LanguageModelV3CallOptions,
|
|
3
|
-
SharedV3Warning,
|
|
4
2
|
UnsupportedFunctionalityError,
|
|
3
|
+
type LanguageModelV3CallOptions,
|
|
4
|
+
type SharedV3Warning,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
|
-
import {
|
|
6
|
+
import type {
|
|
7
|
+
AnthropicTool,
|
|
8
|
+
AnthropicToolChoice,
|
|
9
|
+
} from './anthropic-messages-api';
|
|
7
10
|
import { CacheControlValidator } from './get-cache-control';
|
|
8
11
|
import { textEditor_20250728ArgsSchema } from './tool/text-editor_20250728';
|
|
9
12
|
import { webSearch_20260209ArgsSchema } from './tool/web-search_20260209';
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import {
|
|
2
2
|
InvalidArgumentError,
|
|
3
|
-
LanguageModelV3,
|
|
4
3
|
NoSuchModelError,
|
|
5
|
-
|
|
4
|
+
type LanguageModelV3,
|
|
5
|
+
type ProviderV3,
|
|
6
6
|
} from '@ai-sdk/provider';
|
|
7
7
|
import {
|
|
8
|
-
FetchFunction,
|
|
9
8
|
generateId,
|
|
10
9
|
loadApiKey,
|
|
11
10
|
loadOptionalSetting,
|
|
12
11
|
withoutTrailingSlash,
|
|
13
12
|
withUserAgentSuffix,
|
|
13
|
+
type FetchFunction,
|
|
14
14
|
} from '@ai-sdk/provider-utils';
|
|
15
15
|
import { VERSION } from './version';
|
|
16
16
|
import { AnthropicMessagesLanguageModel } from './anthropic-messages-language-model';
|
|
17
|
-
import { AnthropicMessagesModelId } from './anthropic-messages-options';
|
|
17
|
+
import type { AnthropicMessagesModelId } from './anthropic-messages-options';
|
|
18
18
|
import { anthropicTools } from './anthropic-tools';
|
|
19
19
|
|
|
20
20
|
export interface AnthropicProvider extends ProviderV3 {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
SharedV3Warning,
|
|
3
|
-
LanguageModelV3DataContent,
|
|
4
|
-
LanguageModelV3Message,
|
|
5
|
-
LanguageModelV3Prompt,
|
|
6
|
-
SharedV3ProviderMetadata,
|
|
7
2
|
UnsupportedFunctionalityError,
|
|
3
|
+
type SharedV3Warning,
|
|
4
|
+
type LanguageModelV3DataContent,
|
|
5
|
+
type LanguageModelV3Message,
|
|
6
|
+
type LanguageModelV3Prompt,
|
|
7
|
+
type SharedV3ProviderMetadata,
|
|
8
8
|
} from '@ai-sdk/provider';
|
|
9
9
|
import {
|
|
10
10
|
convertBase64ToUint8Array,
|
|
@@ -12,15 +12,15 @@ import {
|
|
|
12
12
|
parseProviderOptions,
|
|
13
13
|
validateTypes,
|
|
14
14
|
isNonNullable,
|
|
15
|
-
ToolNameMapping,
|
|
15
|
+
type ToolNameMapping,
|
|
16
16
|
} from '@ai-sdk/provider-utils';
|
|
17
17
|
import {
|
|
18
|
-
AnthropicAssistantMessage,
|
|
19
|
-
AnthropicMessagesPrompt,
|
|
20
18
|
anthropicReasoningMetadataSchema,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
type AnthropicAssistantMessage,
|
|
20
|
+
type AnthropicMessagesPrompt,
|
|
21
|
+
type AnthropicToolResultContent,
|
|
22
|
+
type AnthropicUserMessage,
|
|
23
|
+
type AnthropicWebFetchToolResultContent,
|
|
24
24
|
} from './anthropic-messages-api';
|
|
25
25
|
import { anthropicFilePartProviderOptions } from './anthropic-messages-options';
|
|
26
26
|
import { CacheControlValidator } from './get-cache-control';
|
|
@@ -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
|
package/src/get-cache-control.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import type {
|
|
2
|
+
SharedV3Warning,
|
|
3
|
+
SharedV3ProviderMetadata,
|
|
4
|
+
} from '@ai-sdk/provider';
|
|
5
|
+
import type { AnthropicCacheControl } from './anthropic-messages-api';
|
|
3
6
|
|
|
4
7
|
// Anthropic allows a maximum of 4 cache breakpoints per request
|
|
5
8
|
const MAX_CACHE_BREAKPOINTS = 4;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import type { JSONSchema7, JSONSchema7Definition } from '@ai-sdk/provider';
|
|
2
|
+
|
|
3
|
+
const SUPPORTED_STRING_FORMATS = new Set([
|
|
4
|
+
'date-time',
|
|
5
|
+
'time',
|
|
6
|
+
'date',
|
|
7
|
+
'duration',
|
|
8
|
+
'email',
|
|
9
|
+
'hostname',
|
|
10
|
+
'uri',
|
|
11
|
+
'ipv4',
|
|
12
|
+
'ipv6',
|
|
13
|
+
'uuid',
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
const DESCRIPTION_CONSTRAINT_KEYS = [
|
|
17
|
+
'minimum',
|
|
18
|
+
'maximum',
|
|
19
|
+
'exclusiveMinimum',
|
|
20
|
+
'exclusiveMaximum',
|
|
21
|
+
'multipleOf',
|
|
22
|
+
'minLength',
|
|
23
|
+
'maxLength',
|
|
24
|
+
'pattern',
|
|
25
|
+
'minItems',
|
|
26
|
+
'maxItems',
|
|
27
|
+
'uniqueItems',
|
|
28
|
+
'minProperties',
|
|
29
|
+
'maxProperties',
|
|
30
|
+
'not',
|
|
31
|
+
] satisfies Array<keyof JSONSchema7>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Removes JSON Schema keywords that Anthropic rejects in
|
|
35
|
+
* `output_config.format.schema`.
|
|
36
|
+
*
|
|
37
|
+
* The full original schema is still used by AI SDK result validation. This
|
|
38
|
+
* only relaxes the schema sent to Anthropic's constrained decoder.
|
|
39
|
+
*/
|
|
40
|
+
export function sanitizeJsonSchema(schema: JSONSchema7): JSONSchema7 {
|
|
41
|
+
return sanitizeSchema(schema) as JSONSchema7;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function sanitizeDefinition(
|
|
45
|
+
definition: JSONSchema7Definition,
|
|
46
|
+
): JSONSchema7Definition {
|
|
47
|
+
if (typeof definition === 'boolean' || !isPlainObject(definition)) {
|
|
48
|
+
return definition;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return sanitizeSchema(definition as JSONSchema7);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function sanitizeSchema(schema: JSONSchema7): JSONSchema7 {
|
|
55
|
+
const result: JSONSchema7 = {};
|
|
56
|
+
const schemaWithDefs = schema as JSONSchema7 & {
|
|
57
|
+
$defs?: Record<string, JSONSchema7Definition>;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
if (schema.$ref != null) {
|
|
61
|
+
return { $ref: schema.$ref };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (schema.$schema != null) {
|
|
65
|
+
result.$schema = schema.$schema;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (schema.$id != null) {
|
|
69
|
+
result.$id = schema.$id;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (schema.title != null) {
|
|
73
|
+
result.title = schema.title;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (schema.description != null) {
|
|
77
|
+
result.description = schema.description;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (schema.default !== undefined) {
|
|
81
|
+
result.default = schema.default;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (schema.const !== undefined) {
|
|
85
|
+
result.const = schema.const;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (schema.enum != null) {
|
|
89
|
+
result.enum = schema.enum;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (schema.type != null) {
|
|
93
|
+
result.type = schema.type;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (schema.anyOf != null) {
|
|
97
|
+
result.anyOf = schema.anyOf.map(sanitizeDefinition);
|
|
98
|
+
} else if (schema.oneOf != null) {
|
|
99
|
+
result.anyOf = schema.oneOf.map(sanitizeDefinition);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (schema.allOf != null) {
|
|
103
|
+
result.allOf = schema.allOf.map(sanitizeDefinition);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (schema.definitions != null) {
|
|
107
|
+
result.definitions = Object.fromEntries(
|
|
108
|
+
Object.entries(schema.definitions).map(([name, definition]) => [
|
|
109
|
+
name,
|
|
110
|
+
sanitizeDefinition(definition),
|
|
111
|
+
]),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (schemaWithDefs.$defs != null) {
|
|
116
|
+
const resultWithDefs = result as JSONSchema7 & {
|
|
117
|
+
$defs?: Record<string, JSONSchema7Definition>;
|
|
118
|
+
};
|
|
119
|
+
resultWithDefs.$defs = Object.fromEntries(
|
|
120
|
+
Object.entries(schemaWithDefs.$defs).map(([name, definition]) => [
|
|
121
|
+
name,
|
|
122
|
+
sanitizeDefinition(definition),
|
|
123
|
+
]),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (schema.type === 'object' || schema.properties != null) {
|
|
128
|
+
if (schema.properties != null) {
|
|
129
|
+
result.properties = Object.fromEntries(
|
|
130
|
+
Object.entries(schema.properties).map(([name, definition]) => [
|
|
131
|
+
name,
|
|
132
|
+
sanitizeDefinition(definition),
|
|
133
|
+
]),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
result.additionalProperties = false;
|
|
138
|
+
|
|
139
|
+
if (schema.required != null) {
|
|
140
|
+
result.required = schema.required;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (schema.items != null) {
|
|
145
|
+
result.items = Array.isArray(schema.items)
|
|
146
|
+
? schema.items.map(sanitizeDefinition)
|
|
147
|
+
: sanitizeDefinition(schema.items);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (
|
|
151
|
+
typeof schema.format === 'string' &&
|
|
152
|
+
SUPPORTED_STRING_FORMATS.has(schema.format)
|
|
153
|
+
) {
|
|
154
|
+
result.format = schema.format;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const constraintDescription = getConstraintDescription(schema);
|
|
158
|
+
if (constraintDescription != null) {
|
|
159
|
+
result.description =
|
|
160
|
+
result.description == null
|
|
161
|
+
? constraintDescription
|
|
162
|
+
: `${result.description}\n${constraintDescription}`;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function getConstraintDescription(schema: JSONSchema7): string | undefined {
|
|
169
|
+
const descriptions = DESCRIPTION_CONSTRAINT_KEYS.flatMap(key => {
|
|
170
|
+
const value = schema[key];
|
|
171
|
+
|
|
172
|
+
if (value == null || value === false) {
|
|
173
|
+
return [];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return `${formatConstraintName(key)}: ${formatConstraintValue(value)}`;
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
if (
|
|
180
|
+
typeof schema.format === 'string' &&
|
|
181
|
+
!SUPPORTED_STRING_FORMATS.has(schema.format)
|
|
182
|
+
) {
|
|
183
|
+
descriptions.push(`format: ${schema.format}`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return descriptions.length === 0 ? undefined : `${descriptions.join('; ')}.`;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function formatConstraintName(key: string): string {
|
|
190
|
+
return key.replace(/[A-Z]/g, match => ` ${match.toLowerCase()}`);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function formatConstraintValue(value: unknown): string {
|
|
194
|
+
if (typeof value === 'string') {
|
|
195
|
+
return value;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return JSON.stringify(value);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
202
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
203
|
+
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createProviderToolFactory,
|
|
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(
|