{"record":{"id":"54e3b4900e51e4f8","repo":"mem0ai/mem0","slug":"sarvam-api-key-is-required","errorCode":null,"errorMessage":"Sarvam API key is required","messagePattern":"Sarvam API key is required","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/llms/sarvam.ts","lineNumber":18,"sourceCode":"import { OpenAILLM } from \"./openai\";\nimport { LLMConfig, Message } from \"../types\";\nimport { LLMResponse } from \"./base\";\n\n/**\n * Sarvam AI LLM provider.\n *\n * Sarvam's API is OpenAI-compatible, so this simply reuses {@link OpenAILLM}\n * and overrides the connection defaults — mirroring `mem0/llms/sarvam.py` in the\n * Python SDK. The API key resolves from `config.apiKey` or the `SARVAM_API_KEY`\n * env var, and the base URL from `config.baseURL`, `SARVAM_API_BASE`, else\n * `https://api.sarvam.ai/v1`.\n */\nexport class SarvamLLM extends OpenAILLM {\n  constructor(config: LLMConfig) {\n    const apiKey = config.apiKey || process.env.SARVAM_API_KEY;\n    if (!apiKey) {\n      throw new Error(\"Sarvam API key is required\");\n    }\n    super({\n      ...config,\n      apiKey,\n      baseURL:\n        config.baseURL ||\n        process.env.SARVAM_API_BASE ||\n        \"https://api.sarvam.ai/v1\",\n      model: config.model || \"sarvam-m\",\n    });\n  }\n\n  async generateResponse(\n    messages: Message[],\n    responseFormat?: { type: string },\n    tools?: any[],\n  ): Promise<string | LLMResponse> {\n    try {","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/llms/sarvam.ts#L1-L36","documentation":"Thrown synchronously by the SarvamLLM constructor when no Sarvam AI API key is resolved. The provider checks config.apiKey, then the SARVAM_API_KEY environment variable, and throws when both are absent — mirroring the Python SDK's sarvam.py provider, which fronts Sarvam's OpenAI-compatible API at api.sarvam.ai.","triggerScenarios":"Instantiating SarvamLLM (provider 'sarvam') with apiKey missing and SARVAM_API_KEY unset in the process environment; or with a SARVAM_API_BASE pointing at a proxy while forgetting the key entirely.","commonSituations":"New Sarvam users who have not generated a key on dashboard.sarvam.ai; key configured locally but missing in CI/deploy; dotenv loaded after Memory construction.","solutions":["Create a key at dashboard.sarvam.ai and export SARVAM_API_KEY.","Or pass it in config: llm: { provider: 'sarvam', config: { apiKey: process.env.SARVAM_API_KEY } }.","Load dotenv before constructing Memory.","Add the key to CI/production secrets."],"exampleFix":"// before\nconst mem = new Memory({ llm: { provider: 'sarvam', config: {} } });\n// throws: Sarvam API key is required\n\n// after\nimport 'dotenv/config';\nconst mem = new Memory({\n  llm: { provider: 'sarvam', config: { apiKey: process.env.SARVAM_API_KEY! } },\n});","handlingStrategy":"validation","validationCode":"function assertSarvamKey(cfg: LLMConfig) {\n  if (!cfg.apiKey && !process.env.SARVAM_API_KEY) {\n    throw new Error('SARVAM_API_KEY not set — create one at dashboard.sarvam.ai');\n  }\n}","typeGuard":"const hasSarvamKey = (cfg: LLMConfig): boolean => Boolean(cfg.apiKey || process.env.SARVAM_API_KEY);","tryCatchPattern":"try { new SarvamLLM(config); } catch (e) {\n  if ((e as Error).message === 'Sarvam API key is required') throw new StartupConfigError('SARVAM_API_KEY missing');\n  throw e;\n}","preventionTips":["Export SARVAM_API_KEY or pass apiKey explicitly.","Load dotenv before Memory construction.","Add the key to CI/deploy secrets."],"tags":["sarvam","llm","api-key","config","env","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}