@ai-sdk/amazon-bedrock 4.0.55 → 4.0.57
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 +15 -0
- package/dist/anthropic/index.js +1 -1
- package/dist/anthropic/index.mjs +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +63 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +63 -25
- package/dist/index.mjs.map +1 -1
- package/docs/08-amazon-bedrock.mdx +29 -6
- package/package.json +4 -4
- package/src/bedrock-chat-language-model.ts +72 -12
- package/src/bedrock-chat-options.ts +4 -2
- package/src/index.ts +10 -2
- package/src/reranking/bedrock-reranking-model.ts +2 -2
- package/src/reranking/bedrock-reranking-options.ts +2 -2
|
@@ -267,6 +267,8 @@ const result = await generateText({
|
|
|
267
267
|
You can use the `bedrock` provider options to utilize [Amazon Bedrock Guardrails](https://aws.amazon.com/bedrock/guardrails/):
|
|
268
268
|
|
|
269
269
|
```ts
|
|
270
|
+
import { type AmazonBedrockLanguageModelOptions } from '@ai-sdk/amazon-bedrock';
|
|
271
|
+
|
|
270
272
|
const result = await generateText({
|
|
271
273
|
model: bedrock('anthropic.claude-3-sonnet-20240229-v1:0'),
|
|
272
274
|
prompt: 'Write a story about space exploration.',
|
|
@@ -278,7 +280,7 @@ const result = await generateText({
|
|
|
278
280
|
trace: 'enabled' as const,
|
|
279
281
|
streamProcessingMode: 'async',
|
|
280
282
|
},
|
|
281
|
-
},
|
|
283
|
+
} satisfies AmazonBedrockLanguageModelOptions,
|
|
282
284
|
},
|
|
283
285
|
});
|
|
284
286
|
```
|
|
@@ -449,6 +451,21 @@ console.log(
|
|
|
449
451
|
// }
|
|
450
452
|
```
|
|
451
453
|
|
|
454
|
+
### Provider Metadata
|
|
455
|
+
|
|
456
|
+
The following Bedrock-specific metadata may be returned in `providerMetadata.bedrock`:
|
|
457
|
+
|
|
458
|
+
- **trace** _(optional)_
|
|
459
|
+
Guardrail tracing information (when tracing is enabled).
|
|
460
|
+
- **performanceConfig** _(optional)_
|
|
461
|
+
Performance configuration, e.g. `{ latency: 'optimized' }`.
|
|
462
|
+
- **serviceTier** _(optional)_
|
|
463
|
+
Service tier information, e.g. `{ type: 'on-demand' }`.
|
|
464
|
+
- **usage** _(optional)_
|
|
465
|
+
Cache token usage details including `cacheWriteInputTokens` and `cacheDetails`.
|
|
466
|
+
- **stopSequence** _string | null_
|
|
467
|
+
The stop sequence that triggered the stop, if any.
|
|
468
|
+
|
|
452
469
|
## Reasoning
|
|
453
470
|
|
|
454
471
|
Amazon Bedrock supports model creator-specific reasoning features:
|
|
@@ -457,7 +474,10 @@ Amazon Bedrock supports model creator-specific reasoning features:
|
|
|
457
474
|
- Amazon (e.g. `us.amazon.nova-2-lite-v1:0`): enable via the `reasoningConfig` provider option and specifying a maximum reasoning effort level (`'low' | 'medium' | 'high'`).
|
|
458
475
|
|
|
459
476
|
```ts
|
|
460
|
-
import {
|
|
477
|
+
import {
|
|
478
|
+
bedrock,
|
|
479
|
+
type AmazonBedrockLanguageModelOptions,
|
|
480
|
+
} from '@ai-sdk/amazon-bedrock';
|
|
461
481
|
import { generateText } from 'ai';
|
|
462
482
|
|
|
463
483
|
// Anthropic example
|
|
@@ -467,7 +487,7 @@ const anthropicResult = await generateText({
|
|
|
467
487
|
providerOptions: {
|
|
468
488
|
bedrock: {
|
|
469
489
|
reasoningConfig: { type: 'enabled', budgetTokens: 1024 },
|
|
470
|
-
},
|
|
490
|
+
} satisfies AmazonBedrockLanguageModelOptions,
|
|
471
491
|
},
|
|
472
492
|
});
|
|
473
493
|
|
|
@@ -481,7 +501,7 @@ const amazonResult = await generateText({
|
|
|
481
501
|
providerOptions: {
|
|
482
502
|
bedrock: {
|
|
483
503
|
reasoningConfig: { type: 'enabled', maxReasoningEffort: 'medium' },
|
|
484
|
-
},
|
|
504
|
+
} satisfies AmazonBedrockLanguageModelOptions,
|
|
485
505
|
},
|
|
486
506
|
});
|
|
487
507
|
|
|
@@ -497,7 +517,10 @@ on how to integrate reasoning into your chatbot.
|
|
|
497
517
|
Claude Sonnet 4 models on Amazon Bedrock support an extended context window of up to 1 million tokens when using the `context-1m-2025-08-07` beta feature.
|
|
498
518
|
|
|
499
519
|
```ts
|
|
500
|
-
import {
|
|
520
|
+
import {
|
|
521
|
+
bedrock,
|
|
522
|
+
type AmazonBedrockLanguageModelOptions,
|
|
523
|
+
} from '@ai-sdk/amazon-bedrock';
|
|
501
524
|
import { generateText } from 'ai';
|
|
502
525
|
|
|
503
526
|
const result = await generateText({
|
|
@@ -506,7 +529,7 @@ const result = await generateText({
|
|
|
506
529
|
providerOptions: {
|
|
507
530
|
bedrock: {
|
|
508
531
|
anthropicBeta: ['context-1m-2025-08-07'],
|
|
509
|
-
},
|
|
532
|
+
} satisfies AmazonBedrockLanguageModelOptions,
|
|
510
533
|
},
|
|
511
534
|
});
|
|
512
535
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/amazon-bedrock",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.57",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@smithy/eventstream-codec": "^4.0.1",
|
|
39
39
|
"@smithy/util-utf8": "^4.0.0",
|
|
40
40
|
"aws4fetch": "^1.0.20",
|
|
41
|
-
"@ai-sdk/anthropic": "3.0.
|
|
41
|
+
"@ai-sdk/anthropic": "3.0.42",
|
|
42
42
|
"@ai-sdk/provider": "3.0.8",
|
|
43
43
|
"@ai-sdk/provider-utils": "4.0.14"
|
|
44
44
|
},
|
|
@@ -47,8 +47,8 @@
|
|
|
47
47
|
"tsup": "^8.3.0",
|
|
48
48
|
"typescript": "5.8.3",
|
|
49
49
|
"zod": "3.25.76",
|
|
50
|
-
"@ai-
|
|
51
|
-
"@
|
|
50
|
+
"@vercel/ai-tsconfig": "0.0.0",
|
|
51
|
+
"@ai-sdk/test-server": "1.0.3"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"zod": "^3.25.76 || ^4.1.8"
|
|
@@ -31,7 +31,7 @@ import {
|
|
|
31
31
|
} from './bedrock-api-types';
|
|
32
32
|
import {
|
|
33
33
|
BedrockChatModelId,
|
|
34
|
-
|
|
34
|
+
amazonBedrockLanguageModelOptions,
|
|
35
35
|
} from './bedrock-chat-options';
|
|
36
36
|
import { BedrockErrorSchema } from './bedrock-error';
|
|
37
37
|
import { createBedrockEventStreamResponseHandler } from './bedrock-event-stream-response-handler';
|
|
@@ -82,7 +82,7 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
82
82
|
(await parseProviderOptions({
|
|
83
83
|
provider: 'bedrock',
|
|
84
84
|
providerOptions,
|
|
85
|
-
schema:
|
|
85
|
+
schema: amazonBedrockLanguageModelOptions,
|
|
86
86
|
})) ?? {};
|
|
87
87
|
|
|
88
88
|
const warnings: SharedV3Warning[] = [];
|
|
@@ -483,15 +483,32 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
483
483
|
response.additionalModelResponseFields?.delta?.stop_sequence ?? null;
|
|
484
484
|
|
|
485
485
|
const providerMetadata =
|
|
486
|
-
response.trace ||
|
|
486
|
+
response.trace ||
|
|
487
|
+
response.usage ||
|
|
488
|
+
response.performanceConfig ||
|
|
489
|
+
response.serviceTier ||
|
|
490
|
+
isJsonResponseFromTool ||
|
|
491
|
+
stopSequence
|
|
487
492
|
? {
|
|
488
493
|
bedrock: {
|
|
489
494
|
...(response.trace && typeof response.trace === 'object'
|
|
490
495
|
? { trace: response.trace as JSONObject }
|
|
491
496
|
: {}),
|
|
492
|
-
...(response.
|
|
497
|
+
...(response.performanceConfig && {
|
|
498
|
+
performanceConfig: response.performanceConfig,
|
|
499
|
+
}),
|
|
500
|
+
...(response.serviceTier && {
|
|
501
|
+
serviceTier: response.serviceTier,
|
|
502
|
+
}),
|
|
503
|
+
...((response.usage?.cacheWriteInputTokens != null ||
|
|
504
|
+
response.usage?.cacheDetails != null) && {
|
|
493
505
|
usage: {
|
|
494
|
-
|
|
506
|
+
...(response.usage.cacheWriteInputTokens != null && {
|
|
507
|
+
cacheWriteInputTokens: response.usage.cacheWriteInputTokens,
|
|
508
|
+
}),
|
|
509
|
+
...(response.usage.cacheDetails != null && {
|
|
510
|
+
cacheDetails: response.usage.cacheDetails,
|
|
511
|
+
}),
|
|
495
512
|
},
|
|
496
513
|
}),
|
|
497
514
|
...(isJsonResponseFromTool && { isJsonResponseFromTool: true }),
|
|
@@ -511,7 +528,12 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
511
528
|
},
|
|
512
529
|
usage: convertBedrockUsage(response.usage),
|
|
513
530
|
response: {
|
|
514
|
-
|
|
531
|
+
id: responseHeaders?.['x-amzn-requestid'] ?? undefined,
|
|
532
|
+
timestamp:
|
|
533
|
+
responseHeaders?.['date'] != null
|
|
534
|
+
? new Date(responseHeaders['date'])
|
|
535
|
+
: undefined,
|
|
536
|
+
modelId: this.modelId,
|
|
515
537
|
headers: responseHeaders,
|
|
516
538
|
},
|
|
517
539
|
warnings,
|
|
@@ -527,8 +549,9 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
527
549
|
warnings,
|
|
528
550
|
usesJsonResponseTool,
|
|
529
551
|
} = await this.getArgs(options);
|
|
530
|
-
const
|
|
531
|
-
const
|
|
552
|
+
const modelId = this.modelId;
|
|
553
|
+
const isMistral = isMistralModel(modelId);
|
|
554
|
+
const url = `${this.getUrl(modelId)}/converse-stream`;
|
|
532
555
|
|
|
533
556
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
534
557
|
url,
|
|
@@ -573,6 +596,15 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
573
596
|
>({
|
|
574
597
|
start(controller) {
|
|
575
598
|
controller.enqueue({ type: 'stream-start', warnings });
|
|
599
|
+
controller.enqueue({
|
|
600
|
+
type: 'response-metadata',
|
|
601
|
+
id: responseHeaders?.['x-amzn-requestid'] ?? undefined,
|
|
602
|
+
timestamp:
|
|
603
|
+
responseHeaders?.['date'] != null
|
|
604
|
+
? new Date(responseHeaders['date'])
|
|
605
|
+
: undefined,
|
|
606
|
+
modelId,
|
|
607
|
+
});
|
|
576
608
|
},
|
|
577
609
|
|
|
578
610
|
transform(chunk, controller) {
|
|
@@ -631,11 +663,18 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
631
663
|
}
|
|
632
664
|
|
|
633
665
|
const cacheUsage =
|
|
634
|
-
value.metadata.usage?.cacheWriteInputTokens != null
|
|
666
|
+
value.metadata.usage?.cacheWriteInputTokens != null ||
|
|
667
|
+
value.metadata.usage?.cacheDetails != null
|
|
635
668
|
? {
|
|
636
669
|
usage: {
|
|
637
|
-
cacheWriteInputTokens
|
|
638
|
-
|
|
670
|
+
...(value.metadata.usage?.cacheWriteInputTokens !=
|
|
671
|
+
null && {
|
|
672
|
+
cacheWriteInputTokens:
|
|
673
|
+
value.metadata.usage.cacheWriteInputTokens,
|
|
674
|
+
}),
|
|
675
|
+
...(value.metadata.usage?.cacheDetails != null && {
|
|
676
|
+
cacheDetails: value.metadata.usage.cacheDetails,
|
|
677
|
+
}),
|
|
639
678
|
},
|
|
640
679
|
}
|
|
641
680
|
: undefined;
|
|
@@ -646,11 +685,22 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
|
|
|
646
685
|
}
|
|
647
686
|
: undefined;
|
|
648
687
|
|
|
649
|
-
if (
|
|
688
|
+
if (
|
|
689
|
+
cacheUsage ||
|
|
690
|
+
trace ||
|
|
691
|
+
value.metadata.performanceConfig ||
|
|
692
|
+
value.metadata.serviceTier
|
|
693
|
+
) {
|
|
650
694
|
providerMetadata = {
|
|
651
695
|
bedrock: {
|
|
652
696
|
...cacheUsage,
|
|
653
697
|
...trace,
|
|
698
|
+
...(value.metadata.performanceConfig && {
|
|
699
|
+
performanceConfig: value.metadata.performanceConfig,
|
|
700
|
+
}),
|
|
701
|
+
...(value.metadata.serviceTier && {
|
|
702
|
+
serviceTier: value.metadata.serviceTier,
|
|
703
|
+
}),
|
|
654
704
|
},
|
|
655
705
|
};
|
|
656
706
|
}
|
|
@@ -955,12 +1005,17 @@ const BedrockResponseSchema = z.object({
|
|
|
955
1005
|
additionalModelResponseFields:
|
|
956
1006
|
BedrockAdditionalModelResponseFieldsSchema.nullish(),
|
|
957
1007
|
trace: z.unknown().nullish(),
|
|
1008
|
+
performanceConfig: z.object({ latency: z.string() }).nullish(),
|
|
1009
|
+
serviceTier: z.object({ type: z.string() }).nullish(),
|
|
958
1010
|
usage: z.object({
|
|
959
1011
|
inputTokens: z.number(),
|
|
960
1012
|
outputTokens: z.number(),
|
|
961
1013
|
totalTokens: z.number(),
|
|
962
1014
|
cacheReadInputTokens: z.number().nullish(),
|
|
963
1015
|
cacheWriteInputTokens: z.number().nullish(),
|
|
1016
|
+
cacheDetails: z
|
|
1017
|
+
.array(z.object({ inputTokens: z.number(), ttl: z.string() }))
|
|
1018
|
+
.nullish(),
|
|
964
1019
|
}),
|
|
965
1020
|
});
|
|
966
1021
|
|
|
@@ -1015,10 +1070,15 @@ const BedrockStreamSchema = z.object({
|
|
|
1015
1070
|
metadata: z
|
|
1016
1071
|
.object({
|
|
1017
1072
|
trace: z.unknown().nullish(),
|
|
1073
|
+
performanceConfig: z.object({ latency: z.string() }).nullish(),
|
|
1074
|
+
serviceTier: z.object({ type: z.string() }).nullish(),
|
|
1018
1075
|
usage: z
|
|
1019
1076
|
.object({
|
|
1020
1077
|
cacheReadInputTokens: z.number().nullish(),
|
|
1021
1078
|
cacheWriteInputTokens: z.number().nullish(),
|
|
1079
|
+
cacheDetails: z
|
|
1080
|
+
.array(z.object({ inputTokens: z.number(), ttl: z.string() }))
|
|
1081
|
+
.nullish(),
|
|
1022
1082
|
inputTokens: z.number(),
|
|
1023
1083
|
outputTokens: z.number(),
|
|
1024
1084
|
})
|
|
@@ -91,7 +91,7 @@ export type BedrockFilePartProviderOptions = z.infer<
|
|
|
91
91
|
typeof bedrockFilePartProviderOptions
|
|
92
92
|
>;
|
|
93
93
|
|
|
94
|
-
export const
|
|
94
|
+
export const amazonBedrockLanguageModelOptions = z.object({
|
|
95
95
|
/**
|
|
96
96
|
* Additional inference parameters that the model supports,
|
|
97
97
|
* beyond the base set of inference parameters that Converse
|
|
@@ -117,4 +117,6 @@ export const bedrockProviderOptions = z.object({
|
|
|
117
117
|
anthropicBeta: z.array(z.string()).optional(),
|
|
118
118
|
});
|
|
119
119
|
|
|
120
|
-
export type
|
|
120
|
+
export type AmazonBedrockLanguageModelOptions = z.infer<
|
|
121
|
+
typeof amazonBedrockLanguageModelOptions
|
|
122
|
+
>;
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
export type { AnthropicProviderOptions } from '@ai-sdk/anthropic';
|
|
2
2
|
|
|
3
|
-
export type {
|
|
3
|
+
export type {
|
|
4
|
+
AmazonBedrockLanguageModelOptions,
|
|
5
|
+
/** @deprecated Use `AmazonBedrockLanguageModelOptions` instead. */
|
|
6
|
+
AmazonBedrockLanguageModelOptions as BedrockProviderOptions,
|
|
7
|
+
} from './bedrock-chat-options';
|
|
4
8
|
export { bedrock, createAmazonBedrock } from './bedrock-provider';
|
|
5
9
|
export type {
|
|
6
10
|
AmazonBedrockProvider,
|
|
7
11
|
AmazonBedrockProviderSettings,
|
|
8
12
|
} from './bedrock-provider';
|
|
9
|
-
export type {
|
|
13
|
+
export type {
|
|
14
|
+
AmazonBedrockRerankingModelOptions,
|
|
15
|
+
/** @deprecated Use `AmazonBedrockRerankingModelOptions` instead. */
|
|
16
|
+
AmazonBedrockRerankingModelOptions as BedrockRerankingOptions,
|
|
17
|
+
} from './reranking/bedrock-reranking-options';
|
|
10
18
|
export { VERSION } from './version';
|
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
} from './bedrock-reranking-api';
|
|
17
17
|
import {
|
|
18
18
|
BedrockRerankingModelId,
|
|
19
|
-
|
|
19
|
+
amazonBedrockRerankingModelOptionsSchema,
|
|
20
20
|
} from './bedrock-reranking-options';
|
|
21
21
|
|
|
22
22
|
type BedrockRerankingConfig = {
|
|
@@ -48,7 +48,7 @@ export class BedrockRerankingModel implements RerankingModelV3 {
|
|
|
48
48
|
const bedrockOptions = await parseProviderOptions({
|
|
49
49
|
provider: 'bedrock',
|
|
50
50
|
providerOptions,
|
|
51
|
-
schema:
|
|
51
|
+
schema: amazonBedrockRerankingModelOptionsSchema,
|
|
52
52
|
});
|
|
53
53
|
|
|
54
54
|
const {
|
|
@@ -7,7 +7,7 @@ export type BedrockRerankingModelId =
|
|
|
7
7
|
| 'cohere.rerank-v3-5:0'
|
|
8
8
|
| (string & {});
|
|
9
9
|
|
|
10
|
-
export type
|
|
10
|
+
export type AmazonBedrockRerankingModelOptions = {
|
|
11
11
|
/**
|
|
12
12
|
* If the total number of results was greater than could fit in a response, a token is returned in the nextToken field. You can enter that token in this field to return the next batch of results.
|
|
13
13
|
*/
|
|
@@ -19,7 +19,7 @@ export type BedrockRerankingOptions = {
|
|
|
19
19
|
additionalModelRequestFields?: Record<string, unknown>;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
export const
|
|
22
|
+
export const amazonBedrockRerankingModelOptionsSchema = lazySchema(() =>
|
|
23
23
|
zodSchema(
|
|
24
24
|
z.object({
|
|
25
25
|
/**
|