@ai-sdk/amazon-bedrock 5.0.0-beta.8 → 5.0.0-beta.85

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +669 -4
  2. package/README.md +2 -0
  3. package/dist/anthropic/index.d.ts +10 -10
  4. package/dist/anthropic/index.js +151 -117
  5. package/dist/anthropic/index.js.map +1 -1
  6. package/dist/index.d.ts +36 -23
  7. package/dist/index.js +881 -604
  8. package/dist/index.js.map +1 -1
  9. package/dist/mantle/index.cjs +253 -0
  10. package/dist/mantle/index.cjs.map +1 -0
  11. package/dist/mantle/index.d.cts +99 -0
  12. package/dist/mantle/index.d.ts +99 -0
  13. package/dist/mantle/index.js +240 -0
  14. package/dist/mantle/index.js.map +1 -0
  15. package/docs/08-amazon-bedrock.mdx +310 -84
  16. package/mantle/index.d.ts +1 -0
  17. package/package.json +27 -20
  18. package/src/amazon-bedrock-api-types.ts +228 -0
  19. package/src/{bedrock-chat-options.ts → amazon-bedrock-chat-language-model-options.ts} +27 -8
  20. package/src/{bedrock-chat-language-model.ts → amazon-bedrock-chat-language-model.ts} +350 -180
  21. package/src/{bedrock-embedding-options.ts → amazon-bedrock-embedding-model-options.ts} +1 -1
  22. package/src/{bedrock-embedding-model.ts → amazon-bedrock-embedding-model.ts} +61 -29
  23. package/src/{bedrock-error.ts → amazon-bedrock-error.ts} +1 -1
  24. package/src/{bedrock-event-stream-decoder.ts → amazon-bedrock-event-stream-decoder.ts} +1 -1
  25. package/src/{bedrock-event-stream-response-handler.ts → amazon-bedrock-event-stream-response-handler.ts} +6 -6
  26. package/src/{bedrock-image-model.ts → amazon-bedrock-image-model.ts} +62 -38
  27. package/src/amazon-bedrock-image-settings.ts +9 -0
  28. package/src/{bedrock-prepare-tools.ts → amazon-bedrock-prepare-tools.ts} +22 -18
  29. package/src/{bedrock-provider.ts → amazon-bedrock-provider.ts} +53 -46
  30. package/src/amazon-bedrock-reasoning-metadata.ts +10 -0
  31. package/src/{bedrock-sigv4-fetch.ts → amazon-bedrock-sigv4-fetch.ts} +17 -9
  32. package/src/anthropic/amazon-bedrock-anthropic-fetch.ts +104 -0
  33. package/src/anthropic/{bedrock-anthropic-options.ts → amazon-bedrock-anthropic-options.ts} +7 -1
  34. package/src/anthropic/{bedrock-anthropic-provider.ts → amazon-bedrock-anthropic-provider.ts} +40 -24
  35. package/src/anthropic/index.ts +19 -7
  36. package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +4 -4
  37. package/src/convert-to-amazon-bedrock-chat-messages.ts +556 -0
  38. package/src/index.ts +15 -8
  39. package/src/inject-fetch-headers.ts +1 -1
  40. package/src/mantle/bedrock-mantle-options.ts +15 -0
  41. package/src/mantle/bedrock-mantle-provider.ts +283 -0
  42. package/src/mantle/index.ts +6 -0
  43. package/src/{map-bedrock-finish-reason.ts → map-amazon-bedrock-finish-reason.ts} +4 -4
  44. package/src/reranking/{bedrock-reranking-api.ts → amazon-bedrock-reranking-api.ts} +3 -3
  45. package/src/reranking/{bedrock-reranking-options.ts → amazon-bedrock-reranking-model-options.ts} +1 -1
  46. package/src/reranking/{bedrock-reranking-model.ts → amazon-bedrock-reranking-model.ts} +32 -25
  47. package/dist/anthropic/index.d.mts +0 -91
  48. package/dist/anthropic/index.mjs +0 -397
  49. package/dist/anthropic/index.mjs.map +0 -1
  50. package/dist/index.d.mts +0 -194
  51. package/dist/index.mjs +0 -2329
  52. package/dist/index.mjs.map +0 -1
  53. package/src/anthropic/bedrock-anthropic-fetch.ts +0 -68
  54. package/src/bedrock-api-types.ts +0 -216
  55. package/src/bedrock-image-settings.ts +0 -6
  56. package/src/convert-to-bedrock-chat-messages.ts +0 -468
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod/v4';
2
2
 
