{"record":{"id":"bf9fa6e8bed878a3","repo":"mem0ai/mem0","slug":"cohere-api-key-is-required-set-cohere-api-key-env-bf9fa6","errorCode":null,"errorMessage":"Cohere API key is required. Set COHERE_API_KEY environment variable or pass api_key in config.","messagePattern":"Cohere API key is required\\. Set COHERE_API_KEY environment variable or pass api_key in config\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/reranker/cohere_reranker.py","lineNumber":32,"sourceCode":"\n\nclass CohereReranker(BaseReranker):\n    \"\"\"Cohere-based reranker implementation.\"\"\"\n    \n    def __init__(self, config):\n        \"\"\"\n        Initialize Cohere reranker.\n        \n        Args:\n            config: CohereRerankerConfig object with configuration parameters\n        \"\"\"\n        if not COHERE_AVAILABLE:\n            raise ImportError(\"cohere package is required for CohereReranker. Install with: pip install cohere\")\n        \n        self.config = config\n        self.api_key = config.api_key or os.getenv(\"COHERE_API_KEY\")\n        if not self.api_key:\n            raise ValueError(\"Cohere API key is required. Set COHERE_API_KEY environment variable or pass api_key in config.\")\n            \n        self.model = config.model\n        self.client = cohere.Client(self.api_key)\n        \n    def rerank(self, query: str, documents: List[Dict[str, Any]], top_k: int = None) -> List[Dict[str, Any]]:\n        \"\"\"\n        Rerank documents using Cohere's rerank API.\n        \n        Args:\n            query: The search query\n            documents: List of documents to rerank\n            top_k: Number of top documents to return\n            \n        Returns:\n            List of reranked documents with rerank_score\n        \"\"\"\n        if not documents:\n            return documents","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/reranker/cohere_reranker.py#L14-L50","documentation":"CohereReranker requires an API key: config.api_key or the COHERE_API_KEY environment variable; if both are empty it raises ValueError at construction. The key is needed to build cohere.Client for the rerank API. This fails before any network call, at Memory.from_config()/init time.","triggerScenarios":"Configuring the cohere reranker with no api_key in config and no COHERE_API_KEY exported; setting the var in your shell but not in the Docker container/systemd unit/CI where the process actually runs; config.api_key set to an empty string (falsy) with the env var also unset.","commonSituations":"Local config works, deployed environment lacks the env var; .env file not loaded by the service; secrets moved to a vault but the reranker config never updated; empty-string defaults from template configs.","solutions":["Export COHERE_API_KEY in the environment where mem0 runs, or pass config: {'provider':'cohere','config':{'api_key': ...}}","For containers/CI, inject the variable at deploy time and verify with print(os.getenv('COHERE_API_KEY')) inside the process","Never commit the key; use env/secret managers"],"exampleFix":"# before\nconfig = {\"reranker\": {\"provider\": \"cohere\", \"config\": {\"model\": \"rerank-v3.5\"}}}\n# no COHERE_API_KEY set -> ValueError\n\n# after\nimport os\nos.environ[\"COHERE_API_KEY\"] = os.environ[\"COHERE_API_KEY\"]  # from secret manager\nconfig = {\"reranker\": {\"provider\": \"cohere\", \"config\": {\"model\": \"rerank-v3.5\", \"api_key\": os.environ[\"COHERE_API_KEY\"]}}}","handlingStrategy":"validation","validationCode":"import os\ncfg = {\"provider\": \"cohere\", \"config\": {\"api_key\": os.getenv(\"COHERE_API_KEY\")}}\nif not cfg[\"config\"][\"api_key\"]:\n    raise RuntimeError(\"COHERE_API_KEY not set; refusing to build cohere reranker\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Fail fast at startup on missing secrets rather than at Memory construction","Inject secrets via env/secret manager in every environment including CI","Never default api_key to empty string; make absence loud"],"tags":["reranker","cohere","api-key","configuration","environment"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}