@ai-sdk/cohere 4.0.0-beta.3 → 4.0.0-beta.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.
@@ -1,30 +1,33 @@
1
1
  import {
2
- EmbeddingModelV3,
3
2
  TooManyEmbeddingValuesForCallError,
3
+ type EmbeddingModelV4,
4
4
  } from '@ai-sdk/provider';
5
5
  import {
6
6
  combineHeaders,
7
7
  createJsonResponseHandler,
8
- FetchFunction,
9
8
  parseProviderOptions,
10
9
  postJsonToApi,
10
+ serializeModelOptions,
11
+ WORKFLOW_SERIALIZE,
12
+ WORKFLOW_DESERIALIZE,
13
+ type FetchFunction,
11
14
  } from '@ai-sdk/provider-utils';
12
15
  import { z } from 'zod/v4';
13
16
  import {
14
- CohereEmbeddingModelId,
15
17
  cohereEmbeddingModelOptions,
18
+ type CohereEmbeddingModelId,
16
19
  } from './cohere-embedding-options';
17
20
  import { cohereFailedResponseHandler } from './cohere-error';
18
21
 
19
22
  type CohereEmbeddingConfig = {
20
23
  provider: string;
21
24
  baseURL: string;
22
- headers: () => Record<string, string | undefined>;
25
+ headers?: () => Record<string, string | undefined>;
23
26
  fetch?: FetchFunction;
24
27
  };
25
28
 
