{"id":9085,"library":"llama-index-postprocessor-cohere-rerank","title":"Cohere Rerank Postprocessor for LlamaIndex","description":"The llama-index-postprocessor-cohere-rerank library provides an integration for using Cohere's Rerank API within LlamaIndex, a data framework for LLM applications. This postprocessor is designed to enhance the relevance of retrieved documents in Retrieval-Augmented Generation (RAG) pipelines by re-ranking them based on semantic relevance. It is part of the broader LlamaIndex ecosystem, which typically sees frequent updates to its integration packages.","status":"active","version":"0.8.0","language":"en","source_language":"en","source_url":"https://github.com/run-llama/llama_index/tree/main/llama-index-integrations/postprocessor/cohere_rerank","tags":["llama-index","rerank","cohere","LLM","RAG","postprocessor","NLP","information retrieval"],"install":[{"cmd":"pip install llama-index-postprocessor-cohere-rerank","lang":"bash","label":"Install package"}],"dependencies":[{"reason":"This package is a LlamaIndex integration and depends on the core LlamaIndex library for fundamental abstractions.","package":"llama-index-core","optional":false},{"reason":"Direct dependency for interacting with the Cohere Rerank API.","package":"cohere","optional":false}],"imports":[{"note":"CohereRerank is a dedicated integration. 'CoherenceRank' is a different module, and 'llama_index.core.postprocessor' is for core rerankers, not specific integrations.","wrong":"from llama_index.core.postprocessor import CoherenceRank","symbol":"CohereRerank","correct":"from llama_index.postprocessor.cohere_rerank import CohereRerank"}],"quickstart":{"code":"import os\nfrom llama_index.core import VectorStoreIndex, SimpleDirectoryReader\nfrom llama_index.postprocessor.cohere_rerank import CohereRerank\nfrom llama_index.readers.web import SimpleWebPageReader\n\n# Set your Cohere API key\nos.environ[\"COHERE_API_KEY\"] = os.environ.get(\"COHERE_API_KEY\", \"your_cohere_api_key\")\n\n# Load documents (using a dummy example for demonstration)\ndocuments = SimpleWebPageReader(html_to_text=True).load_data(\n    [\"https://docs.cohere.com/v2/docs/cohere-embed\"]\n)\n\n# Build a VectorStoreIndex\nindex = VectorStoreIndex.from_documents(documents=documents)\n\n# Initialize the Cohere Rerank postprocessor\ncohere_rerank = CohereRerank(\n    api_key=os.environ[\"COHERE_API_KEY\"],\n    model=\"rerank-english-v3.0\", # Use an appropriate Cohere rerank model\n    top_n=2 # Number of top results to return after reranking\n)\n\n# Create a query engine with the reranker\nquery_engine = index.as_query_engine(\n    similarity_top_k=10, # Retrieve more nodes initially for reranking\n    node_postprocessors=[cohere_rerank]\n)\n\n# Query the index\nresponse = query_engine.query(\"What is Cohere's Embed Model?\")\n\nprint(response.response)\n# To see source nodes if needed\n# from llama_index.core.response.pprint_utils import pprint_response\n# pprint_response(response, show_source=True)","lang":"python","description":"This quickstart demonstrates how to set up and use the CohereRerank postprocessor with a LlamaIndex VectorStoreIndex. It involves installing the necessary packages, setting the COHERE_API_KEY, loading some example data, building an index, and then configuring the query engine to use CohereRerank to refine the retrieval results."},"warnings":[{"fix":"Ensure `llama-index-postprocessor-cohere-rerank` is installed explicitly and use the correct import path: `from llama_index.postprocessor.cohere_rerank import CohereRerank`.","message":"LlamaIndex v0.10 introduced a major packaging refactor. All integrations, including 'llama-index-postprocessor-cohere-rerank', were split into separate PyPI packages. Direct imports from `llama_index.indices.postprocessor` or older paths will break.","severity":"breaking","affected_versions":"0.1.0 - 0.9.x (prior to v0.10 of LlamaIndex core library)"},{"fix":"Carefully manage `cohere` package versions, ensuring compatibility across all `llama-index` integrations using `cohere`. Consult `pyproject.toml` or `setup.py` files for exact `cohere` version pins for each integration. Upgrading all `llama-index` and `cohere` related packages to their latest compatible versions is often the best solution.","message":"Potential version compatibility issues between `llama-index-postprocessor-cohere-rerank` and `llama-index-embeddings-cohere` due to differing `cohere` package version requirements. This can lead to dependency resolution failures.","severity":"gotcha","affected_versions":"All versions, depending on specific combinations of `llama-index` integration packages."},{"fix":"Set `os.environ[\"COHERE_API_KEY\"] = \"your_key_here\"` before initializing `CohereRerank`, or pass `api_key=\"your_key_here\"` to the constructor.","message":"The Cohere API key (`COHERE_API_KEY`) must be correctly provided, either as an environment variable or directly to the `CohereRerank` constructor. Failure to do so will result in authentication errors or runtime exceptions.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Attempt to upgrade all `llama-index-*` packages and the `cohere` package to their latest compatible versions. If conflicts persist, try isolating the conflicting packages in separate environments or manually specifying `cohere` versions that satisfy both (if possible).","cause":"Conflicting version requirements for the `cohere` library between different LlamaIndex integration packages.","error":"Because no versions of llama-index-postprocessor-cohere-rerank match >0.1.2,<0.2.0 and llama-index-postprocessor-cohere-rerank (0.1.2) depends on cohere (>=4.45,<5.0), llama-index-postprocessor-cohere-rerank (>=0.1.2,<0.2.0) requires cohere (>=4.45,<5.0). And because llama-index-embeddings-cohere (0.1.5) depends on cohere (>=5.1.1,<6.0.0) and no versions of llama-index-embeddings-cohere match >0.1.5,<0.2.0, llama-index-postprocessor-cohere-rerank (>=0.1.2,<0.2.0) is incompatible with llama-index-embeddings-cohere (>=0.1.5,<0.2.0)."},{"fix":"Ensure the package is installed with `pip install llama-index-postprocessor-cohere-rerank` and use the correct import: `from llama_index.postprocessor.cohere_rerank import CohereRerank`.","cause":"The `llama-index-postprocessor-cohere-rerank` package is not installed, or the import path is incorrect, possibly due to a pre-v0.10 LlamaIndex import pattern.","error":"ModuleNotFoundError: No module named 'llama_index.postprocessor.cohere_rerank'"}]}