> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openp.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Rerank

> POST /v1/rerank — the document reranking endpoint

Re-rank a set of candidate documents by relevance to a query, commonly used for precise ranking after RAG recall.
Fully compatible with the Cohere / Jina Rerank protocol.

## Request

```bash theme={null}
curl https://openp.ai/v1/rerank \
  -H "Authorization: Bearer $OPENPAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "rerank-multilingual-v3.0",
    "query": "What is a vector database?",
    "documents": [
      "A vector database stores high-dimensional vectors for similarity search.",
      "Postgres is a relational database.",
      "FAISS / Milvus are common vector-index solutions."
    ],
    "top_n": 2,
    "return_documents": true
  }'
```

### Parameters

| Field                | Type    | Required | Description                                                                                       |
| -------------------- | ------- | :------: | ------------------------------------------------------------------------------------------------- |
| `model`              | string  |     ✅    | Rerank model ID                                                                                   |
| `query`              | string  |     ✅    | The query string                                                                                  |
| `documents`          | array   |     ✅    | Array of documents, either strings or `{ text }` objects                                          |
| `top_n`              | integer |          | Return the top N, default all                                                                     |
| `return_documents`   | boolean |          | Whether to return the original text in the response (default false, returning only index + score) |
| `max_chunks_per_doc` | integer |          | Cohere v3, chunk overly long documents                                                            |

### Recommended models

| Model ID                             | Vendor | Context |
| ------------------------------------ | ------ | ------- |
| `rerank-multilingual-v3.0`           | Cohere | 4096    |
| `rerank-english-v3.0`                | Cohere | 4096    |
| `jina-reranker-v2-base-multilingual` | Jina   | 1024    |
| `bge-reranker-v2-m3`                 | BAAI   | 8192    |
| `bge-reranker-large`                 | BAAI   | 512     |

## Response

```json theme={null}
{
  "id": "rerank-...",
  "results": [
    {
      "index": 0,
      "relevance_score": 0.98,
      "document": {"text": "A vector database stores high-dimensional vectors for similarity search."}
    },
    {
      "index": 2,
      "relevance_score": 0.83,
      "document": {"text": "FAISS / Milvus are common vector-index solutions."}
    }
  ],
  "meta": {
    "api_version": "1",
    "billed_units": {"search_units": 1}
  }
}
```

`results` is sorted descending by `relevance_score`, and `index` points to the position in the original `documents` array.

## Python (raw HTTP)

```python theme={null}
import requests

resp = requests.post(
    "https://openp.ai/v1/rerank",
    headers={"Authorization": "Bearer sk-..."},
    json={
        "model": "bge-reranker-v2-m3",
        "query": "What is a vector database?",
        "documents": ["...", "...", "..."],
        "top_n": 5,
    },
).json()
print(resp["results"])
```

## Cohere SDK

```python theme={null}
import cohere
client = cohere.Client(api_key="sk-...", base_url="https://openp.ai/v1")

resp = client.rerank(
    model="rerank-multilingual-v3.0",
    query="...",
    documents=["...", "..."],
    top_n=3,
)
```

## Billing

Charged by the total tokens of `query + all documents`; the specific multiplier depends on the model.
