# mxbai-rerank

> Open-source reranking models that reorder retrieved passages by relevance

- Category: Retrieval & Knowledge (RAG) / Rerank, Search & Hybrid
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/mixedbread-ai/mxbai-rerank
- Website: https://mixedbread.com
- Canonical page: https://ai-tldr.dev/tools/mxbai-rerank/

## In short

mxbai-rerank is Mixedbread's family of open-source reranking models. A reranker takes a query and a list of documents that an earlier retrieval step returned, then scores and reorders those documents by how well they actually answer the query. It sits in the Rerank, Search & Hybrid category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python and licensed Apache-2.0.

## Overview

mxbai-rerank is Mixedbread's family of open-source reranking models. A reranker takes a query and a list of documents that an earlier retrieval step returned, then scores and reorders those documents by how well they actually answer the query. This second pass usually improves the quality of results in a RAG or search pipeline.

It is aimed at developers who already have a retrieval system (vector search, BM25, or a hybrid of both) and want better ordering before passing context to an LLM. The Python package wraps the v2 models so you can load a model and call rank() in a few lines, without building scoring logic yourself.

Within the rerankers and hybrid search category, it acts as the relevance-scoring layer that sits between first-stage retrieval and the final answer. The v2 models support over 100 languages, long context up to 8k tokens, and ranking of code, SQL, and function calls. A managed API is also available for production use.

## What it does

- Cross-encoder rerank() that scores and reorders documents against a query
- Two v2 model sizes: base-v2 (0.5B) for speed and large-v2 (1.5B) for accuracy
- Multilingual support across 100+ languages out of the box
- Long-context input up to 8k tokens (configurable via max_length, up to 32k on v2)
- Optional per-query instructions to guide ranking (e.g. for code snippets)
- Flash Attention 2 used automatically when installed for faster inference

## Getting started

Install the package, then load a v2 model and call rank() with your query and the documents your retriever returned.

1. Install

   Install the package from PyPI.

```bash
pip install -U mxbai-rerank
```

2. Rerank documents

   Initialize a reranker with a model id, then pass your query and candidate documents to rank(). It returns the documents reordered by relevance.

```python
from mxbai_rerank import MxbaiRerankV2

# Initialize the reranker
reranker = MxbaiRerankV2("mixedbread-ai/mxbai-rerank-base-v2")  # or large-v2

query = "Who wrote 'To Kill a Mockingbird'?"
documents = [
    "'To Kill a Mockingbird' is a novel by Harper Lee published in 1960.",
    "The novel 'Moby-Dick' was written by Herman Melville.",
    "Harper Lee was born in 1926 in Monroeville, Alabama."
]

results = reranker.rank(query=query, documents=documents)
print(results)
```

3. Guide ranking with an instruction (optional)

   Pass an instruction to steer how documents are scored, which helps for technical or code-focused queries.

```python
results = reranker.rank(
    query=query,
    documents=documents,
    instruction="Figure out the best code snippet for the user query."
)
```

## When to use it

- Add a second-stage reranking pass to a RAG pipeline so the most relevant chunks reach the LLM
- Improve ordering of hybrid search results that combine vector and keyword retrieval
- Rank code or SQL snippets for technical and developer-facing search
- Serve multilingual search where queries and documents span many languages
