@ai-sdk/cohere 3.0.32 → 3.0.34

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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # @ai-sdk/cohere
2
2
 
3
+ ## 3.0.34
4
+
5
+ ### Patch Changes
6
+
7
+ - 563e9dd: feat(provider/cohere): add support for passing images to Cohere models
8
+
9
+ ## 3.0.33
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [7beadf0]
14
+ - @ai-sdk/provider-utils@4.0.26
15
+
3
16
  ## 3.0.32
4
17
 
5
18
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -2,7 +2,7 @@ import { z } from 'zod/v4';
2
2
  import { ProviderV3, LanguageModelV3, EmbeddingModelV3, RerankingModelV3 } from '@ai-sdk/provider';
3
3
  import { FetchFunction } from '@ai-sdk/provider-utils';
4
4
 
5
- type CohereChatModelId = 'command-a-03-2025' | 'command-a-reasoning-08-2025' | 'command-r7b-12-2024' | 'command-r-plus-04-2024' | 'command-r-plus' | 'command-r-08-2024' | 'command-r-03-2024' | 'command-r' | 'command' | 'command-nightly' | 'command-light' | 'command-light-nightly' | (string & {});
5
+ type CohereChatModelId = 'command-a-03-2025' | 'command-a-reasoning-08-2025' | 'command-a-vision-07-2025' | 'command-r7b-12-2024' | 'command-r-plus-04-2024' | 'command-r-plus' | 'command-r-08-2024' | 'command-r-03-2024' | 'command-r' | 'command' | 'command-nightly' | 'command-light' | 'command-light-nightly' | (string & {});
6
6
  declare const cohereLanguageModelOptions: z.ZodObject<{
7
7
  thinking: z.ZodOptional<z.ZodObject<{
8
8
  type: z.ZodOptional<z.ZodEnum<{
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import { z } from 'zod/v4';
2
2
  import { ProviderV3, LanguageModelV3, EmbeddingModelV3, RerankingModelV3 } from '@ai-sdk/provider';
3
3
  import { FetchFunction } from '@ai-sdk/provider-utils';
4
4
 
5
- type CohereChatModelId = 'command-a-03-2025' | 'command-a-reasoning-08-2025' | 'command-r7b-12-2024' | 'command-r-plus-04-2024' | 'command-r-plus' | 'command-r-08-2024' | 'command-r-03-2024' | 'command-r' | 'command' | 'command-nightly' | 'command-light' | 'command-light-nightly' | (string & {});
5
+ type CohereChatModelId = 'command-a-03-2025' | 'command-a-reasoning-08-2025' | 'command-a-vision-07-2025' | 'command-r7b-12-2024' | 'command-r-plus-04-2024' | 'command-r-plus' | 'command-r-08-2024' | 'command-r-03-2024' | 'command-r' | 'command' | 'command-nightly' | 'command-light' | 'command-light-nightly' | (string & {});
6
6
  declare const cohereLanguageModelOptions: z.ZodObject<{
7
7
  thinking: z.ZodOptional<z.ZodObject<{
8
8
  type: z.ZodOptional<z.ZodEnum<{
package/dist/index.js CHANGED
@@ -28,10 +28,10 @@ module.exports = __toCommonJS(index_exports);
28
28
 
29
29
  // src/cohere-provider.ts
30
30
  var import_provider4 = require("@ai-sdk/provider");
31
- var import_provider_utils7 = require("@ai-sdk/provider-utils");
31
+ var import_provider_utils8 = require("@ai-sdk/provider-utils");
32
32
 
33
33
  // src/cohere-chat-language-model.ts
34
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
34
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
35
35
  var import_v43 = require("zod/v4");
36
36
 
37
37
  // src/cohere-chat-options.ts
@@ -50,6 +50,14 @@ var cohereLanguageModelOptions = import_v4.z.object({
50
50
  tokenBudget: import_v4.z.number().optional()
51
51
  }).optional()
52
52
  });
53
+ var cohereImagePartProviderOptions = import_v4.z.object({
54
+ /**
55
+ * Image fidelity level passed through as `image_url.detail` on the Cohere chat API.
56
+ *
57
+ * @see https://docs.cohere.com/docs/image-inputs
58
+ */
59
+ detail: import_v4.z.enum(["auto", "low", "high"]).optional()
60
+ });
53
61
 
54
62
  // src/cohere-error.ts
55
63
  var import_provider_utils = require("@ai-sdk/provider-utils");
@@ -157,7 +165,9 @@ function convertCohereUsage(tokens) {
157
165
 
158
166
  // src/convert-to-cohere-chat-prompt.ts
159
167
  var import_provider2 = require("@ai-sdk/provider");
160
- function convertToCohereChatPrompt(prompt) {
168
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
169
+ async function convertToCohereChatPrompt(prompt) {
170
+ var _a, _b;
161
171
  const messages = [];
162
172
  const documents = [];
163
173
  const warnings = [];
@@ -168,43 +178,69 @@ function convertToCohereChatPrompt(prompt) {
168
178
  break;
169
179
  }
170
180
  case "user": {
171
- messages.push({
172
- role: "user",
173
- content: content.map((part) => {
174
- var _a;
175
- switch (part.type) {
176
- case "text": {
177
- return part.text;
181
+ const userContentParts = [];
182
+ let hasImage = false;
183
+ for (const part of content) {
184
+ switch (part.type) {
185
+ case "text": {
186
+ if (part.text.length > 0) {
187
+ userContentParts.push({ type: "text", text: part.text });
178
188
  }
179
- case "file": {
180
- let textContent;
181
- if (typeof part.data === "string") {
182
- textContent = part.data;
183
- } else if (part.data instanceof Uint8Array) {
184
- if (!(((_a = part.mediaType) == null ? void 0 : _a.startsWith("text/")) || part.mediaType === "application/json")) {
185
- throw new import_provider2.UnsupportedFunctionalityError({
186
- functionality: `document media type: ${part.mediaType}`,
187
- message: `Media type '${part.mediaType}' is not supported. Supported media types are: text/* and application/json.`
188
- });
189
+ break;
190
+ }
191
+ case "file": {
192
+ if (isImageMediaType(part.mediaType)) {
193
+ hasImage = true;
194
+ const url = buildImageUrl({ part });
195
+ const cohereOptions = (_a = await (0, import_provider_utils2.parseProviderOptions)({
196
+ provider: "cohere",
197
+ providerOptions: part.providerOptions,
198
+ schema: cohereImagePartProviderOptions
199
+ })) != null ? _a : {};
200
+ userContentParts.push({
201
+ type: "image_url",
202
+ image_url: {
203
+ url,
204
+ ...cohereOptions.detail ? { detail: cohereOptions.detail } : {}
189
205
  }
190
- textContent = new TextDecoder().decode(part.data);
191
- } else {
206
+ });
207
+ break;
208
+ }
209
+ let textContent;
210
+ if (typeof part.data === "string") {
211
+ textContent = part.data;
212
+ } else if (part.data instanceof Uint8Array) {
213
+ if (!(((_b = part.mediaType) == null ? void 0 : _b.startsWith("text/")) || part.mediaType === "application/json")) {
192
214
  throw new import_provider2.UnsupportedFunctionalityError({
193
- functionality: "File URL data",
194
- message: "URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue."
215
+ functionality: `document media type: ${part.mediaType}`,
216
+ message: `Media type '${part.mediaType}' is not supported. Supported media types are: text/* and application/json.`
195
217
  });
196
218
  }
197
- documents.push({
198
- data: {
199
- text: textContent,
200
- title: part.filename
201
- }
219
+ textContent = new TextDecoder().decode(part.data);
220
+ } else {
221
+ throw new import_provider2.UnsupportedFunctionalityError({
222
+ functionality: "File URL data",
223
+ message: "URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue."
202
224
  });
203
- return "";
204
225
  }
226
+ documents.push({
227
+ data: {
228
+ text: textContent,
229
+ title: part.filename
230
+ }
231
+ });
232
+ break;
205
233
  }
206
- }).join("")
207
- });
234
+ }
235
+ }
236
+ if (hasImage) {
237
+ messages.push({ role: "user", content: userContentParts });
238
+ } else {
239
+ messages.push({
240
+ role: "user",
241
+ content: userContentParts.map((p) => p.type === "text" ? p.text : "").join("")
242
+ });
243
+ }
208
244
  break;
209
245
  }
210
246
  case "assistant": {
@@ -240,7 +276,7 @@ function convertToCohereChatPrompt(prompt) {
240
276
  case "tool": {
241
277
  messages.push(
242
278
  ...content.filter((toolResult) => toolResult.type !== "tool-approval-response").map((toolResult) => {
243
- var _a;
279
+ var _a2;
244
280
  const output = toolResult.output;
245
281
  let contentValue;
246
282
  switch (output.type) {
@@ -249,7 +285,7 @@ function convertToCohereChatPrompt(prompt) {
249
285
  contentValue = output.value;
250
286
  break;
251
287
  case "execution-denied":
252
- contentValue = (_a = output.reason) != null ? _a : "Tool execution denied.";
288
+ contentValue = (_a2 = output.reason) != null ? _a2 : "Tool execution denied.";
253
289
  break;
254
290
  case "content":
255
291
  case "json":
@@ -274,6 +310,16 @@ function convertToCohereChatPrompt(prompt) {
274
310
  }
275
311
  return { messages, documents, warnings };
276
312
  }
313
+ function isImageMediaType(mediaType) {
314
+ return mediaType === "image" || (mediaType == null ? void 0 : mediaType.startsWith("image/")) === true;
315
+ }
316
+ function buildImageUrl({ part }) {
317
+ if (part.data instanceof URL) {
318
+ return part.data.toString();
319
+ }
320
+ const mediaType = part.mediaType === "image" || part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
321
+ return `data:${mediaType};base64,${(0, import_provider_utils2.convertToBase64)(part.data)}`;
322
+ }
277
323
 
278
324
  // src/map-cohere-finish-reason.ts
279
325
  function mapCohereFinishReason(finishReason) {
@@ -297,7 +343,7 @@ var CohereChatLanguageModel = class {
297
343
  constructor(modelId, config) {
298
344
  this.specificationVersion = "v3";
299
345
  this.supportedUrls = {
300
- // No URLs are supported.
346
+ "image/*": [/^https?:\/\/.*$/]
301
347
  };
302
348
  this.modelId = modelId;
303
349
  this.config = config;
@@ -321,7 +367,7 @@ var CohereChatLanguageModel = class {
321
367
  providerOptions
322
368
  }) {
323
369
  var _a, _b;
324
- const cohereOptions = (_a = await (0, import_provider_utils2.parseProviderOptions)({
370
+ const cohereOptions = (_a = await (0, import_provider_utils3.parseProviderOptions)({
325
371
  provider: "cohere",
326
372
  providerOptions,
327
373
  schema: cohereLanguageModelOptions
@@ -330,7 +376,7 @@ var CohereChatLanguageModel = class {
330
376
  messages: chatPrompt,
331
377
  documents: cohereDocuments,
332
378
  warnings: promptWarnings
333
- } = convertToCohereChatPrompt(prompt);
379
+ } = await convertToCohereChatPrompt(prompt);
334
380
  const {
335
381
  tools: cohereTools,
336
382
  toolChoice: cohereToolChoice,
@@ -376,12 +422,12 @@ var CohereChatLanguageModel = class {
376
422
  responseHeaders,
377
423
  value: response,
378
424
  rawValue: rawResponse
379
- } = await (0, import_provider_utils2.postJsonToApi)({
425
+ } = await (0, import_provider_utils3.postJsonToApi)({
380
426
  url: `${this.config.baseURL}/chat`,
381
- headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
427
+ headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
382
428
  body: args,
383
429
  failedResponseHandler: cohereFailedResponseHandler,
384
- successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
430
+ successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
385
431
  cohereChatResponseSchema
386
432
  ),
387
433
  abortSignal: options.abortSignal,
@@ -445,12 +491,12 @@ var CohereChatLanguageModel = class {
445
491
  }
446
492
  async doStream(options) {
447
493
  const { args, warnings } = await this.getArgs(options);
448
- const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
494
+ const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
449
495
  url: `${this.config.baseURL}/chat`,
450
- headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
496
+ headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
451
497
  body: { ...args, stream: true },
452
498
  failedResponseHandler: cohereFailedResponseHandler,
453
- successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
499
+ successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
454
500
  cohereChatChunkSchema
455
501
  ),
456
502
  abortSignal: options.abortSignal,
@@ -783,7 +829,7 @@ var cohereChatChunkSchema = import_v43.z.discriminatedUnion("type", [
783
829
 
784
830
  // src/cohere-embedding-model.ts
785
831
  var import_provider3 = require("@ai-sdk/provider");
786
- var import_provider_utils3 = require("@ai-sdk/provider-utils");
832
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
787
833
  var import_v45 = require("zod/v4");
788
834
 
789
835
  // src/cohere-embedding-options.ts
@@ -836,7 +882,7 @@ var CohereEmbeddingModel = class {
836
882
  providerOptions
837
883
  }) {
838
884
  var _a;
839
- const embeddingOptions = await (0, import_provider_utils3.parseProviderOptions)({
885
+ const embeddingOptions = await (0, import_provider_utils4.parseProviderOptions)({
840
886
  provider: "cohere",
841
887
  providerOptions,
842
888
  schema: cohereEmbeddingModelOptions
@@ -853,9 +899,9 @@ var CohereEmbeddingModel = class {
853
899
  responseHeaders,
854
900
  value: response,
855
901
  rawValue
856
- } = await (0, import_provider_utils3.postJsonToApi)({
902
+ } = await (0, import_provider_utils4.postJsonToApi)({
857
903
  url: `${this.config.baseURL}/embed`,
858
- headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), headers),
904
+ headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), headers),
859
905
  body: {
860
906
  model: this.modelId,
861
907
  // The AI SDK only supports 'float' embeddings. Note that the Cohere API
@@ -868,7 +914,7 @@ var CohereEmbeddingModel = class {
868
914
  output_dimension: embeddingOptions == null ? void 0 : embeddingOptions.outputDimension
869
915
  },
870
916
  failedResponseHandler: cohereFailedResponseHandler,
871
- successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
917
+ successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
872
918
  cohereTextEmbeddingResponseSchema
873
919
  ),
874
920
  abortSignal,
@@ -894,13 +940,13 @@ var cohereTextEmbeddingResponseSchema = import_v45.z.object({
894
940
  });
895
941
 
896
942
  // src/reranking/cohere-reranking-model.ts
897
- var import_provider_utils6 = require("@ai-sdk/provider-utils");
943
+ var import_provider_utils7 = require("@ai-sdk/provider-utils");
898
944
 
899
945
  // src/reranking/cohere-reranking-api.ts
900
- var import_provider_utils4 = require("@ai-sdk/provider-utils");
946
+ var import_provider_utils5 = require("@ai-sdk/provider-utils");
901
947
  var import_v46 = require("zod/v4");
902
- var cohereRerankingResponseSchema = (0, import_provider_utils4.lazySchema)(
903
- () => (0, import_provider_utils4.zodSchema)(
948
+ var cohereRerankingResponseSchema = (0, import_provider_utils5.lazySchema)(
949
+ () => (0, import_provider_utils5.zodSchema)(
904
950
  import_v46.z.object({
905
951
  id: import_v46.z.string().nullish(),
906
952
  results: import_v46.z.array(
@@ -915,10 +961,10 @@ var cohereRerankingResponseSchema = (0, import_provider_utils4.lazySchema)(
915
961
  );
916
962
 
917
963
  // src/reranking/cohere-reranking-options.ts
918
- var import_provider_utils5 = require("@ai-sdk/provider-utils");
964
+ var import_provider_utils6 = require("@ai-sdk/provider-utils");
919
965
  var import_v47 = require("zod/v4");
920
- var cohereRerankingModelOptionsSchema = (0, import_provider_utils5.lazySchema)(
921
- () => (0, import_provider_utils5.zodSchema)(
966
+ var cohereRerankingModelOptionsSchema = (0, import_provider_utils6.lazySchema)(
967
+ () => (0, import_provider_utils6.zodSchema)(
922
968
  import_v47.z.object({
923
969
  maxTokensPerDoc: import_v47.z.number().optional(),
924
970
  priority: import_v47.z.number().optional()
@@ -946,7 +992,7 @@ var CohereRerankingModel = class {
946
992
  providerOptions
947
993
  }) {
948
994
  var _a;
949
- const rerankingOptions = await (0, import_provider_utils6.parseProviderOptions)({
995
+ const rerankingOptions = await (0, import_provider_utils7.parseProviderOptions)({
950
996
  provider: "cohere",
951
997
  providerOptions,
952
998
  schema: cohereRerankingModelOptionsSchema
@@ -963,9 +1009,9 @@ var CohereRerankingModel = class {
963
1009
  responseHeaders,
964
1010
  value: response,
965
1011
  rawValue
966
- } = await (0, import_provider_utils6.postJsonToApi)({
1012
+ } = await (0, import_provider_utils7.postJsonToApi)({
967
1013
  url: `${this.config.baseURL}/rerank`,
968
- headers: (0, import_provider_utils6.combineHeaders)(this.config.headers(), headers),
1014
+ headers: (0, import_provider_utils7.combineHeaders)(this.config.headers(), headers),
969
1015
  body: {
970
1016
  model: this.modelId,
971
1017
  query,
@@ -975,7 +1021,7 @@ var CohereRerankingModel = class {
975
1021
  priority: rerankingOptions == null ? void 0 : rerankingOptions.priority
976
1022
  },
977
1023
  failedResponseHandler: cohereFailedResponseHandler,
978
- successfulResponseHandler: (0, import_provider_utils6.createJsonResponseHandler)(
1024
+ successfulResponseHandler: (0, import_provider_utils7.createJsonResponseHandler)(
979
1025
  cohereRerankingResponseSchema
980
1026
  ),
981
1027
  abortSignal,
@@ -997,15 +1043,15 @@ var CohereRerankingModel = class {
997
1043
  };
998
1044
 
999
1045
  // src/version.ts
1000
- var VERSION = true ? "3.0.32" : "0.0.0-test";
1046
+ var VERSION = true ? "3.0.34" : "0.0.0-test";
1001
1047
 
1002
1048
  // src/cohere-provider.ts
1003
1049
  function createCohere(options = {}) {
1004
1050
  var _a;
1005
- const baseURL = (_a = (0, import_provider_utils7.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.cohere.com/v2";
1006
- const getHeaders = () => (0, import_provider_utils7.withUserAgentSuffix)(
1051
+ const baseURL = (_a = (0, import_provider_utils8.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.cohere.com/v2";
1052
+ const getHeaders = () => (0, import_provider_utils8.withUserAgentSuffix)(
1007
1053
  {
1008
- Authorization: `Bearer ${(0, import_provider_utils7.loadApiKey)({
1054
+ Authorization: `Bearer ${(0, import_provider_utils8.loadApiKey)({
1009
1055
  apiKey: options.apiKey,
1010
1056
  environmentVariableName: "COHERE_API_KEY",
1011
1057
  description: "Cohere"
@@ -1021,7 +1067,7 @@ function createCohere(options = {}) {
1021
1067
  baseURL,
1022
1068
  headers: getHeaders,
1023
1069
  fetch: options.fetch,
1024
- generateId: (_a2 = options.generateId) != null ? _a2 : import_provider_utils7.generateId
1070
+ generateId: (_a2 = options.generateId) != null ? _a2 : import_provider_utils8.generateId
1025
1071
  });
1026
1072
  };
1027
1073
  const createEmbeddingModel = (modelId) => new CohereEmbeddingModel(modelId, {