run-llama/llama_index · error · ImportError
Cannot import cohere package, please `pip install cohere`.
Error message
Cannot import cohere package, please `pip install cohere`.
What it means
Raised by CohereRerankRelevancyMetric.__init__ when it tries `from cohere import Client` and the optional cohere package is not installed. llama-index-core declares cohere as an optional dependency (note the `# pants: no-infer-dep` marker), so the rerank relevancy metric fails at construction time on any environment without it.
Source
Thrown at llama-index-core/llama_index/core/evaluation/retrieval/metrics.py:453
_client: Any = PrivateAttr()
def __init__(
self,
model: str = "rerank-english-v2.0",
api_key: Optional[str] = None,
):
try:
api_key = api_key or os.environ["COHERE_API_KEY"]
except IndexError:
raise ValueError(
"Must pass in cohere api key or "
"specify via COHERE_API_KEY environment variable "
)
try:
from cohere import Client # pants: no-infer-dep
except ImportError:
raise ImportError(
"Cannot import cohere package, please `pip install cohere`."
)
super().__init__(model=model)
self._client = Client(api_key=api_key)
def _get_agg_func(self, agg: Literal["max", "median", "mean"]) -> Callable:
"""Get agg func."""
return _AGG_FUNC[agg]
def compute(
self,
query: Optional[str] = None,
expected_ids: Optional[List[str]] = None,
retrieved_ids: Optional[List[str]] = None,
expected_texts: Optional[List[str]] = None,
retrieved_texts: Optional[List[str]] = None,
agg: Literal["max", "median", "mean"] = "max",View on GitHub (pinned to afd0fef371)
Solutions
- Install the SDK: pip install cohere (and pin it in your dependency file).
- If you do not need Cohere reranking, drop 'cohere_rerank_relevancy' from the metrics list and use the built-in hits/mrr/precision/recall metrics instead.
- Gate the metric behind an availability check (importlib.util.find_spec('cohere')) so evaluation configs degrade gracefully on environments without the package.
Example fix
# before metric = CohereRerankRelevancyMetric() # ModuleNotFoundError -> wrapped ImportError # after pip install cohere metric = CohereRerankRelevancyMetric()
Defensive patterns
Strategy: validation
Validate before calling
import importlib.util
if importlib.util.find_spec("cohere") is None:
raise RuntimeError("cohere package required for rerank relevancy: pip install cohere") Try / catch
try:
metric = CohereRerankRelevancyMetric(api_key=key)
except ImportError as e:
if "pip install cohere" in str(e):
log.warning("cohere not installed; skipping rerank relevancy metric")
metric = None
else:
raise Prevention
- Add cohere to your lockfile whenever the cohere_rerank_relevancy metric is registered.
- Gate optional-dependency features behind find_spec checks in eval harnesses.
- Run dependency smoke tests in CI that construct every configured metric.
When it happens
Trigger: Constructing CohereRerankRelevancyMetric (directly or through RetrieverEvaluator with the 'cohere_rerank_relevancy' metric) on an environment where `pip install cohere` was never run — typically a slim Docker image or a fresh venv holding only llama-index-core.
Common situations: Adding cohere-rerank evaluation to an existing project without updating requirements.txt; CI images that cache old dependency locks; upgrading llama-index and losing a transitive cohere install that used to come from another integration package.
Related errors
- `llama-index-embeddings-openai` package not found, please ru
- `llama-index-embeddings-clip` package not found, please run
- `llama-index-embeddings-huggingface` package not found, plea
- `llama-index-embeddings-langchain` package not found, please
- llama-index-llms-openai is not installed. Please install it
AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15).
Data as JSON: /api/errors/b700b04a3c072417.
Report an issue: GitHub.