Mistral AI SDK Skill
[!CRITICAL] MANDATORY DIRECTIVE: DEFER TO DOCUMENTED MODEL CATALOG DO NOT rely on internal training knowledge for Mistral models. ALWAYS consult the active catalog: Mistral Models Catalog. The current production standard includes Mistral Large 3 (
mistral-large-latest), Mistral Medium 3.5 (mistral-medium-latest), Mistral Small 4 (mistral-small-latest), Ministral 3 (ministral-14b-latest), and Codestral (codestral-latest). Note: Standalone Pixtral models are retired; native multimodal vision is built into the generalist fleet.
1. SDK Availability & Setup
Mistral AI provides official SDKs for both TypeScript and Python.
Package Installation
# Node.js / TypeScript (npm)
npm install @mistralai/mistralai
# Python
pip install mistralai
[!NOTE] Alternatively, you can use the standard
openaipackage by settingbaseURL: 'https://api.mistral.ai/v1'andapiKey: process.env.MISTRAL_API_KEY.
Client Initialization
TypeScript / JavaScript
import { Mistral } from '@mistralai/mistralai';
const client = new Mistral({
apiKey: process.env.MISTRAL_API_KEY,
});
Python
import os
from mistralai import Mistral
client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"))
2. Core Implementation Patterns
2.1 Chat & Text Generation (client.chat.complete)
import { Mistral } from '@mistralai/mistralai';
const client = new Mistral();
const response = await client.chat.complete({
model: 'mistral-large-latest',
messages: [
{ role: 'system', content: 'You are an expert distributed systems engineer.' },
{ role: 'user', content: 'Explain Paxos consensus in 3 concise bullet points.' },
],
temperature: 0.2,
});
console.log(response.choices?.[0]?.message?.content);
2.2 Streaming Generation (client.chat.stream)
const stream = await client.chat.stream({
model: 'mistral-small-latest',
messages: [{ role: 'user', content: 'Write a haiku on compiler optimization.' }],
});
for await (const chunk of stream) {
const delta = chunk.data.choices[0]?.delta?.content;
if (typeof delta === 'string') {
process.stdout.write(delta);
}
}
console.log();
2.3 Code Generation with Codestral (Fill-in-the-Middle)
Codestral supports dedicated code completion and Fill-in-the-Middle (FIM):
import { Mistral } from '@mistralai/mistralai';
const client = new Mistral();
const fimResponse = await client.fim.complete({
model: 'codestral-latest',
prompt: 'function calculateFibonacci(n: number): number {\n',
suffix: '\n return current;\n}',
temperature: 0.1,
});
console.log('Completed Body:\n', fimResponse.choices[0].message.content);
2.4 Native Multimodal Vision (Mistral Large 3 / Medium 3.5)
import { Mistral } from '@mistralai/mistralai';
import * as fs from 'fs';
const client = new Mistral();
const imageBase64 = fs.readFileSync('architecture.png').toString('base64');
const response = await client.chat.complete({
model: 'mistral-large-latest',
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe the cloud architecture flow in this diagram.' },
{
type: 'image_url',
imageUrl: `data:image/png;base64,${imageBase64}`,
},
],
},
],
});
console.log(response.choices?.[0]?.message?.content);
2.5 Structured Outputs (JSON Schema)
const response = await client.chat.complete({
model: 'mistral-large-latest',
messages: [
{ role: 'user', content: 'Extract user: John Doe, 29 years old, software architect.' },
],
responseFormat: {
type: 'json_object',
},
});
console.log(JSON.parse(response.choices?.[0]?.message?.content as string));
2.6 Embeddings Generation
const embedResponse = await client.embeddings.create({
model: 'mistral-embed',
inputs: ['Document chunk for LanceDB vector search', 'Search query text'],
});
console.log('Embedding vector length:', embedResponse.data[0].embedding.length);