mem0ai/mem0 · error · Error

Sarvam API key is required

Error message

Sarvam API key is required

What it means

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.

Source

Thrown at mem0-ts/src/oss/src/llms/sarvam.ts:18

import { OpenAILLM } from "./openai";
import { LLMConfig, Message } from "../types";
import { LLMResponse } from "./base";

/**
 * Sarvam AI LLM provider.
 *
 * Sarvam's API is OpenAI-compatible, so this simply reuses {@link OpenAILLM}
 * and overrides the connection defaults — mirroring `mem0/llms/sarvam.py` in the
 * Python SDK. The API key resolves from `config.apiKey` or the `SARVAM_API_KEY`
 * env var, and the base URL from `config.baseURL`, `SARVAM_API_BASE`, else
 * `https://api.sarvam.ai/v1`.
 */
export class SarvamLLM extends OpenAILLM {
  constructor(config: LLMConfig) {
    const apiKey = config.apiKey || process.env.SARVAM_API_KEY;
    if (!apiKey) {
      throw new Error("Sarvam API key is required");
    }
    super({
      ...config,
      apiKey,
      baseURL:
        config.baseURL ||
        process.env.SARVAM_API_BASE ||
        "https://api.sarvam.ai/v1",
      model: config.model || "sarvam-m",
    });
  }

  async generateResponse(
    messages: Message[],
    responseFormat?: { type: string },
    tools?: any[],
  ): Promise<string | LLMResponse> {
    try {

View on GitHub (pinned to 001c235229)

Solutions

  1. Create a key at dashboard.sarvam.ai and export SARVAM_API_KEY.
  2. Or pass it in config: llm: { provider: 'sarvam', config: { apiKey: process.env.SARVAM_API_KEY } }.
  3. Load dotenv before constructing Memory.
  4. Add the key to CI/production secrets.

Example fix

// before
const mem = new Memory({ llm: { provider: 'sarvam', config: {} } });
// throws: Sarvam API key is required

// after
import 'dotenv/config';
const mem = new Memory({
  llm: { provider: 'sarvam', config: { apiKey: process.env.SARVAM_API_KEY! } },
});
Defensive patterns

Strategy: validation

Validate before calling

function assertSarvamKey(cfg: LLMConfig) {
  if (!cfg.apiKey && !process.env.SARVAM_API_KEY) {
    throw new Error('SARVAM_API_KEY not set — create one at dashboard.sarvam.ai');
  }
}

Type guard

const hasSarvamKey = (cfg: LLMConfig): boolean => Boolean(cfg.apiKey || process.env.SARVAM_API_KEY);

Try / catch

try { new SarvamLLM(config); } catch (e) {
  if ((e as Error).message === 'Sarvam API key is required') throw new StartupConfigError('SARVAM_API_KEY missing');
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/54e3b4900e51e4f8. Report an issue: GitHub.