@ai-sdk/mistral 4.0.67 → 4.0.69

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,6 +1,6 @@
1
1
  ---
2
2
  title: Mistral AI
3
- description: Learn how to use Mistral.
3
+ description: Learn how to use Mistral AI with AI Gateway and the direct provider.
4
4
  ---
5
5
 
6
6
  # Mistral AI Provider
@@ -8,12 +8,56 @@ description: Learn how to use Mistral.
8
8
  The [Mistral AI](https://mistral.ai/) provider contains language model,
9
9
  embedding model, speech model, and transcription model support for Mistral
10
10
  APIs.
11
+ You can use Mistral AI models through [AI Gateway](/providers/ai-sdk-providers/ai-gateway) (recommended) or connect directly with `@ai-sdk/mistral`.
11
12
 
12
- ## Setup
13
+ ```ts provider="mistral" model="mistral/mistral-medium-3.5"
14
+ import { generateText } from 'ai';
15
+ __PROVIDER_IMPORT__;
16
+
17
+ const { text } = await generateText({
18
+ model: __MODEL__,
19
+ prompt: 'Write a vegetarian lasagna recipe for 4 people.',
20
+ });
21
+
22
+ console.log(text);
23
+ ```
24
+
25
+ ## Using AI Gateway
26
+
27
+ The AI SDK uses AI Gateway by default for model strings. Gateway provides access to models across providers through a single API key, with usage and cost tracking. Install `ai`:
13
28
 
14
- The Mistral provider is available in the `@ai-sdk/mistral` module. You can install it with
29
+ <InstallPackages packages="ai" />
30
+
31
+ Set an [AI Gateway API key](https://vercel.com/docs/ai-gateway/getting-started) in your environment:
32
+
33
+ ```bash
34
+ export AI_GATEWAY_API_KEY=your_api_key_here
35
+ ```
15
36
 
16
- <InstallPackages packages="@ai-sdk/mistral" />
37
+ Vercel deployments can also use [OIDC authentication](/providers/ai-sdk-providers/ai-gateway#oidc-authentication-vercel-deployments).
38
+ See the [AI Gateway provider documentation](/providers/ai-sdk-providers/ai-gateway) for configuration and the [model catalog](https://vercel.com/ai-gateway/models) for available models.
39
+
40
+ Gateway feature and provider-option support depends on the model and the provider serving the request. See [Gateway provider options](https://vercel.com/docs/ai-gateway/models-and-providers/provider-options).
41
+
42
+ ## Using `@ai-sdk/mistral`
43
+
44
+ For direct access to Mistral AI, install `ai` and `@ai-sdk/mistral`:
45
+
46
+ <InstallPackages packages="ai @ai-sdk/mistral" />
47
+
48
+ Set your Mistral AI API credential in the `MISTRAL_API_KEY` environment variable:
49
+
50
+ ```bash
51
+ export MISTRAL_API_KEY=your_api_key_here
52
+ ```
53
+
54
+ AI Gateway does not expose every API in `@ai-sdk/mistral`. Use the direct provider for provider-specific APIs or custom connection settings.
55
+
56
+ <Note>
57
+ The following sections document the direct `@ai-sdk/mistral` provider,
58
+ including its model factories and settings. Gateway model and feature
59
+ availability can differ.
60
+ </Note>
17
61
 
18
62
  ## Provider Instance
19
63
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/mistral",
3
- "version": "4.0.67",
3
+ "version": "4.0.69",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -29,13 +29,14 @@
29
29
  }
30
30
  },
31
31
  "dependencies": {
32
- "@ai-sdk/provider": "4.0.30",
33
- "@ai-sdk/provider-utils": "5.0.63"
32
+ "@ai-sdk/provider": "4.0.31",
33
+ "@ai-sdk/provider-utils": "5.0.65"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@ai-sdk/test-server": "2.0.4",
37
37
  "@types/node": "22.19.19",
38
38
  "@vercel/ai-tsconfig": "0.0.0",
39
+ "@vercel/ai-tsdown-config": "0.0.0",
39
40
  "tsdown": "^0.23.0",
40
41
  "typescript": "6.0.3",
41
42
  "zod": "3.25.76"
@@ -1,3 +1,4 @@
1
+ import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
1
2
  import { z } from 'zod/v4';
2
3
 
3
4
  // https://docs.mistral.ai/models
@@ -37,54 +38,58 @@ export type MistralChatModelId =
37
38
  | 'zai-glm-5-2'
38
39
  | (string & {});
39
40
 
40
- export const mistralLanguageModelChatOptions = z.object({
41
- /**
42
- * Whether to inject a safety prompt before all conversations.
43
- *
44
- * Defaults to `false`.
45
- */
46
- safePrompt: z.boolean().optional(),
41
+ export const mistralLanguageModelChatOptions = () =>
42
+ z.object({
43
+ /**
44
+ * Whether to inject a safety prompt before all conversations.
45
+ *
46
+ * Defaults to `false`.
47
+ */
48
+ safePrompt: z.boolean().optional(),
47
49
 
48
- documentImageLimit: z.number().optional(),
49
- documentPageLimit: z.number().optional(),
50
+ documentImageLimit: z.number().optional(),
51
+ documentPageLimit: z.number().optional(),
50
52
 
51
- /**
52
- * Whether to use structured outputs.
53
- *
54
- * @default true
55
- */
56
- structuredOutputs: z.boolean().optional(),
53
+ /**
54
+ * Whether to use structured outputs.
55
+ *
56
+ * @default true
57
+ */
58
+ structuredOutputs: z.boolean().optional(),
57
59
 
58
- /**
59
- * Whether to use strict JSON schema validation.
60
- *
61
- * @default false
62
- */
63
- strictJsonSchema: z.boolean().optional(),
60
+ /**
61
+ * Whether to use strict JSON schema validation.
62
+ *
63
+ * @default false
64
+ */
65
+ strictJsonSchema: z.boolean().optional(),
64
66
 
65
- /**
66
- * Whether to enable parallel function calling during tool use.
67
- * When set to false, the model will use at most one tool per response.
68
- *
69
- * @default true
70
- */
71
- parallelToolCalls: z.boolean().optional(),
67
+ /**
68
+ * Whether to enable parallel function calling during tool use.
69
+ * When set to false, the model will use at most one tool per response.
70
+ *
71
+ * @default true
72
+ */
73
+ parallelToolCalls: z.boolean().optional(),
72
74
 
73
- /**
74
- * A stable identifier used to route requests with shared prompt prefixes to
75
- * the same cache.
76
- */
77
- promptCacheKey: z.string().optional(),
75
+ /**
76
+ * A stable identifier used to route requests with shared prompt prefixes to
77
+ * the same cache.
78
+ */
79
+ promptCacheKey: z.string().optional(),
78
80
 
79
- /**
80
- * Controls the reasoning effort for models that support adjustable reasoning.
81
- *
82
- * - `'high'`: Enable reasoning
83
- * - `'none'`: Disable reasoning
84
- */
85
- reasoningEffort: z.enum(['high', 'none']).optional(),
86
- });
81
+ /**
82
+ * Controls the reasoning effort for models that support adjustable reasoning.
83
+ *
84
+ * - `'high'`: Enable reasoning
85
+ * - `'none'`: Disable reasoning
86
+ */
87
+ reasoningEffort: z.enum(['high', 'none']).optional(),
88
+ });
89
+ export const mistralLanguageModelChatOptionsValidator = lazySchema(() =>
90
+ zodSchema(mistralLanguageModelChatOptions()),
91
+ );
87
92
 
88
93
  export type MistralLanguageModelChatOptions = z.infer<
89
- typeof mistralLanguageModelChatOptions
94
+ ReturnType<typeof mistralLanguageModelChatOptions>
90
95
  >;
@@ -9,6 +9,9 @@ import type {
9
9
  SharedV4Warning,
10
10
  } from '@ai-sdk/provider';
11
11
  import {
12
+ lazySchema,
13
+ zodSchema,
14
+ type InferSchema,
12
15
  combineHeaders,
13
16
  createEventSourceResponseHandler,
14
17
  createJsonResponseHandler,
@@ -36,7 +39,7 @@ import { convertToMistralChatMessages } from './convert-to-mistral-chat-messages
36
39
  import { getResponseMetadata } from './get-response-metadata';
37
40
  import { mapMistralFinishReason } from './map-mistral-finish-reason';
38
41
  import {
39
- mistralLanguageModelChatOptions,
42
+ mistralLanguageModelChatOptionsValidator,
40
43
  type MistralChatModelId,
41
44
  } from './mistral-chat-language-model-options';
42
45
  import { mistralFailedResponseHandler } from './mistral-error';
@@ -127,7 +130,7 @@ export class MistralChatLanguageModel implements LanguageModelV4 {
127
130
  (await parseProviderOptions({
128
131
  provider: 'mistral',
129
132
  providerOptions,
130
- schema: mistralLanguageModelChatOptions,
133
+ schema: mistralLanguageModelChatOptionsValidator,
131
134
  })) ?? {};
132
135
 
133
136
  if (topK != null) {
@@ -365,7 +368,7 @@ export class MistralChatLanguageModel implements LanguageModelV4 {
365
368
  return {
366
369
  stream: response.pipeThrough(
367
370
  new TransformStream<
368
- ParseResult<z.infer<typeof mistralChatChunkSchema>>,
371
+ ParseResult<InferSchema<typeof mistralChatChunkSchema>>,
369
372
  LanguageModelV4StreamPart
370
373
  >({
371
374
  start(controller) {
@@ -505,7 +508,9 @@ function extractReasoningContent(
505
508
  .join('');
506
509
  }
507
510
 
508
- function extractTextContent(content: z.infer<typeof mistralContentSchema>) {
511
+ function extractTextContent(
512
+ content: z.infer<ReturnType<typeof mistralContentSchema>>,
513
+ ) {
509
514
  if (typeof content === 'string') {
510
515
  return content;
511
516
  }
@@ -538,98 +543,110 @@ function extractTextContent(content: z.infer<typeof mistralContentSchema>) {
538
543
  return textContent.length ? textContent.join('') : undefined;
539
544
  }
540
545
 
541
- const mistralContentSchema = z
542
- .union([
543
- z.string(),
544
- z.array(
545
- z.discriminatedUnion('type', [
546
- z.object({
547
- type: z.literal('text'),
548
- text: z.string(),
549
- }),
550
- z.object({
551
- type: z.literal('image_url'),
552
- image_url: z.union([
553
- z.string(),
554
- z.object({
555
- url: z.string(),
556
- detail: z.string().nullable(),
557
- }),
558
- ]),
559
- }),
560
- z.object({
561
- type: z.literal('reference'),
562
- reference_ids: z.array(z.union([z.string(), z.number()])),
563
- }),
564
- z.object({
565
- type: z.literal('thinking'),
566
- thinking: z.array(
567
- z.object({
568
- type: z.literal('text'),
569
- text: z.string(),
570
- }),
571
- ),
572
- }),
573
- ]),
574
- ),
575
- ])
576
- .nullish();
546
+ const mistralContentSchema = () =>
547
+ z
548
+ .union([
549
+ z.string(),
550
+ z.array(
551
+ z.discriminatedUnion('type', [
552
+ z.object({
553
+ type: z.literal('text'),
554
+ text: z.string(),
555
+ }),
556
+ z.object({
557
+ type: z.literal('image_url'),
558
+ image_url: z.union([
559
+ z.string(),
560
+ z.object({
561
+ url: z.string(),
562
+ detail: z.string().nullable(),
563
+ }),
564
+ ]),
565
+ }),
566
+ z.object({
567
+ type: z.literal('reference'),
568
+ reference_ids: z.array(z.union([z.string(), z.number()])),
569
+ }),
570
+ z.object({
571
+ type: z.literal('thinking'),
572
+ thinking: z.array(
573
+ z.object({
574
+ type: z.literal('text'),
575
+ text: z.string(),
576
+ }),
577
+ ),
578
+ }),
579
+ ]),
580
+ ),
581
+ ])
582
+ .nullish();
577
583
 
578
584
  // limited version of the schema, focussed on what is needed for the implementation
579
585
  // this approach limits breakages when the API changes and increases efficiency
580
- const mistralChatResponseSchema = z.object({
581
- id: z.string().nullish(),
582
- created: z.number().nullish(),
583
- model: z.string().nullish(),
584
- choices: z.array(
586
+ const mistralChatResponseSchema = lazySchema(() =>
587
+ zodSchema(
585
588
  z.object({
586
- message: z.object({
587
- role: z.literal('assistant'),
588
- content: mistralContentSchema,
589
- tool_calls: z
590
- .array(
591
- z.object({
592
- id: z.string(),
593
- function: z.object({ name: z.string(), arguments: z.string() }),
594
- }),
595
- )
596
- .nullish(),
597
- }),
598
- index: z.number(),
599
- finish_reason: z.string().nullish(),
589
+ id: z.string().nullish(),
590
+ created: z.number().nullish(),
591
+ model: z.string().nullish(),
592
+ choices: z.array(
593
+ z.object({
594
+ message: z.object({
595
+ role: z.literal('assistant'),
596
+ content: mistralContentSchema(),
597
+ tool_calls: z
598
+ .array(
599
+ z.object({
600
+ id: z.string(),
601
+ function: z.object({
602
+ name: z.string(),
603
+ arguments: z.string(),
604
+ }),
605
+ }),
606
+ )
607
+ .nullish(),
608
+ }),
609
+ index: z.number(),
610
+ finish_reason: z.string().nullish(),
611
+ }),
612
+ ),
613
+ object: z.literal('chat.completion'),
614
+ usage: mistralUsageSchema,
600
615
  }),
601
616
  ),
602
- object: z.literal('chat.completion'),
603
- usage: mistralUsageSchema,
604
- });
617
+ );
605
618
 
606
619
  // limited version of the schema, focussed on what is needed for the implementation
607
620
  // this approach limits breakages when the API changes and increases efficiency
608
- const mistralChatChunkSchema = z.object({
609
- id: z.string().nullish(),
610
- created: z.number().nullish(),
611
- model: z.string().nullish(),
612
- choices: z.array(
621
+ const mistralChatChunkSchema = lazySchema(() =>
622
+ zodSchema(
613
623
  z.object({
614
- delta: z.object({
615
- role: z.enum(['assistant']).optional(),
616
- content: mistralContentSchema,
617
- tool_calls: z
618
- .array(
619
- z.object({
620
- index: z.number().nullish(),
621
- id: z.string().nullish(),
622
- function: z.object({
623
- name: z.string().nullish(),
624
- arguments: z.string().nullish(),
625
- }),
626
- }),
627
- )
628
- .nullish(),
629
- }),
630
- finish_reason: z.string().nullish(),
631
- index: z.number(),
624
+ id: z.string().nullish(),
625
+ created: z.number().nullish(),
626
+ model: z.string().nullish(),
627
+ choices: z.array(
628
+ z.object({
629
+ delta: z.object({
630
+ role: z.enum(['assistant']).optional(),
631
+ content: mistralContentSchema(),
632
+ tool_calls: z
633
+ .array(
634
+ z.object({
635
+ index: z.number().nullish(),
636
+ id: z.string().nullish(),
637
+ function: z.object({
638
+ name: z.string().nullish(),
639
+ arguments: z.string().nullish(),
640
+ }),
641
+ }),
642
+ )
643
+ .nullish(),
644
+ }),
645
+ finish_reason: z.string().nullish(),
646
+ index: z.number(),
647
+ }),
648
+ ),
649
+ usage: mistralUsageSchema.nullish(),
632
650
  }),
633
651
  ),
634
- usage: mistralUsageSchema.nullish(),
635
- });
652
+ );
@@ -1,3 +1,4 @@
1
+ import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
1
2
  import {
2
3
  mistralLanguageModelChatOptions,
3
4
  type MistralChatModelId,
@@ -6,8 +7,9 @@ import {
6
7
 
7
8
  export type MistralConversationModelId = MistralChatModelId;
8
9
 
9
- export const mistralLanguageModelConversationOptions =
10
- mistralLanguageModelChatOptions;
10
+ export const mistralLanguageModelConversationOptions = lazySchema(() =>
11
+ zodSchema(mistralLanguageModelChatOptions()),
12
+ );
11
13
 
12
14
  export type MistralLanguageModelConversationOptions =
13
15
  MistralLanguageModelChatOptions;