@ai-sdk/amazon-bedrock 4.0.163 → 4.0.166

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.
@@ -35,7 +35,7 @@ var import_provider_utils = require("@ai-sdk/provider-utils");
35
35
  var import_aws4fetch = require("aws4fetch");
36
36
 
37
37
  // src/version.ts
38
- var VERSION = true ? "4.0.163" : "0.0.0-test";
38
+ var VERSION = true ? "4.0.166" : "0.0.0-test";
39
39
 
40
40
  // src/bedrock-sigv4-fetch.ts
41
41
  function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {
@@ -23,7 +23,7 @@ import {
23
23
  import { AwsV4Signer } from "aws4fetch";
24
24
 
25
25
  // src/version.ts
26
- var VERSION = true ? "4.0.163" : "0.0.0-test";
26
+ var VERSION = true ? "4.0.166" : "0.0.0-test";
27
27
 
28
28
  // src/bedrock-sigv4-fetch.ts
29
29
  function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {
@@ -942,6 +942,30 @@ The following provider options are available for Cohere embedding models:
942
942
 
943
943
  Truncation behavior when input exceeds the model's context length. Accepts: `NONE`, `START`, `END`.
944
944
 
945
+ ### Application Inference Profiles
946
+
947
+ Application inference profile ARNs do not identify their underlying model. Pass
948
+ the model family when creating an embedding model so the provider can select
949
+ the correct request format and batch size:
950
+
951
+ ```ts
952
+ import {
953
+ bedrock,
954
+ type AmazonBedrockEmbeddingModelSettings,
955
+ } from '@ai-sdk/amazon-bedrock';
956
+ import { embedMany } from 'ai';
957
+
958
+ const profileArn =
959
+ 'arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/qibm5eutlkcy';
960
+
961
+ const { embeddings } = await embedMany({
962
+ model: bedrock.embedding(profileArn, {
963
+ modelFamily: 'cohere',
964
+ } satisfies AmazonBedrockEmbeddingModelSettings),
965
+ values: ['hello', 'world'],
966
+ });
967
+ ```
968
+
945
969
  ### Model Capabilities
946
970
 
947
971
  | Model | Default Dimensions | Custom Dimensions |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/amazon-bedrock",
3
- "version": "4.0.163",
3
+ "version": "4.0.166",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -44,10 +44,10 @@
44
44
  "@smithy/eventstream-codec": "^4.0.1",
45
45
  "@smithy/util-utf8": "^4.0.0",
46
46
  "aws4fetch": "^1.0.20",
47
- "@ai-sdk/anthropic": "3.0.113",
48
- "@ai-sdk/openai": "3.0.102",
47
+ "@ai-sdk/anthropic": "3.0.115",
48
+ "@ai-sdk/openai": "3.0.105",
49
49
  "@ai-sdk/provider": "3.0.15",
50
- "@ai-sdk/provider-utils": "4.0.48"
50
+ "@ai-sdk/provider-utils": "4.0.50"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@types/node": "20.17.24",
@@ -61,6 +61,10 @@ type BedrockChatConfig = {
61
61
  generateId: () => string;
62
62
  };
63
63
 
64
+ const anthropicProviderOptions = z.object({
65
+ disableParallelToolUse: z.boolean().optional(),
66
+ });
67
+
64
68
  export class BedrockChatLanguageModel implements LanguageModelV3 {
65
69
  readonly specificationVersion = 'v3';
66
70
  readonly provider = 'amazon-bedrock';
@@ -99,6 +103,12 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
99
103
  schema: amazonBedrockLanguageModelOptions,
100
104
  })) ?? {};
101
105
 
106
+ const anthropicOptions = await parseProviderOptions({
107
+ provider: 'anthropic',
108
+ providerOptions,
109
+ schema: anthropicProviderOptions,
110
+ });
111
+
102
112
  const warnings: SharedV3Warning[] = [];
103
113
 
104
114
  if (frequencyPenalty != null) {
@@ -150,7 +160,12 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
150
160
  });
151
161
  }
152
162
 
153
- const isAnthropicModel = this.modelId.includes('anthropic');
163
+ // Application inference profile ARNs do not expose their underlying model.
164
+ // The Anthropic-only reasoning budget provides the model-family signal.
165
+ const isAnthropicModel =
166
+ this.modelId.includes('anthropic') ||
167
+ (this.modelId.includes(':application-inference-profile/') &&
168
+ bedrockOptions.reasoningConfig?.budgetTokens != null);
154
169
  const openAIModelId = /^(?:[^.]+\.)?(openai\..+)$/.exec(this.modelId)?.[1];