3
- export type BedrockEmbeddingModelId =
3
+ export type AmazonBedrockEmbeddingModelId =
4
4
  | 'amazon.titan-embed-text-v1'
5
5
  | 'amazon.titan-embed-text-v2:0'
6
6
  | 'cohere.embed-english-v3'
@@ -1,41 +1,58 @@
1
1
  import {
2
- EmbeddingModelV4,
3
2
  TooManyEmbeddingValuesForCallError,
3
+ type EmbeddingModelV4,
4
4
  } from '@ai-sdk/provider';
5
5
  import {
6
- FetchFunction,
7
- Resolvable,
8
6
  combineHeaders,
9
7
  createJsonErrorResponseHandler,
10
8
  createJsonResponseHandler,
11
9
  parseProviderOptions,
12
10
  postJsonToApi,
13
11
  resolve,
12
+ serializeModelOptions,
13
+ WORKFLOW_SERIALIZE,
14
+ WORKFLOW_DESERIALIZE,
15
+ type FetchFunction,
16
+ type Resolvable,
14
17
  } from '@ai-sdk/provider-utils';
15
18
  import {
16
- BedrockEmbeddingModelId,
17
19
  amazonBedrockEmbeddingModelOptionsSchema,
18
- } from './bedrock-embedding-options';
19
- import { BedrockErrorSchema } from './bedrock-error';
20
+ type AmazonBedrockEmbeddingModelId,
21
+ } from './amazon-bedrock-embedding-model-options';
22
+ import { AmazonBedrockErrorSchema } from './amazon-bedrock-error';
20
23
  import { z } from 'zod/v4';
21
24
 
22
- type BedrockEmbeddingConfig = {
25
+ type AmazonBedrockEmbeddingConfig = {
23
26
  baseUrl: () => string;
24
- headers: Resolvable<Record<string, string | undefined>>;
27
+ headers?: Resolvable<Record<string, string | undefined>>;
25
28
  fetch?: FetchFunction;
26
29
  };
27
30
 
28
31
  type DoEmbedResponse = Awaited<ReturnType<EmbeddingModelV4['doEmbed']>>;
29
32
 
30
- export class BedrockEmbeddingModel implements EmbeddingModelV4 {
33
+ export class AmazonBedrockEmbeddingModel implements EmbeddingModelV4 {
31
34
  readonly specificationVersion = 'v4';
32
35
  readonly provider = 'amazon-bedrock';
33
36
  readonly maxEmbeddingsPerCall = 1;
34
37
  readonly supportsParallelCalls = true;
35
38
 
39
+ static [WORKFLOW_SERIALIZE](model: AmazonBedrockEmbeddingModel) {
40
+ return serializeModelOptions({
41
+ modelId: model.modelId,
42
+ config: model.config,
43
+ });
44
+ }
45
+
46
+ static [WORKFLOW_DESERIALIZE](options: {
47
+ modelId: string;
48
+ config: AmazonBedrockEmbeddingConfig;
49
+ }) {
50
+ return new AmazonBedrockEmbeddingModel(options.modelId, options.config);
51
+ }
52
+
36
53
  constructor(
37
- readonly modelId: BedrockEmbeddingModelId,
38
- private readonly config: BedrockEmbeddingConfig,
54
+ readonly modelId: AmazonBedrockEmbeddingModelId,
55
+ private readonly config: AmazonBedrockEmbeddingConfig,
39
56
  ) {}
40
57
 
41
58
  private getUrl(modelId: string): string {
@@ -58,13 +75,20 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
58
75
  });
59
76
  }
60
77
 
61
- // Parse provider options
62
- const bedrockOptions =
78
+ // Parse provider options. Prefer `amazonBedrock`; fall back to legacy
79
+ // `bedrock` key for backward compatibility.
80
+ const amazonBedrockOptions =
81
+ (await parseProviderOptions({
82
+ provider: 'amazonBedrock',
83
+ providerOptions,
84
+ schema: amazonBedrockEmbeddingModelOptionsSchema,
85
+ })) ??
63
86
  (await parseProviderOptions({
64
87
  provider: 'bedrock',
65
88
  providerOptions,
66
89
  schema: amazonBedrockEmbeddingModelOptionsSchema,
67
- })) ?? {};
90
+ })) ??
91
+ {};
68
92
 
