mem0ai/mem0 · error · ValueError
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 api_key in config.
What it means
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.
Source
Thrown at mem0/reranker/cohere_reranker.py:32
class CohereReranker(BaseReranker):
"""Cohere-based reranker implementation."""
def __init__(self, config):
"""
Initialize Cohere reranker.
Args:
config: CohereRerankerConfig object with configuration parameters
"""
if not COHERE_AVAILABLE:
raise ImportError("cohere package is required for CohereReranker. Install with: pip install cohere")
self.config = config
self.api_key = config.api_key or os.getenv("COHERE_API_KEY")
if not self.api_key:
raise ValueError("Cohere API key is required. Set COHERE_API_KEY environment variable or pass api_key in config.")
self.model = config.model
self.client = cohere.Client(self.api_key)
def rerank(self, query: str, documents: List[Dict[str, Any]], top_k: int = None) -> List[Dict[str, Any]]:
"""
Rerank documents using Cohere's rerank API.
Args:
query: The search query
documents: List of documents to rerank
top_k: Number of top documents to return
Returns:
List of reranked documents with rerank_score
"""
if not documents:
return documentsView on GitHub (pinned to 001c235229)
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
Example fix
# before
config = {"reranker": {"provider": "cohere", "config": {"model": "rerank-v3.5"}}}
# no COHERE_API_KEY set -> ValueError
# after
import os
os.environ["COHERE_API_KEY"] = os.environ["COHERE_API_KEY"] # from secret manager
config = {"reranker": {"provider": "cohere", "config": {"model": "rerank-v3.5", "api_key": os.environ["COHERE_API_KEY"]}}} Defensive patterns
Strategy: validation
Validate before calling
import os
cfg = {"provider": "cohere", "config": {"api_key": os.getenv("COHERE_API_KEY")}}
if not cfg["config"]["api_key"]:
raise RuntimeError("COHERE_API_KEY not set; refusing to build cohere reranker") Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cohere API key is required. Set COHERE_API_KEY environment v
- Zero Entropy API key is required. Set ZERO_ENTROPY_API_KEY e
- Zero Entropy API key is required. Set ZERO_ENTROPY_API_KEY e
- Pinecone API key required: pass apiKey or set PINECONE_API_K
- cohere package is required for CohereReranker. Install with:
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/bf9fa6e8bed878a3.
Report an issue: GitHub.