mem0ai/mem0 · critical

Cohere API key is required. Set COHERE_API_KEY environment v

Error message

Cohere API key is required. Set COHERE_API_KEY environment variable or pass apiKey in config.

What it means

Thrown by the CohereReranker constructor when neither config.apiKey nor the COHERE_API_KEY environment variable is set. The reranker needs a Cohere credential for its rerank API; the check happens at construction time (when the reranker is created by RerankerFactory from the memory config), before any request. The cohere npm package itself is imported lazily, so only the key blocks startup.

Source

Thrown at mem0-ts/src/oss/src/rerankers/cohere.ts:19

import { RerankerConfig } from "../types";
import { Reranker, RerankResult } from "./base";
import { loadPeer } from "../utils/load_peer";

const DEFAULT_MODEL = "rerank-v3.5";

export class CohereReranker implements Reranker {
  private clientInstance?: any;
  private clientPromise?: Promise<any>;
  private readonly apiKey: string;
  private model: string;
  private topK?: number;
  private returnDocuments: boolean;
  private maxChunksPerDoc?: number;

  constructor(config: RerankerConfig) {
    const apiKey = config.apiKey || process.env.COHERE_API_KEY;
    if (!apiKey) {
      throw new Error(
        "Cohere API key is required. Set COHERE_API_KEY environment variable or pass apiKey in config.",
      );
    }
    this.apiKey = apiKey;
    this.model = config.model || DEFAULT_MODEL;
    this.topK = config.topK;
    this.returnDocuments = config.returnDocuments ?? false;
    this.maxChunksPerDoc = config.maxChunksPerDoc;
  }

  /**
   * Lazily construct (or reuse) the Cohere client, importing the optional
   * `cohere-ai` peer only when the reranker is first used so consumers that
   * never touch Cohere don't need it installed.
   */
  private async getClient(): Promise<any> {
    if (this.clientInstance) return this.clientInstance;
    if (!this.clientPromise) {

View on GitHub (pinned to 001c235229)

Solutions

  1. Set COHERE_API_KEY in the environment where the process runs (docker-compose environment:, CI secret, deploy env)
  2. Or pass it explicitly: reranker: { provider: 'cohere', config: { apiKey: process.env.COHERE_API_KEY } }
  3. Verify with a startup assertion: if (!process.env.COHERE_API_KEY) throw new Error('missing COHERE_API_KEY') so it fails loudly at boot

Example fix

// before
const memory = new Memory({ reranker: { provider: 'cohere', config: { model: 'rerank-v3.5' } } });

// after
const memory = new Memory({
  reranker: { provider: 'cohere', config: { apiKey: process.env.COHERE_API_KEY, model: 'rerank-v3.5' } },
});
Defensive patterns

Strategy: validation

Validate before calling

if (!process.env.COHERE_API_KEY) {
  throw new Error('COHERE_API_KEY is not set — reranker unavailable');
}

Try / catch

try { new Memory({ ...config, reranker: { provider: 'cohere', config: {} } }); } catch (e) { if (e instanceof Error && e.message.includes('COHERE_API_KEY')) { /* fall back to no reranker */ } else throw e; }

Prevention

When it happens

Trigger: Configuring Memory with reranker: { provider: 'cohere', config: {...} } without an apiKey while process.env.COHERE_API_KEY is unset — typical when the env var is set in local shell but missing in Docker/CI/production, or set after the process started.

Common situations: Env var not exported into Docker Compose or the deployment platform; .env file not loaded before Memory is instantiated; key name typo (COHERE_KEY vs COHERE_API_KEY); rotating to a new environment without copying secrets.

Related errors


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