26
- export class CohereEmbeddingModel implements EmbeddingModelV3 {
27
- readonly specificationVersion = 'v3';
29
+ export class CohereEmbeddingModel implements EmbeddingModelV4 {
30
+ readonly specificationVersion = 'v4';
28
31
  readonly modelId: CohereEmbeddingModelId;
29
32
 
30
33
  readonly maxEmbeddingsPerCall = 96;
@@ -32,6 +35,20 @@ export class CohereEmbeddingModel implements EmbeddingModelV3 {
32
35
 
33
36
  private readonly config: CohereEmbeddingConfig;
34
37
 
38
+ static [WORKFLOW_SERIALIZE](model: CohereEmbeddingModel) {
39
+ return serializeModelOptions({
40
+ modelId: model.modelId,
41
+ config: model.config,
42
+ });
43
+ }
44
+
45
+ static [WORKFLOW_DESERIALIZE](options: {
46
+ modelId: CohereEmbeddingModelId;
47
+ config: CohereEmbeddingConfig;
48
+ }) {
49
+ return new CohereEmbeddingModel(options.modelId, options.config);
50
+ }
51
+
35
52
  constructor(modelId: CohereEmbeddingModelId, config: CohereEmbeddingConfig) {
36
53
  this.modelId = modelId;
37
54
  this.config = config;
@@ -46,8 +63,8 @@ export class CohereEmbeddingModel implements EmbeddingModelV3 {
46
63
  headers,
47
64
  abortSignal,
48
65
  providerOptions,
49
- }: Parameters<EmbeddingModelV3['doEmbed']>[0]): Promise<
50
- Awaited<ReturnType<EmbeddingModelV3['doEmbed']>>
66
+ }: Parameters<EmbeddingModelV4['doEmbed']>[0]): Promise<
67
+ Awaited<ReturnType<EmbeddingModelV4['doEmbed']>>
51
68
  > {
52
69
  const embeddingOptions = await parseProviderOptions({
53
70
  provider: 'cohere',
@@ -70,7 +87,7 @@ export class CohereEmbeddingModel implements EmbeddingModelV3 {
70
87
  rawValue,
71
88
  } = await postJsonToApi({
72
89
  url: `${this.config.baseURL}/embed`,
73
- headers: combineHeaders(this.config.headers(), headers),
90
+ headers: combineHeaders(this.config.headers?.(), headers),
74
91
  body: {
75
92
  model: this.modelId,
76
93
  // The AI SDK only supports 'float' embeddings. Note that the Cohere API
@@ -1,16 +1,16 @@
1
1
  import {
2
- LanguageModelV3CallOptions,
3
- SharedV3Warning,
4
2
  UnsupportedFunctionalityError,
3
+ type LanguageModelV4CallOptions,
4
+ type SharedV4Warning,
5
5
  } from '@ai-sdk/provider';
6
- import { CohereToolChoice } from './cohere-chat-prompt';
6
+ import type { CohereToolChoice } from './cohere-chat-prompt';
7
7
 
8
8
  export function prepareTools({
9
9
  tools,
10
10
  toolChoice,
11
11
  }: {
12
- tools: LanguageModelV3CallOptions['tools'];
13
- toolChoice?: LanguageModelV3CallOptions['toolChoice'];
12
+ tools: LanguageModelV4CallOptions['tools'];
13
+ toolChoice?: LanguageModelV4CallOptions['toolChoice'];
14
14
  }): {
15
15
  tools:
16
16
  | Array<{
@@ -23,12 +23,12 @@ export function prepareTools({
23
23
  }>
24
24
  | undefined;
25
25
  toolChoice: CohereToolChoice;
26
- toolWarnings: SharedV3Warning[];
26
+ toolWarnings: SharedV4Warning[];
27
27
  } {
28
28
  // when the tools array is empty, change it to undefined to prevent errors:
29
29
  tools = tools?.length ? tools : undefined;
30
30
 
31
- const toolWarnings: SharedV3Warning[] = [];
31
+ const toolWarnings: SharedV4Warning[] = [];
32
32
 
33
33
  if (tools == null) {
34
34
  return { tools: undefined, toolChoice: undefined, toolWarnings };
@@ -1,63 +1,62 @@
1
1
  import {
2
- EmbeddingModelV3,
3
- LanguageModelV3,
4
2
  NoSuchModelError,
5
- RerankingModelV3,
6
- ProviderV3,
3
+ type EmbeddingModelV4,
4
+ type LanguageModelV4,
5
+ type RerankingModelV4,
6
+ type ProviderV4,
7
7
  } from '@ai-sdk/provider';
8
-
9
8
  import {
10
- FetchFunction,
11
9
  generateId,
12
10
  loadApiKey,
13
11
  withoutTrailingSlash,
14
12
  withUserAgentSuffix,
13
+ type FetchFunction,
15
14
  } from '@ai-sdk/provider-utils';
16
15
  import { CohereChatLanguageModel } from './cohere-chat-language-model';
17
- import { CohereChatModelId } from './cohere-chat-options';
16
+ import type { CohereChatModelId } from './cohere-chat-options';
18
17
  import { CohereEmbeddingModel } from './cohere-embedding-model';
19
- import { CohereRerankingModelId } from './reranking/cohere-reranking-options';
18
+ import type { CohereRerankingModelId } from './reranking/cohere-reranking-options';
20
19
  import { CohereRerankingModel } from './reranking/cohere-reranking-model';
21
- import { CohereEmbeddingModelId } from './cohere-embedding-options';
20
+ import type { CohereEmbeddingModelId } from './cohere-embedding-options';
22
21
  import { VERSION } from './version';
23
22
 
24
- export interface CohereProvider extends ProviderV3 {
25
- (modelId: CohereChatModelId): LanguageModelV3;
23
+ export interface CohereProvider extends ProviderV4 {
24
+ (modelId: CohereChatModelId): LanguageModelV4;
26
25
 
27
26
  /**
28
27
  * Creates a model for text generation.
29
28
  */
30
- languageModel(modelId: CohereChatModelId): LanguageModelV3;
29
+ languageModel(modelId: CohereChatModelId): LanguageModelV4;
31
30
 
32
31
  /**
33
32
  * Creates a model for text embeddings.
34
33
  */
35
- embedding(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
34
+ embedding(modelId: CohereEmbeddingModelId): EmbeddingModelV4;
36
35
 
37
36
  /**
38
37
  * Creates a model for text embeddings.
39
38
  */
40
- embeddingModel(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
39
+ embeddingModel(modelId: CohereEmbeddingModelId): EmbeddingModelV4;
41
40
 
42
41
  /**
43
42
  * @deprecated Use `embedding` instead.
44
43
  */
45
- textEmbedding(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
44
+ textEmbedding(modelId: CohereEmbeddingModelId): EmbeddingModelV4;
46
45
 
47
46
  /**
48
47
  * @deprecated Use `embeddingModel` instead.
49
48
  */
50
- textEmbeddingModel(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
49
+ textEmbeddingModel(modelId: CohereEmbeddingModelId): EmbeddingModelV4;
51
50
 
52
51
  /**
53
52
  * Creates a model for reranking.
54
53
  */
55
- reranking(modelId: CohereRerankingModelId): RerankingModelV3;
54
+ reranking(modelId: CohereRerankingModelId): RerankingModelV4;
56
55
 
57
56
  /**
58
57
  * Creates a model for reranking.
59
58
  */
60
- rerankingModel(modelId: CohereRerankingModelId): RerankingModelV3;
59
+ rerankingModel(modelId: CohereRerankingModelId): RerankingModelV4;
61
60
  }
62
61
 
63
62
  export interface CohereProviderSettings {
@@ -147,7 +146,7 @@ export function createCohere(
147
146
  return createChatModel(modelId);
148
147
  };
149
148
 
150
- provider.specificationVersion = 'v3' as const;
149
+ provider.specificationVersion = 'v4' as const;
151
150
  provider.languageModel = createChatModel;
152
151
  provider.embedding = createEmbeddingModel;
153
152
  provider.embeddingModel = createEmbeddingModel;
@@ -1,4 +1,4 @@
1
- import { LanguageModelV3Usage } from '@ai-sdk/provider';
1
+ import type { LanguageModelV4Usage } from '@ai-sdk/provider';
2
2
 
3
3
  export type CohereUsageTokens = {
4
4
  input_tokens: number;
@@ -7,7 +7,7 @@ export type CohereUsageTokens = {
7
7
 
8
8
  export function convertCohereUsage(
9
9
  tokens: CohereUsageTokens | undefined | null,
10
- ): LanguageModelV3Usage {
10
+ ): LanguageModelV4Usage {
11
11
  if (tokens == null) {
12
12
  return {
13
13
  inputTokens: {
@@ -1,20 +1,23 @@
1
1
  import {
2
- SharedV3Warning,
3
- LanguageModelV3Prompt,
4
2
  UnsupportedFunctionalityError,
3
+ type SharedV4Warning,
4
+ type LanguageModelV4Prompt,
5
5
  } from '@ai-sdk/provider';
6
- import { CohereAssistantMessage, CohereChatPrompt } from './cohere-chat-prompt';
6
+ import type {
7
+ CohereAssistantMessage,
8
+ CohereChatPrompt,
9
+ } from './cohere-chat-prompt';
7
10
 
8
- export function convertToCohereChatPrompt(prompt: LanguageModelV3Prompt): {
11
+ export function convertToCohereChatPrompt(prompt: LanguageModelV4Prompt): {
9
12
  messages: CohereChatPrompt;
10
13
  documents: Array<{
11
14
  data: { text: string; title?: string };
12
15
  }>;
13
- warnings: SharedV3Warning[];
16
+ warnings: SharedV4Warning[];
14
17
  } {
15
18
  const messages: CohereChatPrompt = [];
16
19
  const documents: Array<{ data: { text: string; title?: string } }> = [];
17
- const warnings: SharedV3Warning[] = [];
20
+ const warnings: SharedV4Warning[] = [];
18
21
 
19
22
  for (const { role, content } of prompt) {
20
23
  switch (role) {
@@ -33,32 +36,32 @@ export function convertToCohereChatPrompt(prompt: LanguageModelV3Prompt): {
33
36
  return part.text;
34
37
  }
35
38
  case 'file': {
36
- // Extract documents for RAG
37
39
  let textContent: string;
38
40
 
39
- if (typeof part.data === 'string') {
40
- // Base64 or text data
41
- textContent = part.data;
42
- } else if (part.data instanceof Uint8Array) {
43
- // Check if the media type is supported for text extraction
44
- if (
45
- !(
46
- part.mediaType?.startsWith('text/') ||
47
- part.mediaType === 'application/json'
48
- )
49
- ) {
41
+ switch (part.data.type) {
42
+ case 'reference': {
50
43
  throw new UnsupportedFunctionalityError({
51
- functionality: `document media type: ${part.mediaType}`,
52
- message: `Media type '${part.mediaType}' is not supported. Supported media types are: text/* and application/json.`,
44
+ functionality: 'file parts with provider references',
53
45
  });
54
46
  }
55
- textContent = new TextDecoder().decode(part.data);
56
- } else {
57
- throw new UnsupportedFunctionalityError({
58
- functionality: 'File URL data',
59
- message:
60
- 'URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue.',
61
- });
47
+ case 'url': {
48
+ throw new UnsupportedFunctionalityError({
49
+ functionality: 'File URL data',
50
+ message:
51
+ 'URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue.',
52
+ });
53
+ }
54
+ case 'text': {
55
+ textContent = part.data.text;
56
+ break;
57
+ }
58
+ case 'data': {
59
+ textContent =
60
+ typeof part.data.data === 'string'
61
+ ? part.data.data
62
+ : new TextDecoder().decode(part.data.data);
63
+ break;
64
+ }
62
65
  }
63
66
 
64
67
  documents.push({
@@ -68,8 +71,6 @@ export function convertToCohereChatPrompt(prompt: LanguageModelV3Prompt): {
68
71
  },
69
72
  });
70
73
 
71
- // Files are handled separately via the documents parameter
72
- // Return empty string to not include file content in message text
73
74
  return '';
74
75
  }
75
76
  }
@@ -126,7 +127,7 @@ export function convertToCohereChatPrompt(prompt: LanguageModelV3Prompt): {
126
127
  contentValue = output.value;
127
128
  break;
128
129
  case 'execution-denied':
129
- contentValue = output.reason ?? 'Tool execution denied.';
130
+ contentValue = output.reason ?? 'Tool call execution denied.';
130
131
  break;
131
132
  case 'content':
132
133
  case 'json':
@@ -1,8 +1,8 @@
1
- import { LanguageModelV3FinishReason } from '@ai-sdk/provider';
1
+ import type { LanguageModelV4FinishReason } from '@ai-sdk/provider';
2
2
 
3
3
  export function mapCohereFinishReason(
4
4
  finishReason: string | null | undefined,
5
- ): LanguageModelV3FinishReason['unified'] {
5
+ ): LanguageModelV4FinishReason['unified'] {
6
6
  switch (finishReason) {
7
7
  case 'COMPLETE':
8
8
  case 'STOP_SEQUENCE':
@@ -1,21 +1,20 @@
1
- import { RerankingModelV3, SharedV3Warning } from '@ai-sdk/provider';
1
+ import type { RerankingModelV4, SharedV4Warning } from '@ai-sdk/provider';
2
2
  import {
3
3
  combineHeaders,
4
4
  createJsonResponseHandler,
5
- FetchFunction,
6
5
  parseProviderOptions,
7
6
  postJsonToApi,
7
+ type FetchFunction,
8
8
  } from '@ai-sdk/provider-utils';
9
9
  import { cohereFailedResponseHandler } from '../cohere-error';
10
10
  import {
11
- CohereRerankingInput,
12
11
  cohereRerankingResponseSchema,
12
+ type CohereRerankingInput,
13
13
  } from './cohere-reranking-api';
14
14
  import {
15
- CohereRerankingModelId,
16
15
  cohereRerankingModelOptionsSchema,
16
+ type CohereRerankingModelId,
17
17
  } from './cohere-reranking-options';
18
-
19
18
  type CohereRerankingConfig = {
20
19
  provider: string;
21
20
  baseURL: string;
@@ -23,8 +22,8 @@ type CohereRerankingConfig = {
23
22
  fetch?: FetchFunction;
24
23
  };
25
24
 
26
- export class CohereRerankingModel implements RerankingModelV3 {
27
- readonly specificationVersion = 'v3';
25
+ export class CohereRerankingModel implements RerankingModelV4 {
26
+ readonly specificationVersion = 'v4';
28
27
  readonly modelId: CohereRerankingModelId;
29
28
 
30
29
  private readonly config: CohereRerankingConfig;
@@ -46,8 +45,8 @@ export class CohereRerankingModel implements RerankingModelV3 {
46
45
  topN,
47
46
  abortSignal,
48
47
  providerOptions,
49
- }: Parameters<RerankingModelV3['doRerank']>[0]): Promise<
50
- Awaited<ReturnType<RerankingModelV3['doRerank']>>
48
+ }: Parameters<RerankingModelV4['doRerank']>[0]): Promise<
49
+ Awaited<ReturnType<RerankingModelV4['doRerank']>>
51
50
  > {
52
51
  const rerankingOptions = await parseProviderOptions({
53
52
  provider: 'cohere',
@@ -55,7 +54,7 @@ export class CohereRerankingModel implements RerankingModelV3 {
55
54
  schema: cohereRerankingModelOptionsSchema,
56
55
  });
57
56
 
58
- const warnings: SharedV3Warning[] = [];
57
+ const warnings: SharedV4Warning[] = [];
59
58
 
60
59
  if (documents.type === 'object') {
61
60
  warnings.push({
@@ -1,4 +1,8 @@
1
- import { FlexibleSchema, lazySchema, zodSchema } from '@ai-sdk/provider-utils';
1
+ import {
2
+ lazySchema,
3
+ zodSchema,
4
+ type FlexibleSchema,
5
+ } from '@ai-sdk/provider-utils';
2
6
  import { z } from 'zod/v4';
3
7
 
4
8
  // https://docs.cohere.com/docs/rerank
package/dist/index.d.mts DELETED
@@ -1,117 +0,0 @@
1
- import { z } from 'zod/v4';
2
- import { ProviderV3, LanguageModelV3, EmbeddingModelV3, RerankingModelV3 } from '@ai-sdk/provider';
3
- import { FetchFunction } from '@ai-sdk/provider-utils';
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 & {});
6
- declare const cohereLanguageModelOptions: z.ZodObject<{
7
- thinking: z.ZodOptional<z.ZodObject<{
8
- type: z.ZodOptional<z.ZodEnum<{
9
- enabled: "enabled";
10
- disabled: "disabled";
11
- }>>;
12
- tokenBudget: z.ZodOptional<z.ZodNumber>;
13
- }, z.core.$strip>>;
14
- }, z.core.$strip>;
15
- type CohereLanguageModelOptions = z.infer<typeof cohereLanguageModelOptions>;
16
-
17
- type CohereRerankingModelId = 'rerank-v3.5' | 'rerank-english-v3.0' | 'rerank-multilingual-v3.0' | (string & {});
18
- type CohereRerankingModelOptions = {
19
- /**
20
- * Long documents will be automatically truncated to the specified number of tokens.
21
- *
22
- * @default 4096
23
- */
24
- maxTokensPerDoc?: number;
25
- /**
26
- * The priority of the request.
27
- *
28
- * @default 0
29
- */
30
- priority?: number;
31
- };
32
-
33
- type CohereEmbeddingModelId = 'embed-english-v3.0' | 'embed-multilingual-v3.0' | 'embed-english-light-v3.0' | 'embed-multilingual-light-v3.0' | 'embed-english-v2.0' | 'embed-english-light-v2.0' | 'embed-multilingual-v2.0' | (string & {});
34
- declare const cohereEmbeddingModelOptions: z.ZodObject<{
35
- inputType: z.ZodOptional<z.ZodEnum<{
36
- search_document: "search_document";
37
- search_query: "search_query";
38
- classification: "classification";
39
- clustering: "clustering";
40
- }>>;
41
- truncate: z.ZodOptional<z.ZodEnum<{
42
- NONE: "NONE";
43
- START: "START";
44
- END: "END";
45
- }>>;
46
- outputDimension: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<256>, z.ZodLiteral<512>, z.ZodLiteral<1024>, z.ZodLiteral<1536>]>>;
47
- }, z.core.$strip>;
48
- type CohereEmbeddingModelOptions = z.infer<typeof cohereEmbeddingModelOptions>;
49
-
50
- interface CohereProvider extends ProviderV3 {
51
- (modelId: CohereChatModelId): LanguageModelV3;
52
- /**
53
- * Creates a model for text generation.
54
- */
55
- languageModel(modelId: CohereChatModelId): LanguageModelV3;
56
- /**
57
- * Creates a model for text embeddings.
58
- */
59
- embedding(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
60
- /**
61
- * Creates a model for text embeddings.
62
- */
63
- embeddingModel(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
64
- /**
65
- * @deprecated Use `embedding` instead.
66
- */
67
- textEmbedding(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
68
- /**
69
- * @deprecated Use `embeddingModel` instead.
70
- */
71
- textEmbeddingModel(modelId: CohereEmbeddingModelId): EmbeddingModelV3;
72
- /**
73
- * Creates a model for reranking.
74
- */
75
- reranking(modelId: CohereRerankingModelId): RerankingModelV3;
76
- /**
77
- * Creates a model for reranking.
78
- */
79
- rerankingModel(modelId: CohereRerankingModelId): RerankingModelV3;
80
- }
81
- interface CohereProviderSettings {
82
- /**
83
- * Use a different URL prefix for API calls, e.g. to use proxy servers.
84
- * The default prefix is `https://api.cohere.com/v2`.
85
- */
86
- baseURL?: string;
87
- /**
88
- * API key that is being send using the `Authorization` header.
89
- * It defaults to the `COHERE_API_KEY` environment variable.
90
- */
91
- apiKey?: string;
92
- /**
93
- * Custom headers to include in the requests.
94
- */
95
- headers?: Record<string, string>;
96
- /**
97
- * Custom fetch implementation. You can use it as a middleware to intercept requests,
98
- * or to provide a custom fetch implementation for e.g. testing.
99
- */
100
- fetch?: FetchFunction;
101
- /**
102
- * Optional function to generate a unique ID for each request.
103
- */
104
- generateId?: () => string;
105
- }
106
- /**
107
- * Create a Cohere AI provider instance.
108
- */
109
- declare function createCohere(options?: CohereProviderSettings): CohereProvider;
110
- /**
111
- * Default Cohere provider instance.
112
- */
113
- declare const cohere: CohereProvider;
114
-
115
- declare const VERSION: string;
116
-
117
- export { type CohereLanguageModelOptions as CohereChatModelOptions, type CohereEmbeddingModelOptions, type CohereLanguageModelOptions, type CohereProvider, type CohereProviderSettings, type CohereRerankingModelOptions, type CohereRerankingModelOptions as CohereRerankingOptions, VERSION, cohere, createCohere };