mem0ai/mem0 · error · Error
Mistral API key is required
Error message
Mistral API key is required
What it means
Thrown synchronously by the MistralLLM constructor when config.apiKey is falsy. Unlike several sibling providers, MistralLLM reads NO environment-variable fallback — only config.apiKey — so relying on MISTRAL_API_KEY alone will always throw here. The @mistralai/mistralai client is created lazily, making this the up-front credential check.
Source
Thrown at mem0-ts/src/oss/src/llms/mistral.ts:13
import type { Mistral } from "@mistralai/mistralai";
import { LLM, LLMResponse } from "./base";
import { LLMConfig, Message } from "../types";
import { loadPeer } from "../utils/load_peer";
export class MistralLLM implements LLM {
private client!: Mistral;
private model: string;
private readonly apiKey: string;
constructor(config: LLMConfig) {
if (!config.apiKey) {
throw new Error("Mistral API key is required");
}
this.apiKey = config.apiKey;
this.model = config.model || "mistral-tiny-latest";
}
private async ensureClient(): Promise<void> {
if (this.client) return;
const sdk = await loadPeer(
"@mistralai/mistralai",
"Mistral LLM",
() => import("@mistralai/mistralai"),
);
this.client = new sdk.Mistral({ apiKey: this.apiKey });
}
// Helper function to convert content to string
private contentToString(content: any): string {
if (typeof content === "string") {View on GitHub (pinned to 001c235229)
Solutions
- Pass the key explicitly in the config: llm: { provider: 'mistral', config: { apiKey: process.env.MISTRAL_API_KEY } }.
- Ensure MISTRAL_API_KEY is actually set in the environment your process reads (dotenv loaded before construction, or platform env config).
- Do not put the key under modelProperties — the constructor only checks config.apiKey.
- Create the key at console.mistral.ai if you do not have one.
Example fix
// before
// MISTRAL_API_KEY set in env, but constructor ignores env vars
const mem = new Memory({ llm: { provider: 'mistral', config: {} } });
// throws: Mistral API key is required
// after
const mem = new Memory({
llm: { provider: 'mistral', config: { apiKey: process.env.MISTRAL_API_KEY! } },
}); Defensive patterns
Strategy: validation
Validate before calling
function assertMistralKey(cfg: LLMConfig) {
// MistralLLM has NO env fallback — config.apiKey is mandatory
if (!cfg.apiKey) {
throw new Error('Pass apiKey explicitly for provider mistral (env vars are not read)');
}
} Type guard
const hasMistralApiKey = (cfg: LLMConfig): cfg is LLMConfig & { apiKey: string } => typeof cfg.apiKey === 'string' && cfg.apiKey.length > 0; Try / catch
try { new MistralLLM(config); } catch (e) {
if ((e as Error).message === 'Mistral API key is required') {
throw new StartupConfigError('mistral requires config.apiKey (no MISTRAL_API_KEY fallback)');
}
throw e;
} Prevention
- Never rely on MISTRAL_API_KEY alone — this provider reads only config.apiKey.
- Centralize LLM config construction so the explicit-key rule is enforced in one place.
- Boot-time env/config validation catches this before first request.
When it happens
Trigger: Instantiating MistralLLM (provider 'mistral') with apiKey undefined, empty string, or omitted from the LLM config — even if MISTRAL_API_KEY is set in the environment, because the constructor does not consult it.
Common situations: Assuming env-var fallback exists because other mem0 providers (DeepSeek, Groq, Together) have it; passing apiKey: process.env.MISTRAL_API_KEY when the var is not exported; configuring the key under modelProperties instead of the top-level config.
Related errors
- DeepSeek API key is required
- Groq API key is required
- MiniMax API key is required
- Sarvam API key is required
- Together API key is required
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/d739f21293200fb5.
Report an issue: GitHub.