{"record":{"id":"d739f21293200fb5","repo":"mem0ai/mem0","slug":"mistral-api-key-is-required","errorCode":null,"errorMessage":"Mistral API key is required","messagePattern":"Mistral API key is required","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/llms/mistral.ts","lineNumber":13,"sourceCode":"import type { Mistral } from \"@mistralai/mistralai\";\nimport { LLM, LLMResponse } from \"./base\";\nimport { LLMConfig, Message } from \"../types\";\nimport { loadPeer } from \"../utils/load_peer\";\n\nexport class MistralLLM implements LLM {\n  private client!: Mistral;\n  private model: string;\n  private readonly apiKey: string;\n\n  constructor(config: LLMConfig) {\n    if (!config.apiKey) {\n      throw new Error(\"Mistral API key is required\");\n    }\n    this.apiKey = config.apiKey;\n    this.model = config.model || \"mistral-tiny-latest\";\n  }\n\n  private async ensureClient(): Promise<void> {\n    if (this.client) return;\n    const sdk = await loadPeer(\n      \"@mistralai/mistralai\",\n      \"Mistral LLM\",\n      () => import(\"@mistralai/mistralai\"),\n    );\n    this.client = new sdk.Mistral({ apiKey: this.apiKey });\n  }\n\n  // Helper function to convert content to string\n  private contentToString(content: any): string {\n    if (typeof content === \"string\") {","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/llms/mistral.ts#L1-L31","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n// MISTRAL_API_KEY set in env, but constructor ignores env vars\nconst mem = new Memory({ llm: { provider: 'mistral', config: {} } });\n// throws: Mistral API key is required\n\n// after\nconst mem = new Memory({\n  llm: { provider: 'mistral', config: { apiKey: process.env.MISTRAL_API_KEY! } },\n});","handlingStrategy":"validation","validationCode":"function assertMistralKey(cfg: LLMConfig) {\n  // MistralLLM has NO env fallback — config.apiKey is mandatory\n  if (!cfg.apiKey) {\n    throw new Error('Pass apiKey explicitly for provider mistral (env vars are not read)');\n  }\n}","typeGuard":"const hasMistralApiKey = (cfg: LLMConfig): cfg is LLMConfig & { apiKey: string } => typeof cfg.apiKey === 'string' && cfg.apiKey.length > 0;","tryCatchPattern":"try { new MistralLLM(config); } catch (e) {\n  if ((e as Error).message === 'Mistral API key is required') {\n    throw new StartupConfigError('mistral requires config.apiKey (no MISTRAL_API_KEY fallback)');\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["mistral","llm","api-key","config","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}