@ai-sdk/cohere 3.0.72 → 3.0.73

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.
@@ -8,6 +8,9 @@ import type {
8
8
  LanguageModelV3StreamResult,
9
9
  } from '@ai-sdk/provider';
10
10
  import {
11
+ lazySchema,
12
+ zodSchema,
13
+ type InferSchema,
11
14
  combineHeaders,
12
15
  createEventSourceResponseHandler,
13
16
  createJsonResponseHandler,
@@ -254,7 +257,7 @@ export class CohereChatLanguageModel implements LanguageModelV3 {
254
257
  return {
255
258
  stream: response.pipeThrough(
256
259
  new TransformStream<
257
- ParseResult<z.infer<typeof cohereChatChunkSchema>>,
260
+ ParseResult<InferSchema<typeof cohereChatChunkSchema>>,
258
261
  LanguageModelV3StreamPart
259
262
  >({
260
263
  start(controller) {
@@ -436,175 +439,184 @@ export class CohereChatLanguageModel implements LanguageModelV3 {
436
439
  }
437
440
 
438
441
  // Loose, nested objects included: the parsed value is returned as `usage.raw`.
439
- const cohereUsageSchema = z.looseObject({
440
- billed_units: z
441
- .looseObject({
442
+ const cohereUsageSchema = () =>
443
+ z.looseObject({
444
+ billed_units: z
445
+ .looseObject({
446
+ input_tokens: z.number(),
447
+ output_tokens: z.number(),
448
+ })
449
+ .nullish(),
450
+ tokens: z.looseObject({
442
451
  input_tokens: z.number(),
443
452
  output_tokens: z.number(),
444
- })
445
- .nullish(),
446
- tokens: z.looseObject({
447
- input_tokens: z.number(),
448
- output_tokens: z.number(),
449
- }),
450
- cached_tokens: z.number().nullish(),
451
- });
452
-
453
- const cohereChatResponseSchema = z.object({
454
- generation_id: z.string().nullish(),
455
- message: z.object({
456
- role: z.string(),
457
- content: z
458
- .array(
459
- z.union([
460
- z.object({
461
- type: z.literal('text'),
462
- text: z.string(),
463
- }),
464
- z.object({
465
- type: z.literal('thinking'),
466
- thinking: z.string(),
467
- }),
468
- ]),
469
- )
470
- .nullish(),
471
- tool_plan: z.string().nullish(),
472
- tool_calls: z
473
- .array(
474
- z.object({
475
- id: z.string(),
476
- type: z.literal('function'),
477
- function: z.object({
478
- name: z.string(),
479
- arguments: z.string(),
480
- }),
481
- }),
482
- )
483
- .nullish(),
484
- citations: z
485
- .array(
486
- z.object({
487
- start: z.number(),
488
- end: z.number(),
489
- text: z.string(),
490
- sources: z.array(
491
- z.object({
492
- type: z.string().optional(),
493
- id: z.string().optional(),
494
- document: z.object({
495
- id: z.string().optional(),
453
+ }),
454
+ cached_tokens: z.number().nullish(),
455
+ });
456
+
457
+ const cohereChatResponseSchema = lazySchema(() =>
458
+ zodSchema(
459
+ z.object({
460
+ generation_id: z.string().nullish(),
461
+ message: z.object({
462
+ role: z.string(),
463
+ content: z
464
+ .array(
465
+ z.union([
466
+ z.object({
467
+ type: z.literal('text'),
496
468
  text: z.string(),
497
- title: z.string(),
469
+ }),
470
+ z.object({
471
+ type: z.literal('thinking'),
472
+ thinking: z.string(),
473
+ }),
474
+ ]),
475
+ )
476
+ .nullish(),
477
+ tool_plan: z.string().nullish(),
478
+ tool_calls: z
479
+ .array(
480
+ z.object({
481
+ id: z.string(),
482
+ type: z.literal('function'),
483
+ function: z.object({
484
+ name: z.string(),
485
+ arguments: z.string(),
498
486
  }),
499
487
  }),
500
- ),
501
- type: z.string().optional(),
502
- }),
503
- )
504
- .nullish(),
505
- }),
506
- finish_reason: z.string(),
507
- usage: cohereUsageSchema,
508
- });
488
+ )
489
+ .nullish(),
490
+ citations: z
491
+ .array(
492
+ z.object({
493
+ start: z.number(),
494
+ end: z.number(),
495
+ text: z.string(),
496
+ sources: z.array(
497
+ z.object({
498
+ type: z.string().optional(),
499
+ id: z.string().optional(),
500
+ document: z.object({
501
+ id: z.string().optional(),
502
+ text: z.string(),
503
+ title: z.string(),
504
+ }),
505
+ }),
506
+ ),
507
+ type: z.string().optional(),
508
+ }),
509
+ )
510
+ .nullish(),
511
+ }),
512
+ finish_reason: z.string(),
513
+ usage: cohereUsageSchema(),
514
+ }),
515
+ ),
516
+ );
509
517
 
510
518
  // limited version of the schema, focussed on what is needed for the implementation
511
519
  // this approach limits breakages when the API changes and increases efficiency
512
- const cohereChatChunkSchema = z.discriminatedUnion('type', [
513
- z.object({
514
- type: z.literal('citation-start'),
515
- }),
516
- z.object({
517
- type: z.literal('citation-end'),
518
- }),
519
- z.object({
520
- type: z.literal('content-start'),
521
- index: z.number(),
522
- delta: z.object({
523
- message: z.object({
524
- content: z.union([
525
- z.object({
526
- type: z.literal('text'),
527
- text: z.string(),
528
- }),
529
- z.object({
530
- type: z.literal('thinking'),
531
- thinking: z.string(),
532
- }),
533
- ]),
520
+ const cohereChatChunkSchema = lazySchema(() =>
521
+ zodSchema(
522
+ z.discriminatedUnion('type', [
523
+ z.object({
524
+ type: z.literal('citation-start'),
534
525
  }),
535
- }),
536
- }),
537
- z.object({
538
- type: z.literal('content-delta'),
539
- index: z.number(),
540
- delta: z.object({
541
- message: z.object({
542
- content: z.union([
543
- z.object({
544
- text: z.string(),
526
+ z.object({
527
+ type: z.literal('citation-end'),
528
+ }),
529
+ z.object({
530
+ type: z.literal('content-start'),
531
+ index: z.number(),
532
+ delta: z.object({
533
+ message: z.object({
534
+ content: z.union([
535
+ z.object({
536
+ type: z.literal('text'),
537
+ text: z.string(),
538
+ }),
539
+ z.object({
540
+ type: z.literal('thinking'),
541
+ thinking: z.string(),
542
+ }),
543
+ ]),
545
544
  }),
546
- z.object({
547
- thinking: z.string(),
545
+ }),
546
+ }),
547
+ z.object({
548
+ type: z.literal('content-delta'),
549
+ index: z.number(),
550
+ delta: z.object({
551
+ message: z.object({
552
+ content: z.union([
553
+ z.object({
554
+ text: z.string(),
555
+ }),
556
+ z.object({
557
+ thinking: z.string(),
558
+ }),
559
+ ]),
548
560
  }),
549
- ]),
561
+ }),
550
562
  }),
551
- }),
552
- }),
553
- z.object({
554
- type: z.literal('content-end'),
555
- index: z.number(),
556
- }),
557
- z.object({
558
- type: z.literal('message-start'),
559
- id: z.string().nullish(),
560
- }),
561
- z.object({
562
- type: z.literal('message-end'),
563
- delta: z.object({
564
- finish_reason: z.string(),
565
- usage: cohereUsageSchema,
566
- }),
567
- }),
568
- // https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
569
- z.object({
570
- type: z.literal('tool-plan-delta'),
571
- delta: z.object({
572
- message: z.object({
573
- tool_plan: z.string(),
563
+ z.object({
564
+ type: z.literal('content-end'),
565
+ index: z.number(),
574
566
  }),
575
- }),
576
- }),
577
- z.object({
578
- type: z.literal('tool-call-start'),
579
- delta: z.object({
580
- message: z.object({
581
- tool_calls: z.object({
582
- id: z.string(),
583
- type: z.literal('function'),
584
- function: z.object({
585
- name: z.string(),
586
- arguments: z.string(),
567
+ z.object({
568
+ type: z.literal('message-start'),
569
+ id: z.string().nullish(),
570
+ }),
571
+ z.object({
572
+ type: z.literal('message-end'),
573
+ delta: z.object({
574
+ finish_reason: z.string(),
575
+ usage: cohereUsageSchema(),
576
+ }),
577
+ }),
578
+ // https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
579
+ z.object({
580
+ type: z.literal('tool-plan-delta'),
581
+ delta: z.object({
582
+ message: z.object({
583
+ tool_plan: z.string(),
587
584
  }),
588
585
  }),
589
586
  }),
590
- }),
591
- }),
592
- // A single tool call's `arguments` stream in chunks and must be accumulated
593
- // in a string and so the full tool object info can only be parsed once we see
594
- // `tool-call-end`.
595
- z.object({
596
- type: z.literal('tool-call-delta'),
597
- delta: z.object({
598
- message: z.object({
599
- tool_calls: z.object({
600
- function: z.object({
601
- arguments: z.string(),
587
+ z.object({
588
+ type: z.literal('tool-call-start'),
589
+ delta: z.object({
590
+ message: z.object({
591
+ tool_calls: z.object({
592
+ id: z.string(),
593
+ type: z.literal('function'),
594
+ function: z.object({
595
+ name: z.string(),
596
+ arguments: z.string(),
597
+ }),
598
+ }),
602
599
  }),
603
600
  }),
604
601
  }),
605
- }),
606
- }),
607
- z.object({
608
- type: z.literal('tool-call-end'),
609
- }),
610
- ]);
602
+ // A single tool call's `arguments` stream in chunks and must be accumulated
603
+ // in a string and so the full tool object info can only be parsed once we see
604
+ // `tool-call-end`.
605
+ z.object({
606
+ type: z.literal('tool-call-delta'),
607
+ delta: z.object({
608
+ message: z.object({
609
+ tool_calls: z.object({
610
+ function: z.object({
611
+ arguments: z.string(),
612
+ }),
613
+ }),
614
+ }),
615
+ }),
616
+ }),
617
+ z.object({
618
+ type: z.literal('tool-call-end'),
619
+ }),
620
+ ]),
621
+ ),
622
+ );
@@ -1,3 +1,8 @@
1
+ import {
2
+ lazySchema,
3
+ zodSchema,
4
+ type InferSchema,
5
+ } from '@ai-sdk/provider-utils';
1
6
  import { z } from 'zod/v4';
2
7
 
3
8
  // https://docs.cohere.com/docs/models
@@ -17,36 +22,44 @@ export type CohereChatModelId =
17
22
  | 'command-light-nightly'
18
23
  | (string & {});
19
24
 
20
- export const cohereLanguageModelOptions = z.object({
21
- /**
22
- * Configuration for reasoning features (optional)
23
- *
24
- * Can be set to an object with the two properties `type` and `tokenBudget`. `type` can be set to `'enabled'` or `'disabled'` (defaults to `'enabled'`).
25
- * `tokenBudget` is the maximum number of tokens the model can use for thinking, which must be set to a positive integer. The model will stop thinking if it reaches the thinking token budget and will proceed with the response
26
- *
27
- * @see https://docs.cohere.com/reference/chat#request.body.thinking
28
- */
29
- thinking: z
30
- .object({
31
- type: z.enum(['enabled', 'disabled']).optional(),
32
- tokenBudget: z.number().optional(),
33
- })
34
- .optional(),
35
- });
25
+ export const cohereLanguageModelOptions = lazySchema(() =>
26
+ zodSchema(
27
+ z.object({
28
+ /**
29
+ * Configuration for reasoning features (optional)
30
+ *
31
+ * Can be set to an object with the two properties `type` and `tokenBudget`. `type` can be set to `'enabled'` or `'disabled'` (defaults to `'enabled'`).
32
+ * `tokenBudget` is the maximum number of tokens the model can use for thinking, which must be set to a positive integer. The model will stop thinking if it reaches the thinking token budget and will proceed with the response
33
+ *
34
+ * @see https://docs.cohere.com/reference/chat#request.body.thinking
35
+ */
36
+ thinking: z
37
+ .object({
38
+ type: z.enum(['enabled', 'disabled']).optional(),
39
+ tokenBudget: z.number().optional(),
40
+ })
41
+ .optional(),
42
+ }),
43
+ ),
44
+ );
36
45
 
37
- export type CohereLanguageModelOptions = z.infer<
46
+ export type CohereLanguageModelOptions = InferSchema<
38
47
  typeof cohereLanguageModelOptions
39
48
  >;
40
49
 
41
- export const cohereImagePartProviderOptions = z.object({
42
- /**
43
- * Image fidelity level passed through as `image_url.detail` on the Cohere chat API.
44
- *
45
- * @see https://docs.cohere.com/docs/image-inputs
46
- */
47
- detail: z.enum(['auto', 'low', 'high']).optional(),
48
- });
50
+ export const cohereImagePartProviderOptions = lazySchema(() =>
51
+ zodSchema(
52
+ z.object({
53
+ /**
54
+ * Image fidelity level passed through as `image_url.detail` on the Cohere chat API.
55
+ *
56
+ * @see https://docs.cohere.com/docs/image-inputs
57
+ */
58
+ detail: z.enum(['auto', 'low', 'high']).optional(),
59
+ }),
60
+ ),
61
+ );
49
62
 
50
- export type CohereImagePartProviderOptions = z.infer<
63
+ export type CohereImagePartProviderOptions = InferSchema<
51
64
  typeof cohereImagePartProviderOptions
52
65
  >;
@@ -3,6 +3,8 @@ import {
3
3
  type EmbeddingModelV3,
4
4
  } from '@ai-sdk/provider';
5
5
  import {
6
+ lazySchema,
7
+ zodSchema,
6
8
  combineHeaders,
7
9
  createJsonResponseHandler,
8
10
  parseProviderOptions,
@@ -101,13 +103,17 @@ export class CohereEmbeddingModel implements EmbeddingModelV3 {
101
103
 
102
104
  // minimal version of the schema, focussed on what is needed for the implementation
103
105
  // this approach limits breakages when the API changes and increases efficiency
104
- const cohereTextEmbeddingResponseSchema = z.object({
105
- embeddings: z.object({
106
- float: z.array(z.array(z.number())),
107
- }),
108
- meta: z.object({
109
- billed_units: z.object({
110
- input_tokens: z.number(),
106
+ const cohereTextEmbeddingResponseSchema = lazySchema(() =>
107
+ zodSchema(
108
+ z.object({
109
+ embeddings: z.object({
110
+ float: z.array(z.array(z.number())),
111
+ }),
112
+ meta: z.object({
113
+ billed_units: z.object({
114
+ input_tokens: z.number(),
115
+ }),
116
+ }),
111
117
  }),
112
- }),
113
- });
118
+ ),
119
+ );
@@ -1,3 +1,8 @@
1
+ import {
2
+ lazySchema,
3
+ zodSchema,
4
+ type InferSchema,
5
+ } from '@ai-sdk/provider-utils';
1
6
  import { z } from 'zod/v4';
2
7
 
3
8
  export type CohereEmbeddingModelId =
@@ -10,48 +15,57 @@ export type CohereEmbeddingModelId =
10
15
  | 'embed-multilingual-v2.0'
11
16
  | (string & {});
12
17
 
13
- export const cohereEmbeddingModelOptions = z.object({
14
- /**
15
- * Specifies the type of input passed to the model. Default is `search_query`.
16
- *
17
- * - "search_document": Used for embeddings stored in a vector database for search use-cases.
18
- * - "search_query": Used for embeddings of search queries run against a vector DB to find relevant documents.
19
- * - "classification": Used for embeddings passed through a text classifier.
20
- * - "clustering": Used for embeddings run through a clustering algorithm.
21
- */
22
- inputType: z
23
- .enum(['search_document', 'search_query', 'classification', 'clustering'])
24
- .optional(),
18
+ export const cohereEmbeddingModelOptions = lazySchema(() =>
19
+ zodSchema(
20
+ z.object({
21
+ /**
22
+ * Specifies the type of input passed to the model. Default is `search_query`.
23
+ *
24
+ * - "search_document": Used for embeddings stored in a vector database for search use-cases.
25
+ * - "search_query": Used for embeddings of search queries run against a vector DB to find relevant documents.
26
+ * - "classification": Used for embeddings passed through a text classifier.
27
+ * - "clustering": Used for embeddings run through a clustering algorithm.
28
+ */
29
+ inputType: z
30
+ .enum([
31
+ 'search_document',
32
+ 'search_query',
33
+ 'classification',
34
+ 'clustering',
35
+ ])
36
+ .optional(),
25
37
 
26
- /**
27
- * Specifies how the API will handle inputs longer than the maximum token length.
28
- * Default is `END`.
29
- *
30
- * - "NONE": If selected, when the input exceeds the maximum input token length will return an error.
31
- * - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
32
- * - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
33
- */
34
- truncate: z.enum(['NONE', 'START', 'END']).optional(),
38
+ /**
39
+ * Specifies how the API will handle inputs longer than the maximum token length.
40
+ * Default is `END`.
41
+ *
42
+ * - "NONE": If selected, when the input exceeds the maximum input token length will return an error.
43
+ * - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
44
+ * - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
45
+ */
46
+ truncate: z.enum(['NONE', 'START', 'END']).optional(),
35
47
 
36
- /**
37
- * The number of dimensions of the output embedding.
38
- * Only available for `embed-v4.0` and newer models.
39
- *
40
- * Embed v4.0 supports `256`, `512`, `1024`, and `1536` (default).
41
- * Embed v5.0 Pro and Fast also support `768` and `2048` (default).
42
- */
43
- outputDimension: z
44
- .union([
45
- z.literal(256),
46
- z.literal(512),
47
- z.literal(768),
48
- z.literal(1024),
49
- z.literal(1536),
50
- z.literal(2048),
51
- ])
52
- .optional(),
53
- });
48
+ /**
49
+ * The number of dimensions of the output embedding.
50
+ * Only available for `embed-v4.0` and newer models.
51
+ *
52
+ * Embed v4.0 supports `256`, `512`, `1024`, and `1536` (default).
53
+ * Embed v5.0 Pro and Fast also support `768` and `2048` (default).
54
+ */
55
+ outputDimension: z
56
+ .union([
57
+ z.literal(256),
58
+ z.literal(512),
59
+ z.literal(768),
60
+ z.literal(1024),
61
+ z.literal(1536),
62
+ z.literal(2048),
63
+ ])
64
+ .optional(),
65
+ }),
66
+ ),
67
+ );
54
68
 
55
- export type CohereEmbeddingModelOptions = z.infer<
69
+ export type CohereEmbeddingModelOptions = InferSchema<
56
70
  typeof cohereEmbeddingModelOptions
57
71
  >;
@@ -1,11 +1,20 @@
1
- import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils';
1
+ import {
2
+ lazySchema,
3
+ zodSchema,
4
+ type InferSchema,
5
+ createJsonErrorResponseHandler,
6
+ } from '@ai-sdk/provider-utils';
2
7
  import { z } from 'zod/v4';
3
8
 
4
- const cohereErrorDataSchema = z.object({
5
- message: z.string(),
6
- });
9
+ const cohereErrorDataSchema = lazySchema(() =>
10
+ zodSchema(
11
+ z.object({
12
+ message: z.string(),
13
+ }),
14
+ ),
15
+ );
7
16
 
8
- export type CohereErrorData = z.infer<typeof cohereErrorDataSchema>;
17
+ export type CohereErrorData = InferSchema<typeof cohereErrorDataSchema>;
9
18
 
10
19
  export const cohereFailedResponseHandler = createJsonErrorResponseHandler({
11
20
  errorSchema: cohereErrorDataSchema,