{"record":{"id":"edebf48f6ac60fe8","repo":"mem0ai/mem0","slug":"cohere-api-key-is-required-set-cohere-api-key-env","errorCode":null,"errorMessage":"Cohere API key is required. Set COHERE_API_KEY environment variable or pass apiKey in config.","messagePattern":"Cohere API key is required\\. Set COHERE_API_KEY environment variable or pass apiKey in config\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"mem0-ts/src/oss/src/rerankers/cohere.ts","lineNumber":19,"sourceCode":"import { RerankerConfig } from \"../types\";\nimport { Reranker, RerankResult } from \"./base\";\nimport { loadPeer } from \"../utils/load_peer\";\n\nconst DEFAULT_MODEL = \"rerank-v3.5\";\n\nexport class CohereReranker implements Reranker {\n  private clientInstance?: any;\n  private clientPromise?: Promise<any>;\n  private readonly apiKey: string;\n  private model: string;\n  private topK?: number;\n  private returnDocuments: boolean;\n  private maxChunksPerDoc?: number;\n\n  constructor(config: RerankerConfig) {\n    const apiKey = config.apiKey || process.env.COHERE_API_KEY;\n    if (!apiKey) {\n      throw new Error(\n        \"Cohere API key is required. Set COHERE_API_KEY environment variable or pass apiKey in config.\",\n      );\n    }\n    this.apiKey = apiKey;\n    this.model = config.model || DEFAULT_MODEL;\n    this.topK = config.topK;\n    this.returnDocuments = config.returnDocuments ?? false;\n    this.maxChunksPerDoc = config.maxChunksPerDoc;\n  }\n\n  /**\n   * Lazily construct (or reuse) the Cohere client, importing the optional\n   * `cohere-ai` peer only when the reranker is first used so consumers that\n   * never touch Cohere don't need it installed.\n   */\n  private async getClient(): Promise<any> {\n    if (this.clientInstance) return this.clientInstance;\n    if (!this.clientPromise) {","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/rerankers/cohere.ts#L1-L37","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set COHERE_API_KEY in the environment where the process runs (docker-compose environment:, CI secret, deploy env)","Or pass it explicitly: reranker: { provider: 'cohere', config: { apiKey: process.env.COHERE_API_KEY } }","Verify with a startup assertion: if (!process.env.COHERE_API_KEY) throw new Error('missing COHERE_API_KEY') so it fails loudly at boot"],"exampleFix":"// before\nconst memory = new Memory({ reranker: { provider: 'cohere', config: { model: 'rerank-v3.5' } } });\n\n// after\nconst memory = new Memory({\n  reranker: { provider: 'cohere', config: { apiKey: process.env.COHERE_API_KEY, model: 'rerank-v3.5' } },\n});","handlingStrategy":"validation","validationCode":"if (!process.env.COHERE_API_KEY) {\n  throw new Error('COHERE_API_KEY is not set — reranker unavailable');\n}","typeGuard":null,"tryCatchPattern":"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; }","preventionTips":["Assert required API keys at process startup, not at first use","Declare env vars explicitly in Docker Compose / deploy configs so they cannot silently vanish"],"tags":["configuration","cohere","reranker","environment","secrets"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}