@ai-sdk/amazon-bedrock 4.0.29 → 4.0.31

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.
@@ -758,6 +758,13 @@ The following optional provider options are available for Bedrock Titan embeddin
758
758
  | `cohere.embed-english-v3` | 1024 | <Cross size={18} /> |
759
759
  | `cohere.embed-multilingual-v3` | 1024 | <Cross size={18} /> |
760
760
 
761
+ <Note>
762
+ Cohere embedding models on Bedrock require an <code>input_type</code>. Set it
763
+ via
764
+ <code>providerOptions.bedrock.inputType</code> (defaults to{' '}
765
+ <code>search_query</code>).
766
+ </Note>
767
+
761
768
  ## Reranking Models
762
769
 
763
770
  You can create models that call the [Bedrock Rerank API](https://docs.aws.amazon.com/bedrock/latest/userguide/rerank-api.html)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/amazon-bedrock",
3
- "version": "4.0.29",
3
+ "version": "4.0.31",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -67,11 +67,41 @@ export class BedrockEmbeddingModel implements EmbeddingModelV3 {
67
67
  })) ?? {};
68
68
 
69
69
  // https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html
70
- const args = {
71
- inputText: values[0],
72
- dimensions: bedrockOptions.dimensions,
73
- normalize: bedrockOptions.normalize,
74
- };
70
+ //
71
+ // Note: Different embedding model families expect different request/response
72
+ // payloads (e.g. Titan vs Cohere vs Nova). We keep the public interface stable and
73
+ // adapt here based on the modelId.
74
+ const isNovaModel =
75
+ this.modelId.startsWith('amazon.nova-') && this.modelId.includes('embed');
76
+ const isCohereModel = this.modelId.startsWith('cohere.embed-');
77
+
78
+ const args = isNovaModel
79
+ ? {
80
+ taskType: 'SINGLE_EMBEDDING',
81
+ singleEmbeddingParams: {
82
+ embeddingPurpose:
83
+ bedrockOptions.embeddingPurpose ?? 'GENERIC_INDEX',
84
+ embeddingDimension: bedrockOptions.embeddingDimension ?? 1024,
85
+ text: {
86
+ truncationMode: bedrockOptions.truncate ?? 'END',
87
+ value: values[0],
88
+ },
89
+ },
90
+ }
91
+ : isCohereModel
92
+ ? {
93
+ // Cohere embedding models on Bedrock require `input_type`.
94
+ // Without it, the service attempts other schema branches and rejects the request.
95
+ input_type: bedrockOptions.inputType ?? 'search_query',
96
+ texts: [values[0]],
97
+ truncate: bedrockOptions.truncate,
98
+ }
99
+ : {
100
+ inputText: values[0],
101
+ dimensions: bedrockOptions.dimensions,
102
+ normalize: bedrockOptions.normalize,
103
+ };
104
+
75
105
  const url = this.getUrl(this.modelId);
76
106
  const { value: response } = await postJsonToApi({
77
107
  url,
@@ -90,15 +120,69 @@ export class BedrockEmbeddingModel implements EmbeddingModelV3 {
90
120
  abortSignal,
91
121
  });
92
122
 
123
+ // Extract embedding based on response format
124
+ let embedding: number[];
125
+ if ('embedding' in response) {
126
+ // Titan response
127
+ embedding = response.embedding;
128
+ } else if (Array.isArray(response.embeddings)) {
129
+ const firstEmbedding = response.embeddings[0];
130
+ if (
131
+ typeof firstEmbedding === 'object' &&
132
+ firstEmbedding !== null &&
133
+ 'embeddingType' in firstEmbedding
134
+ ) {
135
+ // Nova response
136
+ embedding = firstEmbedding.embedding;
137
+ } else {
138
+ // Cohere v3 response
139
+ embedding = firstEmbedding as number[];
140
+ }
141
+ } else {
142
+ // Cohere v4 response
143
+ embedding = response.embeddings.float[0];
144
+ }
145
+
146
+ // Extract token count based on response format
147
+ const tokens =
148
+ 'inputTextTokenCount' in response
149
+ ? response.inputTextTokenCount // Titan response
150
+ : 'inputTokenCount' in response
151
+ ? (response.inputTokenCount ?? 0) // Nova response
152
+ : NaN; // Cohere doesn't return token count
153
+
93
154
  return {
155
+ embeddings: [embedding],
156
+ usage: { tokens },
94
157
  warnings: [],
95
- embeddings: [response.embedding],
96
- usage: { tokens: response.inputTextTokenCount },
97
158
  };
98
159
  }
99
160
  }
100
161
 
101
- const BedrockEmbeddingResponseSchema = z.object({
102
- embedding: z.array(z.number()),
103
- inputTextTokenCount: z.number(),
104
- });
162
+ const BedrockEmbeddingResponseSchema = z.union([
163
+ // Titan-style response
164
+ z.object({
165
+ embedding: z.array(z.number()),
166
+ inputTextTokenCount: z.number(),
167
+ }),
168
+ // Nova-style response
169
+ z.object({
170
+ embeddings: z.array(
171
+ z.object({
172
+ embeddingType: z.string(),
173
+ embedding: z.array(z.number()),
174
+ }),
175
+ ),
176
+ inputTokenCount: z.number().optional(),
177
+ }),
178
+ // Cohere v3-style response
179
+ z.object({
180
+ embeddings: z.array(z.array(z.number())),
181
+ }),
182
+ // Cohere v4-style response
183
+ z.object({
184
+ embeddings: z.object({
185
+ float: z.array(z.array(z.number())),
186
+ }),
187
+ }),
188
+ ]);
@@ -21,4 +21,46 @@ Flag indicating whether or not to normalize the output embeddings. Defaults to t
21
21
  Only supported in amazon.titan-embed-text-v2:0.
22
22
  */
23
23
  normalize: z.boolean().optional(),
24
+
25
+ /**
26
+ The number of dimensions for Nova embedding models (defaults to 1024).
27
+ Supported values: 256, 384, 1024, 3072.
28
+ Only supported in amazon.nova-* embedding models.
29
+ */
30
+ embeddingDimension: z
31
+ .union([z.literal(256), z.literal(384), z.literal(1024), z.literal(3072)])
32
+ .optional(),
33
+
34
+ /**
35
+ The purpose of the embedding. Defaults to 'GENERIC_INDEX'.
36
+ Only supported in amazon.nova-* embedding models.
37
+ */
38
+ embeddingPurpose: z
39
+ .enum([
40
+ 'GENERIC_INDEX',
41
+ 'TEXT_RETRIEVAL',
42
+ 'IMAGE_RETRIEVAL',
43
+ 'VIDEO_RETRIEVAL',
44
+ 'DOCUMENT_RETRIEVAL',
45
+ 'AUDIO_RETRIEVAL',
46
+ 'GENERIC_RETRIEVAL',
47
+ 'CLASSIFICATION',
48
+ 'CLUSTERING',
49
+ ])
50
+ .optional(),
51
+
52
+ /**
53
+ Input type for Cohere embedding models on Bedrock.
54
+ Common values: `search_document`, `search_query`, `classification`, `clustering`.
55
+ If not set, the provider defaults to `search_query`.
56
+ */
57
+ inputType: z
58
+ .enum(['search_document', 'search_query', 'classification', 'clustering'])
59
+ .optional(),
60
+
61
+ /**
62
+ Truncation behavior when input exceeds the model's context length.
63
+ Supported in Cohere and Nova embedding models. Defaults to 'END' for Nova models.
64
+ */
65
+ truncate: z.enum(['NONE', 'START', 'END']).optional(),
24
66
  });