69
93
  // https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html
70
94
  //
@@ -73,17 +97,19 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
73
97
  // adapt here based on the modelId.
74
98
  const isNovaModel =
75
99
  this.modelId.startsWith('amazon.nova-') && this.modelId.includes('embed');
76
- const isCohereModel = this.modelId.startsWith('cohere.embed-');
100
+ // Use `includes` so cross-region inference profile ids (e.g.
101
+ // `us.cohere.embed-v4:0`, `global.cohere.embed-v4:0`) are detected too.
102
+ const isCohereModel = this.modelId.includes('cohere.embed-');
77
103
 
78
104
  const args = isNovaModel
79
105
  ? {
80
106
  taskType: 'SINGLE_EMBEDDING',
81
107
  singleEmbeddingParams: {
82
108
  embeddingPurpose:
83
- bedrockOptions.embeddingPurpose ?? 'GENERIC_INDEX',
84
- embeddingDimension: bedrockOptions.embeddingDimension ?? 1024,
109
+ amazonBedrockOptions.embeddingPurpose ?? 'GENERIC_INDEX',
110
+ embeddingDimension: amazonBedrockOptions.embeddingDimension ?? 1024,
85
111
  text: {
86
- truncationMode: bedrockOptions.truncate ?? 'END',
112
+ truncationMode: amazonBedrockOptions.truncate ?? 'END',
87
113
  value: values[0],
88
114
  },
89
115
  },
@@ -92,30 +118,33 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
92
118
  ? {
93
119
  // Cohere embedding models on Bedrock require `input_type`.
94
120
  // Without it, the service attempts other schema branches and rejects the request.
95
- input_type: bedrockOptions.inputType ?? 'search_query',
121
+ input_type: amazonBedrockOptions.inputType ?? 'search_query',
96
122
  texts: [values[0]],
97
- truncate: bedrockOptions.truncate,
98
- output_dimension: bedrockOptions.outputDimension,
123
+ truncate: amazonBedrockOptions.truncate,
124
+ output_dimension: amazonBedrockOptions.outputDimension,
99
125
  }
100
126
  : {
101
127
  inputText: values[0],
102
- dimensions: bedrockOptions.dimensions,
103
- normalize: bedrockOptions.normalize,
128
+ dimensions: amazonBedrockOptions.dimensions,
129
+ normalize: amazonBedrockOptions.normalize,
104
130
  };
105
131
 
106
132
  const url = this.getUrl(this.modelId);
107
- const { value: response } = await postJsonToApi({
133
+ const { value: response, responseHeaders } = await postJsonToApi({
108
134
  url,
109
135
  headers: await resolve(
110
- combineHeaders(await resolve(this.config.headers), headers),
136
+ combineHeaders(
137
+ this.config.headers ? await resolve(this.config.headers) : undefined,
138
+ headers,
139
+ ),
111
140
  ),
112
141
  body: args,
113
142
  failedResponseHandler: createJsonErrorResponseHandler({
114
- errorSchema: BedrockErrorSchema,
143
+ errorSchema: AmazonBedrockErrorSchema,
115
144
  errorToMessage: error => `${error.type}: ${error.message}`,
116
145
  }),
117
146
  successfulResponseHandler: createJsonResponseHandler(
118
- BedrockEmbeddingResponseSchema,
147
+ AmazonBedrockEmbeddingResponseSchema,
119
148
  ),
120
149
  fetch: this.config.fetch,
121
150
  abortSignal,
@@ -145,12 +174,15 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
145
174
  }
146
175
 
147
176
  // Extract token count based on response format
177
+ const headerTokenCount = Number(
178
+ responseHeaders?.['x-amzn-bedrock-input-token-count'],
179
+ );
148
180
  const tokens =
149
181
  'inputTextTokenCount' in response
150
182
  ? response.inputTextTokenCount // Titan response
151
183
  : 'inputTokenCount' in response
152
184
  ? (response.inputTokenCount ?? 0) // Nova response
153
- : NaN; // Cohere doesn't return token count
185
+ : headerTokenCount;
154
186
 
155
187
  return {
156
188
  embeddings: [embedding],
@@ -160,7 +192,7 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
160
192
  }
161
193
  }
162
194
 
163
- const BedrockEmbeddingResponseSchema = z.union([
195
+ const AmazonBedrockEmbeddingResponseSchema = z.union([
164
196
  // Titan-style response
165
197
  z.object({
166
198
  embedding: z.array(z.number()),
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod/v4';
2
2
 
3
- export const BedrockErrorSchema = z.object({
3
+ export const AmazonBedrockErrorSchema = z.object({
4
4
  message: z.string(),
5
5
  type: z.string().nullish(),
6
6
  });
@@ -7,7 +7,7 @@ export interface DecodedEvent {
7
7
  data: string;
8
8
  }
9
9
 
10
- export function createBedrockEventStreamDecoder<T>(
10
+ export function createAmazonBedrockEventStreamDecoder<T>(
11
11
  body: ReadableStream<Uint8Array>,
12
12
  processEvent: (
13
13
  event: DecodedEvent,
@@ -1,15 +1,15 @@
1
1
  import { EmptyResponseBodyError } from '@ai-sdk/provider';
2
2
  import {
3
- ParseResult,
4
3
  safeParseJSON,
5
4
  extractResponseHeaders,
6
- ResponseHandler,
7
5
  safeValidateTypes,
6
+ type ParseResult,
7
+ type ResponseHandler,
8
8
  } from '@ai-sdk/provider-utils';
9
- import { ZodType } from 'zod/v4';
10
- import { createBedrockEventStreamDecoder } from './bedrock-event-stream-decoder';
9
+ import type { ZodType } from 'zod/v4';
10
+ import { createAmazonBedrockEventStreamDecoder } from './amazon-bedrock-event-stream-decoder';
11
11
 
12
- export const createBedrockEventStreamResponseHandler =
12
+ export const createAmazonBedrockEventStreamResponseHandler =
13
13
  <T>(
14
14
  chunkSchema: ZodType<T, any>,
15
15
  ): ResponseHandler<ReadableStream<ParseResult<T>>> =>
@@ -22,7 +22,7 @@ export const createBedrockEventStreamResponseHandler =
22
22
 
23
23
  return {
24
24
  responseHeaders,
25
- value: createBedrockEventStreamDecoder<ParseResult<T>>(
25
+ value: createAmazonBedrockEventStreamDecoder<ParseResult<T>>(
26
26
  response.body,
27
27
  async (event, controller) => {
28
28
  if (event.messageType === 'event') {
@@ -1,38 +1,55 @@
1
- import {
1
+ import type {
2
2
  ImageModelV4,
3
3
  ImageModelV4File,
4
4
  SharedV4Warning,
5
5
  } from '@ai-sdk/provider';
6
6
  import {
7
- FetchFunction,
8
- Resolvable,
9
7
  combineHeaders,
10
8
  convertUint8ArrayToBase64,
11
9
  createJsonErrorResponseHandler,
12
10
  createJsonResponseHandler,
13
11
  postJsonToApi,
14
12
  resolve,
13
+ serializeModelOptions,
14
+ WORKFLOW_SERIALIZE,
15
+ WORKFLOW_DESERIALIZE,
16
+ type FetchFunction,
17
+ type Resolvable,
15
18
  } from '@ai-sdk/provider-utils';
16
19
  import {
17
- BedrockImageModelId,
18
20
  modelMaxImagesPerCall,
19
- } from './bedrock-image-settings';
20
- import { BedrockErrorSchema } from './bedrock-error';
21
+ type AmazonBedrockImageModelId,
22
+ } from './amazon-bedrock-image-settings';
23
+ import { AmazonBedrockErrorSchema } from './amazon-bedrock-error';
21
24
  import { z } from 'zod/v4';
22
25
 
23
- type BedrockImageModelConfig = {
26
+ type AmazonBedrockImageModelConfig = {
24
27
  baseUrl: () => string;
25
- headers: Resolvable<Record<string, string | undefined>>;
28
+ headers?: Resolvable<Record<string, string | undefined>>;
26
29
  fetch?: FetchFunction;
27
30
  _internal?: {
28
31
  currentDate?: () => Date;
29
32
  };
30
33
  };
31
34
 
32
- export class BedrockImageModel implements ImageModelV4 {
35
+ export class AmazonBedrockImageModel implements ImageModelV4 {
33
36
  readonly specificationVersion = 'v4';
34
37
  readonly provider = 'amazon-bedrock';
35
38
 
39
+ static [WORKFLOW_SERIALIZE](model: AmazonBedrockImageModel) {
40
+ return serializeModelOptions({
41
+ modelId: model.modelId,
42
+ config: model.config,
43
+ });
44
+ }
45
+
46
+ static [WORKFLOW_DESERIALIZE](options: {
47
+ modelId: string;
48
+ config: AmazonBedrockImageModelConfig;
49
+ }) {
50
+ return new AmazonBedrockImageModel(options.modelId, options.config);
51
+ }
52
+
36
53
  get maxImagesPerCall(): number {
37
54
  return modelMaxImagesPerCall[this.modelId] ?? 1;
38
55
  }
@@ -43,8 +60,8 @@ export class BedrockImageModel implements ImageModelV4 {
43
60
  }
44
61
 
45
62
  constructor(
46
- readonly modelId: BedrockImageModelId,
47
- private readonly config: BedrockImageModelConfig,
63
+ readonly modelId: AmazonBedrockImageModelId,
64
+ private readonly config: AmazonBedrockImageModelConfig,
48
65
  ) {}
49
66
 
50
67
  async doGenerate({
@@ -66,17 +83,22 @@ export class BedrockImageModel implements ImageModelV4 {
66
83
 
67
84
  const hasFiles = files != null && files.length > 0;
68
85
 
86
+ // Prefer the new `amazonBedrock` providerOptions key; fall back to the
87
+ // legacy `bedrock` key for backward compatibility.
88
+ const amazonBedrockOptions = (providerOptions?.amazonBedrock ??
89
+ providerOptions?.bedrock) as Record<string, any> | undefined;
90
+
69
91
  // Build image generation config (common to most modes)
70
92
  const imageGenerationConfig = {
71
93
  ...(width ? { width } : {}),
72
94
  ...(height ? { height } : {}),
73
95
  ...(seed ? { seed } : {}),
74
96
  ...(n ? { numberOfImages: n } : {}),
75
- ...(providerOptions?.bedrock?.quality
76
- ? { quality: providerOptions.bedrock.quality }
97
+ ...(amazonBedrockOptions?.quality
98
+ ? { quality: amazonBedrockOptions.quality }
77
99
  : {}),
78
- ...(providerOptions?.bedrock?.cfgScale
79
- ? { cfgScale: providerOptions.bedrock.cfgScale }
100
+ ...(amazonBedrockOptions?.cfgScale
101
+ ? { cfgScale: amazonBedrockOptions.cfgScale }
80
102
  : {}),
81
103
  };
82
104
 
@@ -85,11 +107,11 @@ export class BedrockImageModel implements ImageModelV4 {
85
107
  if (hasFiles) {
86
108
  // Check if mask is actually provided (has valid data, not just an empty object)
87
109
  const hasMask = mask?.type != null;
88
- const hasMaskPrompt = providerOptions?.bedrock?.maskPrompt != null;
110
+ const hasMaskPrompt = amazonBedrockOptions?.maskPrompt != null;
89
111
 
90
112
  // Determine task type from provider options, or infer from mask presence
91
113
  const taskType =
92
- providerOptions?.bedrock?.taskType ??
114
+ amazonBedrockOptions?.taskType ??
93
115
  (hasMask || hasMaskPrompt ? 'INPAINTING' : 'IMAGE_VARIATION');
94
116
 
95
117
  const sourceImageBase64 = getBase64Data(files[0]);
@@ -99,8 +121,8 @@ export class BedrockImageModel implements ImageModelV4 {
99
121
  const inPaintingParams: Record<string, unknown> = {
100
122
  image: sourceImageBase64,
101
123
  ...(prompt ? { text: prompt } : {}),
102
- ...(providerOptions?.bedrock?.negativeText
103
- ? { negativeText: providerOptions.bedrock.negativeText }
124
+ ...(amazonBedrockOptions?.negativeText
125
+ ? { negativeText: amazonBedrockOptions.negativeText }
104
126
  : {}),
105
127
  };
106
128
 
@@ -108,7 +130,7 @@ export class BedrockImageModel implements ImageModelV4 {
108
130
  if (hasMask) {
109
131
  inPaintingParams.maskImage = getBase64Data(mask);
110
132
  } else if (hasMaskPrompt) {
111
- inPaintingParams.maskPrompt = providerOptions.bedrock.maskPrompt;
133
+ inPaintingParams.maskPrompt = amazonBedrockOptions.maskPrompt;
112
134
  }
113
135
 
114
136
  args = {
@@ -123,11 +145,11 @@ export class BedrockImageModel implements ImageModelV4 {
123
145
  const outPaintingParams: Record<string, unknown> = {
124
146
  image: sourceImageBase64,
125
147
  ...(prompt ? { text: prompt } : {}),
126
- ...(providerOptions?.bedrock?.negativeText
127
- ? { negativeText: providerOptions.bedrock.negativeText }
148
+ ...(amazonBedrockOptions?.negativeText
149
+ ? { negativeText: amazonBedrockOptions.negativeText }
128
150
  : {}),
129
- ...(providerOptions?.bedrock?.outPaintingMode
130
- ? { outPaintingMode: providerOptions.bedrock.outPaintingMode }
151
+ ...(amazonBedrockOptions?.outPaintingMode
152
+ ? { outPaintingMode: amazonBedrockOptions.outPaintingMode }
131
153
  : {}),
132
154
  };
133
155
 
@@ -135,7 +157,7 @@ export class BedrockImageModel implements ImageModelV4 {
135
157
  if (hasMask) {
136
158
  outPaintingParams.maskImage = getBase64Data(mask);
137
159
  } else if (hasMaskPrompt) {
138
- outPaintingParams.maskPrompt = providerOptions.bedrock.maskPrompt;
160
+ outPaintingParams.maskPrompt = amazonBedrockOptions.maskPrompt;
139
161
  }
140
162
 
141
163
  args = {
@@ -164,13 +186,12 @@ export class BedrockImageModel implements ImageModelV4 {
164
186
  const imageVariationParams: Record<string, unknown> = {
165
187
  images,
166
188
  ...(prompt ? { text: prompt } : {}),
167
- ...(providerOptions?.bedrock?.negativeText
168
- ? { negativeText: providerOptions.bedrock.negativeText }
189
+ ...(amazonBedrockOptions?.negativeText
190
+ ? { negativeText: amazonBedrockOptions.negativeText }
169
191
  : {}),
170
- ...(providerOptions?.bedrock?.similarityStrength != null
192
+ ...(amazonBedrockOptions?.similarityStrength != null
171
193
  ? {
172
- similarityStrength:
173
- providerOptions.bedrock.similarityStrength,
194
+ similarityStrength: amazonBedrockOptions.similarityStrength,
174
195
  }
175
196
  : {}),
176
197
  };
@@ -192,14 +213,14 @@ export class BedrockImageModel implements ImageModelV4 {
192
213
  taskType: 'TEXT_IMAGE',
193
214
  textToImageParams: {
194
215
  text: prompt,
195
- ...(providerOptions?.bedrock?.negativeText
216
+ ...(amazonBedrockOptions?.negativeText
196
217
  ? {
197
- negativeText: providerOptions.bedrock.negativeText,
218
+ negativeText: amazonBedrockOptions.negativeText,
198
219
  }
199
220
  : {}),
200
- ...(providerOptions?.bedrock?.style
221
+ ...(amazonBedrockOptions?.style
201
222
  ? {
202
- style: providerOptions.bedrock.style,
223
+ style: amazonBedrockOptions.style,
203
224
  }
204
225
  : {}),
205
226
  },
@@ -220,15 +241,18 @@ export class BedrockImageModel implements ImageModelV4 {
220
241
  const { value: response, responseHeaders } = await postJsonToApi({
221
242
  url: this.getUrl(this.modelId),
222
243
  headers: await resolve(
223
- combineHeaders(await resolve(this.config.headers), headers),
244
+ combineHeaders(
245
+ this.config.headers ? await resolve(this.config.headers) : undefined,
246
+ headers,
247
+ ),
224
248
  ),
225
249
  body: args,
226
250
  failedResponseHandler: createJsonErrorResponseHandler({
227
- errorSchema: BedrockErrorSchema,
251
+ errorSchema: AmazonBedrockErrorSchema,
228
252
  errorToMessage: error => `${error.type}: ${error.message}`,
229
253
  }),
230
254
  successfulResponseHandler: createJsonResponseHandler(
231
- bedrockImageResponseSchema,
255
+ amazonBedrockImageResponseSchema,
232
256
  ),
233
257
  abortSignal,
234
258
  fetch: this.config.fetch,
@@ -283,7 +307,7 @@ function getBase64Data(file: ImageModelV4File): string {
283
307
 
284
308
  // minimal version of the schema, focussed on what is needed for the implementation
285
309
  // this approach limits breakages when the API changes and increases efficiency
286
- const bedrockImageResponseSchema = z.object({
310
+ const amazonBedrockImageResponseSchema = z.object({
287
311
  // Normal successful response
288
312
  images: z.array(z.string()).optional(),
289
313
 
@@ -0,0 +1,9 @@
1
+ export type AmazonBedrockImageModelId =
2
+ | 'amazon.nova-canvas-v1:0'
3
+ | (string & {});
4
+
5
+ // https://docs.aws.amazon.com/nova/latest/userguide/image-gen-req-resp-structure.html
6
+ export const modelMaxImagesPerCall: Record<AmazonBedrockImageModelId, number> =
7
+ {
8
+ 'amazon.nova-canvas-v1:0': 5,
9
+ };
@@ -1,15 +1,18 @@
1
1
  import {
2
- JSONObject,
3
- LanguageModelV4CallOptions,
4
- SharedV4Warning,
5
2
  UnsupportedFunctionalityError,
3
+ type JSONObject,
4
+ type LanguageModelV4CallOptions,
5
+ type SharedV4Warning,
6
6
  } from '@ai-sdk/provider';
7
7
  import { asSchema } from '@ai-sdk/provider-utils';
8
8
  import {
9
9
  anthropicTools,
10
10
  prepareTools as prepareAnthropicTools,
11
11
  } from '@ai-sdk/anthropic/internal';
12
- import { BedrockTool, BedrockToolConfiguration } from './bedrock-api-types';
12
+ import type {
13
+ AmazonBedrockTool,
14
+ AmazonBedrockToolConfiguration,
15
+ } from './amazon-bedrock-api-types';
13
16
 
14
17
  export async function prepareTools({
15
18
  tools,
@@ -20,7 +23,7 @@ export async function prepareTools({
20
23
  toolChoice?: LanguageModelV4CallOptions['toolChoice'];
21
24
  modelId: string;
22
25
  }): Promise<{
23
- toolConfig: BedrockToolConfiguration;
26
+ toolConfig: AmazonBedrockToolConfiguration;
24
27
  additionalTools: Record<string, unknown> | undefined;
25
28
  betas: Set<string>;
26
29
  toolWarnings: SharedV4Warning[];
@@ -68,7 +71,7 @@ export async function prepareTools({
68
71
  const functionTools = supportedTools.filter(t => t.type === 'function');
69
72
 
70
73
  let additionalTools: Record<string, unknown> | undefined = undefined;
71
- const bedrockTools: BedrockTool[] = [];
74
+ const amazonBedrockTools: AmazonBedrockTool[] = [];
72
75
 
73
76
  const usingAnthropicTools = isAnthropicModel && ProviderTools.length > 0;
74
77
 
@@ -105,7 +108,7 @@ export async function prepareTools({
105
108
 
106
109
  if (toolFactory != null) {
107
110
  const fullToolDefinition = (toolFactory as (args: any) => any)({});
108
- bedrockTools.push({
111
+ amazonBedrockTools.push({
109
112
  toolSpec: {
110
113
  name: tool.name,
111
114
  inputSchema: {
@@ -131,7 +134,7 @@ export async function prepareTools({
131
134
  : functionTools;
132
135
 
133
136
  for (const tool of filteredFunctionTools) {
134
- bedrockTools.push({
137
+ amazonBedrockTools.push({
135
138
  toolSpec: {
136
139
  name: tool.name,
137
140
  ...(tool.description?.trim() !== ''
@@ -146,22 +149,23 @@ export async function prepareTools({
146
149
  }
147
150
 
148
151
  // Handle toolChoice for standard Bedrock tools, but NOT for Anthropic provider-defined tools
149
- let bedrockToolChoice: BedrockToolConfiguration['toolChoice'] = undefined;
150
- if (!usingAnthropicTools && bedrockTools.length > 0 && toolChoice) {
152
+ let amazonBedrockToolChoice: AmazonBedrockToolConfiguration['toolChoice'] =
153
+ undefined;
154
+ if (!usingAnthropicTools && amazonBedrockTools.length > 0 && toolChoice) {
151
155
  const type = toolChoice.type;
152
156
  switch (type) {
153
157
  case 'auto':
154
- bedrockToolChoice = { auto: {} };
158
+ amazonBedrockToolChoice = { auto: {} };
155
159
  break;
156
160
  case 'required':
157
- bedrockToolChoice = { any: {} };
161
+ amazonBedrockToolChoice = { any: {} };
158
162
  break;
159
163
  case 'none':
160
- bedrockTools.length = 0;
161
- bedrockToolChoice = undefined;
164
+ amazonBedrockTools.length = 0;
165
+ amazonBedrockToolChoice = undefined;
162
166
  break;
163
167
  case 'tool':
164
- bedrockToolChoice = { tool: { name: toolChoice.toolName } };
168
+ amazonBedrockToolChoice = { tool: { name: toolChoice.toolName } };
165
169
  break;
166
170
  default: {
167
171
  const _exhaustiveCheck: never = type;
@@ -172,9 +176,9 @@ export async function prepareTools({
172
176
  }
173
177
  }
174
178
 
175
- const toolConfig: BedrockToolConfiguration =
176
- bedrockTools.length > 0
177
- ? { tools: bedrockTools, toolChoice: bedrockToolChoice }
179
+ const toolConfig: AmazonBedrockToolConfiguration =
180
+ amazonBedrockTools.length > 0
181
+ ? { tools: amazonBedrockTools, toolChoice: amazonBedrockToolChoice }
178
182
  : {};
179
183
 
180
184
  return {