@ai-sdk/amazon-bedrock 4.0.29 → 4.0.30

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.30",
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-sdk/test-server": "1.0.3",
51
- "@vercel/ai-tsconfig": "0.0.0"
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"
@@ -67,11 +67,23 @@ 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). We keep the public interface stable and
73
+ // adapt here based on the modelId.
74
+ const args = this.modelId.startsWith('cohere.embed-')
75
+ ? {
76
+ // Cohere embedding models on Bedrock require `input_type`.
77
+ // Without it, the service attempts other schema branches and rejects the request.
78
+ input_type: bedrockOptions.inputType ?? 'search_query',
79
+ texts: [values[0]],
80
+ truncate: bedrockOptions.truncate,
81
+ }
82
+ : {
83
+ inputText: values[0],
84
+ dimensions: bedrockOptions.dimensions,
85
+ normalize: bedrockOptions.normalize,
86
+ };
75
87
  const url = this.getUrl(this.modelId);
76
88
  const { value: response } = await postJsonToApi({
77
89
  url,
@@ -90,15 +102,40 @@ export class BedrockEmbeddingModel implements EmbeddingModelV3 {
90
102
  abortSignal,
91
103
  });
92
104
 
105
+ const embedding =
106
+ 'embedding' in response
107
+ ? response.embedding
108
+ : Array.isArray(response.embeddings)
109
+ ? response.embeddings[0]
110
+ : response.embeddings.float[0];
111
+
93
112
  return {
94
113
  warnings: [],
95
- embeddings: [response.embedding],
96
- usage: { tokens: response.inputTextTokenCount },
114
+ embeddings: [embedding],
115
+ usage: {
116
+ tokens:
117
+ 'inputTextTokenCount' in response
118
+ ? response.inputTextTokenCount
119
+ : NaN,
120
+ },
97
121
  };
98
122
  }
99
123
  }
100
124
 
101
- const BedrockEmbeddingResponseSchema = z.object({
102
- embedding: z.array(z.number()),
103
- inputTextTokenCount: z.number(),
104
- });
125
+ const BedrockEmbeddingResponseSchema = z.union([
126
+ // Titan-style response
127
+ z.object({
128
+ embedding: z.array(z.number()),
129
+ inputTextTokenCount: z.number(),
130
+ }),
131
+ // Cohere v3-style response
132
+ z.object({
133
+ embeddings: z.array(z.array(z.number())),
134
+ }),
135
+ // Cohere v4-style response
136
+ z.object({
137
+ embeddings: z.object({
138
+ float: z.array(z.array(z.number())),
139
+ }),
140
+ }),
141
+ ]);
@@ -21,4 +21,18 @@ 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
+ Input type for Cohere embedding models on Bedrock.
27
+ Common values: `search_document`, `search_query`, `classification`, `clustering`.
28
+ If not set, the provider defaults to `search_query`.
29
+ */
30
+ inputType: z
31
+ .enum(['search_document', 'search_query', 'classification', 'clustering'])
32
+ .optional(),
33
+
34
+ /**
35
+ Truncation behavior for Cohere embedding models on Bedrock.
36
+ */
37
+ truncate: z.enum(['NONE', 'START', 'END']).optional(),
24
38
  });