{"record":{"id":"ca116b0078d72d6a","repo":"mem0ai/mem0","slug":"cohere-package-is-required-for-coherereranker-ins","errorCode":null,"errorMessage":"cohere package is required for CohereReranker. Install with: pip install cohere","messagePattern":"cohere package is required for CohereReranker\\. Install with: pip install cohere","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"mem0/reranker/cohere_reranker.py","lineNumber":27,"sourceCode":"    COHERE_AVAILABLE = True\nexcept ImportError:\n    COHERE_AVAILABLE = False\n\nlogger = logging.getLogger(__name__)\n\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            ","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/reranker/cohere_reranker.py#L9-L45","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["pip install cohere in the environment that runs mem0 (add it to requirements/pyproject extras)","Or remove/disable the reranker config if reranking is optional for you","Verify with python -c \"import cohere\" against the same interpreter/venv mem0 runs under"],"exampleFix":"# before\nconfig = {\"reranker\": {\"provider\": \"cohere\", \"config\": {\"model\": \"rerank-v3.5\", \"api_key\": ck}}}\nmemory = Memory.from_config(config)  # ImportError\n\n# after\n# pip install cohere\nconfig = {\"reranker\": {\"provider\": \"cohere\", \"config\": {\"model\": \"rerank-v3.5\", \"api_key\": ck}}}\nmemory = Memory.from_config(config)","handlingStrategy":"validation","validationCode":"import importlib.util\nif importlib.util.find_spec(\"cohere\") is None:\n    raise RuntimeError(\"install cohere before enabling the cohere reranker\")","typeGuard":null,"tryCatchPattern":"try:\n    reranker = CohereReranker(cfg)\nexcept ImportError:\n    reranker = None  # or fall back to no reranking / another provider","preventionTips":["Declare optional provider deps in the same lockfile as mem0","Add a config-driven dependency check at app startup","Use lean prod images only after confirming which extras your config needs"],"tags":["reranker","cohere","dependency","import-error","configuration"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}