@ai-sdk/amazon-bedrock 4.0.56 → 4.0.58
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 +13 -0
- package/dist/anthropic/index.js +1 -1
- package/dist/anthropic/index.mjs +1 -1
- package/dist/index.d.mts +32 -3
- package/dist/index.d.ts +32 -3
- package/dist/index.js +61 -23
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +61 -23
- package/dist/index.mjs.map +1 -1
- package/docs/08-amazon-bedrock.mdx +21 -3
- package/package.json +3 -3
- package/src/bedrock-chat-language-model.ts +70 -10
- package/src/bedrock-embedding-model.ts +2 -2
- package/src/bedrock-embedding-options.ts +5 -1
- package/src/index.ts +1 -0
|
@@ -451,6 +451,21 @@ console.log(
|
|
|
451
451
|
// }
|
|
452
452
|
```
|
|
453
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
|
+
|
|
454
469
|
## Reasoning
|
|
455
470
|
|
|
456
471
|
Amazon Bedrock supports model creator-specific reasoning features:
|
|
@@ -769,6 +784,7 @@ You can pass them as an options argument:
|
|
|
769
784
|
|
|
770
785
|
```ts
|
|
771
786
|
import { bedrock } from '@ai-sdk/amazon-bedrock';
|
|
787
|
+
import { type AmazonBedrockEmbeddingModelOptions } from '@ai-sdk/amazon-bedrock';
|
|
772
788
|
import { embed } from 'ai';
|
|
773
789
|
|
|
774
790
|
const model = bedrock.embedding('amazon.titan-embed-text-v2:0');
|
|
@@ -780,7 +796,7 @@ const { embedding } = await embed({
|
|
|
780
796
|
bedrock: {
|
|
781
797
|
dimensions: 512, // optional, number of dimensions for the embedding
|
|
782
798
|
normalize: true, // optional, normalize the output embeddings
|
|
783
|
-
},
|
|
799
|
+
} satisfies AmazonBedrockEmbeddingModelOptions,
|
|
784
800
|
},
|
|
785
801
|
});
|
|
786
802
|
```
|
|
@@ -801,6 +817,7 @@ Amazon Nova embedding models support additional provider options:
|
|
|
801
817
|
|
|
802
818
|
```ts
|
|
803
819
|
import { bedrock } from '@ai-sdk/amazon-bedrock';
|
|
820
|
+
import { type AmazonBedrockEmbeddingModelOptions } from '@ai-sdk/amazon-bedrock';
|
|
804
821
|
import { embed } from 'ai';
|
|
805
822
|
|
|
806
823
|
const { embedding } = await embed({
|
|
@@ -811,7 +828,7 @@ const { embedding } = await embed({
|
|
|
811
828
|
embeddingDimension: 1024, // optional, number of dimensions
|
|
812
829
|
embeddingPurpose: 'TEXT_RETRIEVAL', // optional, purpose of embedding
|
|
813
830
|
truncate: 'END', // optional, truncation behavior
|
|
814
|
-
},
|
|
831
|
+
} satisfies AmazonBedrockEmbeddingModelOptions,
|
|
815
832
|
},
|
|
816
833
|
});
|
|
817
834
|
```
|
|
@@ -836,6 +853,7 @@ Cohere embedding models on Bedrock require an `inputType` and support truncation
|
|
|
836
853
|
|
|
837
854
|
```ts
|
|
838
855
|
import { bedrock } from '@ai-sdk/amazon-bedrock';
|
|
856
|
+
import { type AmazonBedrockEmbeddingModelOptions } from '@ai-sdk/amazon-bedrock';
|
|
839
857
|
import { embed } from 'ai';
|
|
840
858
|
|
|
841
859
|
const { embedding } = await embed({
|
|
@@ -845,7 +863,7 @@ const { embedding } = await embed({
|
|
|
845
863
|
bedrock: {
|
|
846
864
|
inputType: 'search_document', // required for Cohere
|
|
847
865
|
truncate: 'END', // optional, truncation behavior
|
|
848
|
-
},
|
|
866
|
+
} satisfies AmazonBedrockEmbeddingModelOptions,
|
|
849
867
|
},
|
|
850
868
|
});
|
|
851
869
|
```
|
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.58",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -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"
|
|
@@ -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
|
})
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
} from '@ai-sdk/provider-utils';
|
|
15
15
|
import {
|
|
16
16
|
BedrockEmbeddingModelId,
|
|
17
|
-
|
|
17
|
+
amazonBedrockEmbeddingModelOptionsSchema,
|
|
18
18
|
} from './bedrock-embedding-options';
|
|
19
19
|
import { BedrockErrorSchema } from './bedrock-error';
|
|
20
20
|
import { z } from 'zod/v4';
|
|
@@ -63,7 +63,7 @@ export class BedrockEmbeddingModel implements EmbeddingModelV3 {
|
|
|
63
63
|
(await parseProviderOptions({
|
|
64
64
|
provider: 'bedrock',
|
|
65
65
|
providerOptions,
|
|
66
|
-
schema:
|
|
66
|
+
schema: amazonBedrockEmbeddingModelOptionsSchema,
|
|
67
67
|
})) ?? {};
|
|
68
68
|
|
|
69
69
|
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html
|
|
@@ -7,7 +7,7 @@ export type BedrockEmbeddingModelId =
|
|
|
7
7
|
| 'cohere.embed-multilingual-v3'
|
|
8
8
|
| (string & {});
|
|
9
9
|
|
|
10
|
-
export const
|
|
10
|
+
export const amazonBedrockEmbeddingModelOptionsSchema = z.object({
|
|
11
11
|
/**
|
|
12
12
|
* The number of dimensions the resulting output embeddings should have (defaults to 1024).
|
|
13
13
|
* Only supported in amazon.titan-embed-text-v2:0.
|
|
@@ -72,3 +72,7 @@ export const bedrockEmbeddingProviderOptions = z.object({
|
|
|
72
72
|
.union([z.literal(256), z.literal(512), z.literal(1024), z.literal(1536)])
|
|
73
73
|
.optional(),
|
|
74
74
|
});
|
|
75
|
+
|
|
76
|
+
export type AmazonBedrockEmbeddingModelOptions = z.infer<
|
|
77
|
+
typeof amazonBedrockEmbeddingModelOptionsSchema
|
|
78
|
+
>;
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export type { AnthropicProviderOptions } from '@ai-sdk/anthropic';
|
|
2
2
|
|
|
3
|
+
export type { AmazonBedrockEmbeddingModelOptions } from './bedrock-embedding-options';
|
|
3
4
|
export type {
|
|
4
5
|
AmazonBedrockLanguageModelOptions,
|
|
5
6
|
/** @deprecated Use `AmazonBedrockLanguageModelOptions` instead. */
|