155
170
  const isOpenAIModel = openAIModelId != null;
156
171
  const isOpenAIGptOssModel =
@@ -196,6 +211,7 @@ export class BedrockChatLanguageModel implements LanguageModelV3 {
196
211
  toolChoice:
197
212
  jsonResponseTool != null ? { type: 'required' } : toolChoice,
198
213
  modelId: this.modelId,
214
+ disableParallelToolUse: anthropicOptions?.disableParallelToolUse,
199
215
  });
200
216
 
201
217
  warnings.push(...toolWarnings);
@@ -1267,6 +1283,9 @@ const BedrockStreamSchema = z.object({
1267
1283
  delta: z
1268
1284
  .union([
1269
1285
  z.object({ text: z.string() }),
1286
+ z.object({
1287
+ citation: z.record(z.string(), z.unknown()),
1288
+ }),
1270
1289
  z.object({ toolUse: z.object({ input: z.string() }) }),
1271
1290
  z.object({
1272
1291
  reasoningContent: z.object({ text: z.string() }),
@@ -15,6 +15,7 @@ import {
15
15
  import {
16
16
  amazonBedrockEmbeddingModelOptionsSchema,
17
17
  type BedrockEmbeddingModelId,
18
+ type AmazonBedrockEmbeddingModelSettings,
18
19
  } from './bedrock-embedding-options';
19
20
  import { BedrockErrorSchema } from './bedrock-error';
20
21
  import { z } from 'zod/v4';
@@ -23,6 +24,7 @@ type BedrockEmbeddingConfig = {
23
24
  baseUrl: () => string;
24
25
  headers: Resolvable<Record<string, string | undefined>>;
25
26
  fetch?: FetchFunction;
27
+ modelFamily?: AmazonBedrockEmbeddingModelSettings['modelFamily'];
26
28
  };
27
29
 
28
30
  type DoEmbedResponse = Awaited<ReturnType<EmbeddingModelV3['doEmbed']>>;
@@ -33,7 +35,11 @@ export class BedrockEmbeddingModel implements EmbeddingModelV3 {
33
35
  readonly supportsParallelCalls = true;
34
36
 
35
37
  get maxEmbeddingsPerCall() {
36
- return isCohereEmbeddingModel(this.modelId) ? 96 : 1;
38
+ return this.modelFamily === 'cohere' ? 96 : 1;
39
+ }
40
+
41
+ private get modelFamily() {
42
+ return this.config.modelFamily ?? detectEmbeddingModelFamily(this.modelId);
37
43
  }
38
44
 
39
45
  constructor(
@@ -74,36 +80,36 @@ export class BedrockEmbeddingModel implements EmbeddingModelV3 {
74
80
  // Note: Different embedding model families expect different request/response
75
81
  // payloads (e.g. Titan vs Cohere vs Nova). We keep the public interface stable and
76
82
  // adapt here based on the modelId.
77
- const isNovaModel = isNovaEmbeddingModel(this.modelId);
78
- const isCohereModel = isCohereEmbeddingModel(this.modelId);
79
-
80
- const args = isNovaModel
81
- ? {
82
- taskType: 'SINGLE_EMBEDDING',
83
- singleEmbeddingParams: {
84
- embeddingPurpose:
85
- bedrockOptions.embeddingPurpose ?? 'GENERIC_INDEX',
86
- embeddingDimension: bedrockOptions.embeddingDimension ?? 1024,
87
- text: {
88
- truncationMode: bedrockOptions.truncate ?? 'END',
89
- value: values[0],
90
- },
91
- },
92
- }
93
- : isCohereModel
83
+ const modelFamily = this.modelFamily;
84
+
85
+ const args =
86
+ modelFamily === 'nova'
94
87
  ? {
95
- // Cohere embedding models on Bedrock require `input_type`.
96
- // Without it, the service attempts other schema branches and rejects the request.
97
- input_type: bedrockOptions.inputType ?? 'search_query',
98
- texts: values,
99
- truncate: bedrockOptions.truncate,
100
- output_dimension: bedrockOptions.outputDimension,
88
+ taskType: 'SINGLE_EMBEDDING',
89
+ singleEmbeddingParams: {
90
+ embeddingPurpose:
91
+ bedrockOptions.embeddingPurpose ?? 'GENERIC_INDEX',
92
+ embeddingDimension: bedrockOptions.embeddingDimension ?? 1024,
93
+ text: {
94
+ truncationMode: bedrockOptions.truncate ?? 'END',
95
+ value: values[0],
96
+ },
97
+ },
101
98
  }
102
- : {
103
- inputText: values[0],
104
- dimensions: bedrockOptions.dimensions,
105
- normalize: bedrockOptions.normalize,
106
- };
99
+ : modelFamily === 'cohere'
100
+ ? {
101
+ // Cohere embedding models on Bedrock require `input_type`.
102
+ // Without it, the service attempts other schema branches and rejects the request.
103
+ input_type: bedrockOptions.inputType ?? 'search_query',
104
+ texts: values,
105
+ truncate: bedrockOptions.truncate,
106
+ output_dimension: bedrockOptions.outputDimension,
107
+ }
108
+ : {
109
+ inputText: values[0],
110
+ dimensions: bedrockOptions.dimensions,
111
+ normalize: bedrockOptions.normalize,
112
+ };
107
113
 
108
114
  const url = this.getUrl(this.modelId);
109
115
  const { value: response, responseHeaders } = await postJsonToApi({
@@ -172,7 +178,17 @@ function isCohereEmbeddingModel(modelId: string) {
172
178
  }
173
179
 
174
180
  function isNovaEmbeddingModel(modelId: string) {
175
- return modelId.startsWith('amazon.nova-') && modelId.includes('embed');
181
+ return modelId.includes('amazon.nova-') && modelId.includes('embed');
182
+ }
183
+
184
+ function detectEmbeddingModelFamily(
185
+ modelId: string,
186
+ ): NonNullable<AmazonBedrockEmbeddingModelSettings['modelFamily']> {
187
+ return isNovaEmbeddingModel(modelId)
188
+ ? 'nova'
189
+ : isCohereEmbeddingModel(modelId)
190
+ ? 'cohere'
191
+ : 'titan';
176
192
  }
177
193
 
178
194
  const BedrockEmbeddingResponseSchema = z.union([
@@ -7,6 +7,16 @@ export type BedrockEmbeddingModelId =
7
7
  | 'cohere.embed-multilingual-v3'
8
8
  | (string & {});
9
9
 
10
+ export type AmazonBedrockEmbeddingModelSettings = {
11
+ /**
12
+ * The embedding model family.
13
+ *
14
+ * Specify this when the model ID does not identify the underlying model,
15
+ * such as an application inference profile ARN.
16
+ */
17
+ modelFamily?: 'titan' | 'cohere' | 'nova';
18
+ };
19
+
10
20
  export const amazonBedrockEmbeddingModelOptionsSchema = z.object({
11
21
  /**
12
22
  * The number of dimensions the resulting output embeddings should have (defaults to 1024).
@@ -19,10 +19,12 @@ export async function prepareTools({
19
19
  tools,
20
20
  toolChoice,
21
21
  modelId,
22
+ disableParallelToolUse,
22
23
  }: {
23
24
  tools: LanguageModelV3CallOptions['tools'];
24
25
  toolChoice?: LanguageModelV3CallOptions['toolChoice'];
25
26
  modelId: string;
27
+ disableParallelToolUse?: boolean;
26
28
  }): Promise<{
27
29
  toolConfig: BedrockToolConfiguration;
28
30
  additionalTools: Record<string, unknown> | undefined;
@@ -85,6 +87,7 @@ export async function prepareTools({
85
87
  } = await prepareAnthropicTools({
86
88
  tools: ProviderTools,
87
89
  toolChoice,
90
+ disableParallelToolUse,
88
91
  supportsStructuredOutput: false,
89
92
  supportsStrictTools: false,
90
93
  });
@@ -161,9 +164,35 @@ export async function prepareTools({
161
164
  });
162
165
  }
163
166
 
167
+ if (
168
+ isAnthropicModel &&
169
+ !usingAnthropicTools &&
170
+ disableParallelToolUse &&
171
+ bedrockTools.length > 0 &&
172
+ toolChoice?.type !== 'none'
173
+ ) {
174
+ additionalTools = {
175
+ tool_choice:
176
+ toolChoice?.type === 'required'
177
+ ? { type: 'any', disable_parallel_tool_use: true }
178
+ : toolChoice?.type === 'tool'
179
+ ? {
180
+ type: 'tool',
181
+ name: toolChoice.toolName,
182
+ disable_parallel_tool_use: true,
183
+ }
184
+ : { type: 'auto', disable_parallel_tool_use: true },
185
+ };
186
+ }
187
+
164
188
  // Handle toolChoice for standard Bedrock tools, but NOT for Anthropic provider-defined tools
165
189
  let bedrockToolChoice: BedrockToolConfiguration['toolChoice'] = undefined;
166
- if (!usingAnthropicTools && bedrockTools.length > 0 && toolChoice) {
190
+ if (
191
+ !usingAnthropicTools &&
192
+ additionalTools?.tool_choice == null &&
193
+ bedrockTools.length > 0 &&
194
+ toolChoice
195
+ ) {
167
196
  const type = toolChoice.type;
168
197
  switch (type) {
169
198
  case 'auto':
@@ -17,7 +17,10 @@ import {
17
17
  import { BedrockChatLanguageModel } from './bedrock-chat-language-model';
18
18
  import type { BedrockChatModelId } from './bedrock-chat-options';
19
19
  import { BedrockEmbeddingModel } from './bedrock-embedding-model';
20
- import type { BedrockEmbeddingModelId } from './bedrock-embedding-options';
20
+ import type {
21
+ BedrockEmbeddingModelId,
22
+ AmazonBedrockEmbeddingModelSettings,
23
+ } from './bedrock-embedding-options';
21
24
  import { BedrockImageModel } from './bedrock-image-model';
22
25
  import type { BedrockImageModelId } from './bedrock-image-settings';
23
26
  import {
@@ -117,22 +120,34 @@ export interface AmazonBedrockProvider extends ProviderV3 {
117
120
  /**
118
121
  * Creates a model for text embeddings.
119
122
  */
120
- embedding(modelId: BedrockEmbeddingModelId): EmbeddingModelV3;
123
+ embedding(
124
+ modelId: BedrockEmbeddingModelId,
125
+ settings?: AmazonBedrockEmbeddingModelSettings,
126
+ ): EmbeddingModelV3;
121
127
 
122
128
  /**
123
129
  * Creates a model for text embeddings.
124
130
  */
125
- embeddingModel(modelId: BedrockEmbeddingModelId): EmbeddingModelV3;
131
+ embeddingModel(
132
+ modelId: BedrockEmbeddingModelId,
133
+ settings?: AmazonBedrockEmbeddingModelSettings,
134
+ ): EmbeddingModelV3;
126
135
 
127
136
  /**
128
137
  * @deprecated Use `embedding` instead.
129
138
  */
130
- textEmbedding(modelId: BedrockEmbeddingModelId): EmbeddingModelV3;
139
+ textEmbedding(
140
+ modelId: BedrockEmbeddingModelId,
141
+ settings?: AmazonBedrockEmbeddingModelSettings,
142
+ ): EmbeddingModelV3;
131
143
 
132
144
  /**
133
145
  * @deprecated Use `embeddingModel` instead.
134
146
  */
135
- textEmbeddingModel(modelId: BedrockEmbeddingModelId): EmbeddingModelV3;
147
+ textEmbeddingModel(
148
+ modelId: BedrockEmbeddingModelId,
149
+ settings?: AmazonBedrockEmbeddingModelSettings,
150
+ ): EmbeddingModelV3;
136
151
 
137
152
  /**
138
153
  * Creates a model for image generation.
@@ -308,11 +323,15 @@ export function createAmazonBedrock(
308
323
  return createChatModel(modelId);
309
324
  };
310
325
 
311
- const createEmbeddingModel = (modelId: BedrockEmbeddingModelId) =>
326
+ const createEmbeddingModel = (
327
+ modelId: BedrockEmbeddingModelId,
328
+ settings: AmazonBedrockEmbeddingModelSettings = {},
329
+ ) =>
312
330
  new BedrockEmbeddingModel(modelId, {
313
331
  baseUrl: getBedrockRuntimeBaseUrl,
314
332
  headers: getHeaders,
315
333
  fetch: fetchFunction,
334
+ modelFamily: settings.modelFamily,
316
335
  });
317
336
 
318
337
  const createImageModel = (modelId: BedrockImageModelId) =>
@@ -334,7 +334,12 @@ export async function convertToBedrockChatMessages(
334
334
  pushCachePoint(bedrockContent, providerOptions);
335
335
  }
336
336
 
337
- messages.push({ role: 'user', content: bedrockContent });
337
+ const previousMessage = messages.at(-1);
338
+ if (previousMessage?.role === 'user') {
339
+ previousMessage.content.push(...bedrockContent);
340
+ } else {
341
+ messages.push({ role: 'user', content: bedrockContent });
342
+ }
338
343
 
339
344
  break;
340
345
  }
@@ -347,8 +352,56 @@ export async function convertToBedrockChatMessages(
347
352
  const message = block.messages[j];
348
353
  const isLastMessage = j === block.messages.length - 1;
349
354
  const { content } = message;
350
- const hasReasoningBlocks = content.some(
351
- part => part.type === 'reasoning',
355
+ const convertedReasoningContent: Array<
356
+ BedrockAssistantMessage['content'][number] | undefined
357
+ > = await Promise.all(
358
+ content.map(async part => {
359
+ if (part.type !== 'reasoning') {
360
+ return undefined;
361
+ }
362
+
363
+ const metadata = await parseProviderOptions({
364
+ provider: 'bedrock',
365
+ providerOptions: part.providerOptions,
366
+ schema: bedrockReasoningMetadataSchema,
367
+ });
368
+
369
+ if (metadata?.signature != null) {
370
+ return {
371
+ reasoningContent: {
372
+ reasoningText: {
373
+ // do not trim reasoning text when a signature is present:
374
+ // the signature validates the exact original bytes
375
+ text: part.text,
376
+ signature: metadata.signature,
377
+ },
378
+ },
379
+ };
380
+ }
381
+
382
+ if (metadata?.redactedContent != null) {
383
+ return {
384
+ reasoningContent: {
385
+ redactedContent: metadata.redactedContent,
386
+ },
387
+ };
388
+ }
389
+
390
+ if (metadata?.redactedData != null) {
391
+ return {
392
+ reasoningContent: {
393
+ redactedReasoning: {
394
+ data: metadata.redactedData,
395
+ },
396
+ },
397
+ };
398
+ }
399
+
400
+ return undefined;
401
+ }),
402
+ );
403
+ const hasReplayableReasoningBlocks = convertedReasoningContent.some(
404
+ part => part != null,
352
405
  );
353
406
 
354
407
  for (let k = 0; k < content.length; k++) {
@@ -357,8 +410,9 @@ export async function convertToBedrockChatMessages(
357
410
 
358
411
  switch (part.type) {
359
412
  case 'text': {
360
- // Skip empty text blocks unless reasoning blocks are present
361
- if (!part.text.trim() && !hasReasoningBlocks) {
413
+ // Skip empty text blocks unless replayable reasoning blocks are
414
+ // present and the original block order must be preserved.
415
+ if (!part.text.trim() && !hasReplayableReasoningBlocks) {
362
416
  break;
363
417
  }
364
418
 
@@ -378,37 +432,9 @@ export async function convertToBedrockChatMessages(
378
432
  }
379
433
 
380
434
  case 'reasoning': {
381
- const reasoningMetadata = await parseProviderOptions({
382
- provider: 'bedrock',
383
- providerOptions: part.providerOptions,
384
- schema: bedrockReasoningMetadataSchema,
385
- });
386
-
387
- if (reasoningMetadata?.signature != null) {
388
- // do not trim reasoning text when a signature is present:
389
- // the signature validates the exact original bytes
390
- bedrockContent.push({
391
- reasoningContent: {
392
- reasoningText: {
393
- text: part.text,
394
- signature: reasoningMetadata.signature,
395
- },
396
- },
397
- });
398
- } else if (reasoningMetadata?.redactedContent != null) {
399
- bedrockContent.push({
400
- reasoningContent: {
401
- redactedContent: reasoningMetadata.redactedContent,
402
- },
403
- });
404
- } else if (reasoningMetadata?.redactedData != null) {
405
- bedrockContent.push({
406
- reasoningContent: {
407
- redactedReasoning: {
408
- data: reasoningMetadata.redactedData,
409
- },
410
- },
411
- });
435
+ const convertedPart = convertedReasoningContent[k];
436
+ if (convertedPart != null) {
437
+ bedrockContent.push(convertedPart);
412
438
  }
413
439
  // Unsigned reasoning is intentionally not replayed. Some
414
440
  // Bedrock models (for example OpenAI gpt-oss) return reasoning
@@ -434,7 +460,9 @@ export async function convertToBedrockChatMessages(
434
460
  pushCachePoint(bedrockContent, message.providerOptions);
435
461
  }
436
462
 
437
- messages.push({ role: 'assistant', content: bedrockContent });
463
+ if (bedrockContent.some(block => !('cachePoint' in block))) {
464
+ messages.push({ role: 'assistant', content: bedrockContent });
465
+ }
438
466
 
439
467
  break;
440
468
  }
package/src/index.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  export type { AnthropicProviderOptions } from '@ai-sdk/anthropic';
2
2
 
3
- export type { AmazonBedrockEmbeddingModelOptions } from './bedrock-embedding-options';
3
+ export type {
4
+ AmazonBedrockEmbeddingModelOptions,
5
+ AmazonBedrockEmbeddingModelSettings,
6
+ } from './bedrock-embedding-options';
4
7
  export type {
5
8
  AmazonBedrockLanguageModelOptions,
6
9
  /** @deprecated Use `AmazonBedrockLanguageModelOptions` instead. */