@ai-sdk/anthropic 4.0.0-beta.2 → 4.0.0-beta.4
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 +12 -0
- package/dist/index.d.mts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +28 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +25 -14
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +22 -13
- package/dist/internal/index.d.ts +22 -13
- package/dist/internal/index.js +26 -15
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +23 -12
- package/dist/internal/index.mjs.map +1 -1
- package/docs/05-anthropic.mdx +45 -4
- package/package.json +3 -3
- package/src/anthropic-messages-language-model.ts +46 -34
- package/src/anthropic-prepare-tools.ts +22 -8
- package/src/anthropic-provider.ts +8 -8
- package/src/convert-anthropic-messages-usage.ts +2 -2
- package/src/convert-to-anthropic-messages-prompt.ts +17 -17
- package/src/get-cache-control.ts +5 -5
- package/src/map-anthropic-stop-reason.ts +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4, LanguageModelV4CallOptions, LanguageModelV4GenerateResult, LanguageModelV4StreamResult, JSONSchema7, SharedV4ProviderMetadata, SharedV4Warning } from '@ai-sdk/provider';
|
|
2
2
|
import * as _ai_sdk_provider_utils from '@ai-sdk/provider-utils';
|
|
3
3
|
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
|
|
4
4
|
|
|
@@ -11,15 +11,20 @@ type AnthropicMessagesConfig = {
|
|
|
11
11
|
fetch?: FetchFunction;
|
|
12
12
|
buildRequestUrl?: (baseURL: string, isStreaming: boolean) => string;
|
|
13
13
|
transformRequestBody?: (args: Record<string, any>, betas: Set<string>) => Record<string, any>;
|
|
14
|
-
supportedUrls?: () =>
|
|
14
|
+
supportedUrls?: () => LanguageModelV4['supportedUrls'];
|
|
15
15
|
generateId?: () => string;
|
|
16
16
|
/**
|
|
17
17
|
* When false, the model will use JSON tool fallback for structured outputs.
|
|
18
18
|
*/
|
|
19
19
|
supportsNativeStructuredOutput?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* When false, `strict` on tool definitions will be ignored and a warning emitted.
|
|
22
|
+
* Defaults to true.
|
|
23
|
+
*/
|
|
24
|
+
supportsStrictTools?: boolean;
|
|
20
25
|
};
|
|
21
|
-
declare class AnthropicMessagesLanguageModel implements
|
|
22
|
-
readonly specificationVersion = "
|
|
26
|
+
declare class AnthropicMessagesLanguageModel implements LanguageModelV4 {
|
|
27
|
+
readonly specificationVersion = "v4";
|
|
23
28
|
readonly modelId: AnthropicMessagesModelId;
|
|
24
29
|
private readonly config;
|
|
25
30
|
private readonly generateId;
|
|
@@ -38,8 +43,8 @@ declare class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
38
43
|
private buildRequestUrl;
|
|
39
44
|
private transformRequestBody;
|
|
40
45
|
private extractCitationDocuments;
|
|
41
|
-
doGenerate(options:
|
|
42
|
-
doStream(options:
|
|
46
|
+
doGenerate(options: LanguageModelV4CallOptions): Promise<LanguageModelV4GenerateResult>;
|
|
47
|
+
doStream(options: LanguageModelV4CallOptions): Promise<LanguageModelV4StreamResult>;
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
declare const anthropicTools: {
|
|
@@ -934,26 +939,30 @@ type AnthropicToolChoice = {
|
|
|
934
939
|
declare class CacheControlValidator {
|
|
935
940
|
private breakpointCount;
|
|
936
941
|
private warnings;
|
|
937
|
-
getCacheControl(providerMetadata:
|
|
942
|
+
getCacheControl(providerMetadata: SharedV4ProviderMetadata | undefined, context: {
|
|
938
943
|
type: string;
|
|
939
944
|
canCache: boolean;
|
|
940
945
|
}): AnthropicCacheControl | undefined;
|
|
941
|
-
getWarnings():
|
|
946
|
+
getWarnings(): SharedV4Warning[];
|
|
942
947
|
}
|
|
943
948
|
|
|
944
|
-
declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cacheControlValidator, supportsStructuredOutput, }: {
|
|
945
|
-
tools:
|
|
946
|
-
toolChoice:
|
|
949
|
+
declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cacheControlValidator, supportsStructuredOutput, supportsStrictTools, }: {
|
|
950
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
951
|
+
toolChoice: LanguageModelV4CallOptions['toolChoice'] | undefined;
|
|
947
952
|
disableParallelToolUse?: boolean;
|
|
948
953
|
cacheControlValidator?: CacheControlValidator;
|
|
949
954
|
/**
|
|
950
|
-
* Whether the model supports structured output.
|
|
955
|
+
* Whether the model supports native structured output response format.
|
|
951
956
|
*/
|
|
952
957
|
supportsStructuredOutput: boolean;
|
|
958
|
+
/**
|
|
959
|
+
* Whether the model supports strict mode on tool definitions.
|
|
960
|
+
*/
|
|
961
|
+
supportsStrictTools: boolean;
|
|
953
962
|
}): Promise<{
|
|
954
963
|
tools: Array<AnthropicTool> | undefined;
|
|
955
964
|
toolChoice: AnthropicToolChoice | undefined;
|
|
956
|
-
toolWarnings:
|
|
965
|
+
toolWarnings: SharedV4Warning[];
|
|
957
966
|
betas: Set<string>;
|
|
958
967
|
}>;
|
|
959
968
|
|
package/dist/internal/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4, LanguageModelV4CallOptions, LanguageModelV4GenerateResult, LanguageModelV4StreamResult, JSONSchema7, SharedV4ProviderMetadata, SharedV4Warning } from '@ai-sdk/provider';
|
|
2
2
|
import * as _ai_sdk_provider_utils from '@ai-sdk/provider-utils';
|
|
3
3
|
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
|
|
4
4
|
|
|
@@ -11,15 +11,20 @@ type AnthropicMessagesConfig = {
|
|
|
11
11
|
fetch?: FetchFunction;
|
|
12
12
|
buildRequestUrl?: (baseURL: string, isStreaming: boolean) => string;
|
|
13
13
|
transformRequestBody?: (args: Record<string, any>, betas: Set<string>) => Record<string, any>;
|
|
14
|
-
supportedUrls?: () =>
|
|
14
|
+
supportedUrls?: () => LanguageModelV4['supportedUrls'];
|
|
15
15
|
generateId?: () => string;
|
|
16
16
|
/**
|
|
17
17
|
* When false, the model will use JSON tool fallback for structured outputs.
|
|
18
18
|
*/
|
|
19
19
|
supportsNativeStructuredOutput?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* When false, `strict` on tool definitions will be ignored and a warning emitted.
|
|
22
|
+
* Defaults to true.
|
|
23
|
+
*/
|
|
24
|
+
supportsStrictTools?: boolean;
|
|
20
25
|
};
|
|
21
|
-
declare class AnthropicMessagesLanguageModel implements
|
|
22
|
-
readonly specificationVersion = "
|
|
26
|
+
declare class AnthropicMessagesLanguageModel implements LanguageModelV4 {
|
|
27
|
+
readonly specificationVersion = "v4";
|
|
23
28
|
readonly modelId: AnthropicMessagesModelId;
|
|
24
29
|
private readonly config;
|
|
25
30
|
private readonly generateId;
|
|
@@ -38,8 +43,8 @@ declare class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
38
43
|
private buildRequestUrl;
|
|
39
44
|
private transformRequestBody;
|
|
40
45
|
private extractCitationDocuments;
|
|
41
|
-
doGenerate(options:
|
|
42
|
-
doStream(options:
|
|
46
|
+
doGenerate(options: LanguageModelV4CallOptions): Promise<LanguageModelV4GenerateResult>;
|
|
47
|
+
doStream(options: LanguageModelV4CallOptions): Promise<LanguageModelV4StreamResult>;
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
declare const anthropicTools: {
|
|
@@ -934,26 +939,30 @@ type AnthropicToolChoice = {
|
|
|
934
939
|
declare class CacheControlValidator {
|
|
935
940
|
private breakpointCount;
|
|
936
941
|
private warnings;
|
|
937
|
-
getCacheControl(providerMetadata:
|
|
942
|
+
getCacheControl(providerMetadata: SharedV4ProviderMetadata | undefined, context: {
|
|
938
943
|
type: string;
|
|
939
944
|
canCache: boolean;
|
|
940
945
|
}): AnthropicCacheControl | undefined;
|
|
941
|
-
getWarnings():
|
|
946
|
+
getWarnings(): SharedV4Warning[];
|
|
942
947
|
}
|
|
943
948
|
|
|
944
|
-
declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cacheControlValidator, supportsStructuredOutput, }: {
|
|
945
|
-
tools:
|
|
946
|
-
toolChoice:
|
|
949
|
+
declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cacheControlValidator, supportsStructuredOutput, supportsStrictTools, }: {
|
|
950
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
951
|
+
toolChoice: LanguageModelV4CallOptions['toolChoice'] | undefined;
|
|
947
952
|
disableParallelToolUse?: boolean;
|
|
948
953
|
cacheControlValidator?: CacheControlValidator;
|
|
949
954
|
/**
|
|
950
|
-
* Whether the model supports structured output.
|
|
955
|
+
* Whether the model supports native structured output response format.
|
|
951
956
|
*/
|
|
952
957
|
supportsStructuredOutput: boolean;
|
|
958
|
+
/**
|
|
959
|
+
* Whether the model supports strict mode on tool definitions.
|
|
960
|
+
*/
|
|
961
|
+
supportsStrictTools: boolean;
|
|
953
962
|
}): Promise<{
|
|
954
963
|
tools: Array<AnthropicTool> | undefined;
|
|
955
964
|
toolChoice: AnthropicToolChoice | undefined;
|
|
956
|
-
toolWarnings:
|
|
965
|
+
toolWarnings: SharedV4Warning[];
|
|
957
966
|
betas: Set<string>;
|
|
958
967
|
}>;
|
|
959
968
|
|
package/dist/internal/index.js
CHANGED
|
@@ -18,13 +18,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
20
|
// src/internal/index.ts
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
23
|
AnthropicMessagesLanguageModel: () => AnthropicMessagesLanguageModel,
|
|
24
24
|
anthropicTools: () => anthropicTools,
|
|
25
25
|
prepareTools: () => prepareTools
|
|
26
26
|
});
|
|
27
|
-
module.exports = __toCommonJS(
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
28
|
|
|
29
29
|
// src/anthropic-messages-language-model.ts
|
|
30
30
|
var import_provider3 = require("@ai-sdk/provider");
|
|
@@ -1261,7 +1261,8 @@ async function prepareTools({
|
|
|
1261
1261
|
toolChoice,
|
|
1262
1262
|
disableParallelToolUse,
|
|
1263
1263
|
cacheControlValidator,
|
|
1264
|
-
supportsStructuredOutput
|
|
1264
|
+
supportsStructuredOutput,
|
|
1265
|
+
supportsStrictTools
|
|
1265
1266
|
}) {
|
|
1266
1267
|
var _a;
|
|
1267
1268
|
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
@@ -1283,13 +1284,20 @@ async function prepareTools({
|
|
|
1283
1284
|
const eagerInputStreaming = anthropicOptions == null ? void 0 : anthropicOptions.eagerInputStreaming;
|
|
1284
1285
|
const deferLoading = anthropicOptions == null ? void 0 : anthropicOptions.deferLoading;
|
|
1285
1286
|
const allowedCallers = anthropicOptions == null ? void 0 : anthropicOptions.allowedCallers;
|
|
1287
|
+
if (!supportsStrictTools && tool.strict != null) {
|
|
1288
|
+
toolWarnings.push({
|
|
1289
|
+
type: "unsupported",
|
|
1290
|
+
feature: "strict",
|
|
1291
|
+
details: `Tool '${tool.name}' has strict: ${tool.strict}, but strict mode is not supported by this provider. The strict property will be ignored.`
|
|
1292
|
+
});
|
|
1293
|
+
}
|
|
1286
1294
|
anthropicTools2.push({
|
|
1287
1295
|
name: tool.name,
|
|
1288
1296
|
description: tool.description,
|
|
1289
1297
|
input_schema: tool.inputSchema,
|
|
1290
1298
|
cache_control: cacheControl,
|
|
1291
1299
|
...eagerInputStreaming ? { eager_input_streaming: true } : {},
|
|
1292
|
-
...
|
|
1300
|
+
...supportsStrictTools === true && tool.strict != null ? { strict: tool.strict } : {},
|
|
1293
1301
|
...deferLoading != null ? { defer_loading: deferLoading } : {},
|
|
1294
1302
|
...allowedCallers != null ? { allowed_callers: allowedCallers } : {},
|
|
1295
1303
|
...tool.inputExamples != null ? {
|
|
@@ -2822,7 +2830,7 @@ function createCitationSource(citation, citationDocuments, generateId2) {
|
|
|
2822
2830
|
}
|
|
2823
2831
|
var AnthropicMessagesLanguageModel = class {
|
|
2824
2832
|
constructor(modelId, config) {
|
|
2825
|
-
this.specificationVersion = "
|
|
2833
|
+
this.specificationVersion = "v4";
|
|
2826
2834
|
var _a;
|
|
2827
2835
|
this.modelId = modelId;
|
|
2828
2836
|
this.config = config;
|
|
@@ -2864,7 +2872,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
2864
2872
|
providerOptions,
|
|
2865
2873
|
stream
|
|
2866
2874
|
}) {
|
|
2867
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
2875
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2868
2876
|
const warnings = [];
|
|
2869
2877
|
if (frequencyPenalty != null) {
|
|
2870
2878
|
warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
|
|
@@ -2922,7 +2930,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
2922
2930
|
isKnownModel
|
|
2923
2931
|
} = getModelCapabilities(this.modelId);
|
|
2924
2932
|
const supportsStructuredOutput = ((_a = this.config.supportsNativeStructuredOutput) != null ? _a : true) && modelSupportsStructuredOutput;
|
|
2925
|
-
const
|
|
2933
|
+
const supportsStrictTools = ((_b = this.config.supportsStrictTools) != null ? _b : true) && modelSupportsStructuredOutput;
|
|
2934
|
+
const structureOutputMode = (_c = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _c : "auto";
|
|
2926
2935
|
const useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
|
|
2927
2936
|
const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useStructuredOutput ? {
|
|
2928
2937
|
type: "function",
|
|
@@ -2957,14 +2966,14 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
2957
2966
|
});
|
|
2958
2967
|
const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
|
|
2959
2968
|
prompt,
|
|
2960
|
-
sendReasoning: (
|
|
2969
|
+
sendReasoning: (_d = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _d : true,
|
|
2961
2970
|
warnings,
|
|
2962
2971
|
cacheControlValidator,
|
|
2963
2972
|
toolNameMapping
|
|
2964
2973
|
});
|
|
2965
|
-
const thinkingType = (
|
|
2974
|
+
const thinkingType = (_e = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _e.type;
|
|
2966
2975
|
const isThinking = thinkingType === "enabled" || thinkingType === "adaptive";
|
|
2967
|
-
let thinkingBudget = thinkingType === "enabled" ? (
|
|
2976
|
+
let thinkingBudget = thinkingType === "enabled" ? (_f = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _f.budgetTokens : void 0;
|
|
2968
2977
|
const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
|
|
2969
2978
|
const baseArgs = {
|
|
2970
2979
|
// model id:
|
|
@@ -3171,7 +3180,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
3171
3180
|
if ((anthropicOptions == null ? void 0 : anthropicOptions.speed) === "fast") {
|
|
3172
3181
|
betas.add("fast-mode-2026-02-01");
|
|
3173
3182
|
}
|
|
3174
|
-
if (stream && ((
|
|
3183
|
+
if (stream && ((_g = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _g : true)) {
|
|
3175
3184
|
betas.add("fine-grained-tool-streaming-2025-05-14");
|
|
3176
3185
|
}
|
|
3177
3186
|
const {
|
|
@@ -3185,13 +3194,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
3185
3194
|
toolChoice: { type: "required" },
|
|
3186
3195
|
disableParallelToolUse: true,
|
|
3187
3196
|
cacheControlValidator,
|
|
3188
|
-
supportsStructuredOutput: false
|
|
3197
|
+
supportsStructuredOutput: false,
|
|
3198
|
+
supportsStrictTools
|
|
3189
3199
|
} : {
|
|
3190
3200
|
tools: tools != null ? tools : [],
|
|
3191
3201
|
toolChoice,
|
|
3192
3202
|
disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
|
|
3193
3203
|
cacheControlValidator,
|
|
3194
|
-
supportsStructuredOutput
|
|
3204
|
+
supportsStructuredOutput,
|
|
3205
|
+
supportsStrictTools
|
|
3195
3206
|
}
|
|
3196
3207
|
);
|
|
3197
3208
|
const cacheWarnings = cacheControlValidator.getWarnings();
|
|
@@ -3208,7 +3219,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
3208
3219
|
...betas,
|
|
3209
3220
|
...toolsBetas,
|
|
3210
3221
|
...userSuppliedBetas,
|
|
3211
|
-
...(
|
|
3222
|
+
...(_h = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _h : []
|
|
3212
3223
|
]),
|
|
3213
3224
|
usesJsonResponseTool: jsonResponseTool != null,
|
|
3214
3225
|
toolNameMapping,
|