mem0ai/mem0 · error · ImportError

cohere package is required for CohereReranker. Install with:

Error message

cohere package is required for CohereReranker. Install with: pip install cohere

What it means

CohereReranker.__init__ raises ImportError when the optional `cohere` package is absent (the module guards its import with a COHERE_AVAILABLE flag). Rerankers are optional extras in mem0, so the class is importable but not constructible without its backing SDK. The message tells you the exact install command.

Source

Thrown at mem0/reranker/cohere_reranker.py:27

    COHERE_AVAILABLE = True
except ImportError:
    COHERE_AVAILABLE = False

logger = logging.getLogger(__name__)


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
            

View on GitHub (pinned to 001c235229)

Solutions

  1. pip install cohere in the environment that runs mem0 (add it to requirements/pyproject extras)
  2. Or remove/disable the reranker config if reranking is optional for you
  3. Verify with python -c "import cohere" against the same interpreter/venv mem0 runs under

Example fix

# before
config = {"reranker": {"provider": "cohere", "config": {"model": "rerank-v3.5", "api_key": ck}}}
memory = Memory.from_config(config)  # ImportError

# after
# pip install cohere
config = {"reranker": {"provider": "cohere", "config": {"model": "rerank-v3.5", "api_key": ck}}}
memory = Memory.from_config(config)
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util
if importlib.util.find_spec("cohere") is None:
    raise RuntimeError("install cohere before enabling the cohere reranker")

Try / catch

try:
    reranker = CohereReranker(cfg)
except ImportError:
    reranker = None  # or fall back to no reranking / another provider

Prevention

When it happens

Trigger: Configuring Memory with reranker={'provider': 'cohere', ...} (or instantiating CohereReranker directly) in an environment without `pip install cohere`; a Docker/CI image built from a minimal mem0 install without extras; adding a reracker config that works on a teammate's machine but not on yours.

Common situations: Enabling reranking for better recall after following docs that assume extras installed; prod images slimmed to cut dependencies; version conflicts where cohere was removed during a dependency prune.

Related errors


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