Rerank
/v1/rerank takes a query and a list of documents and returns the documents ordered by relevance with a score for each. Rerankers improve retrieval pipelines by re-scoring the candidates a vector search returned, so the model gets the few passages that matter instead of the twenty that merely match keywords. The gateway serves one request format for every rerank provider and prices calls per query or per token as the price map records.
Scenario
A documentation assistant searches a vector store and gets twenty candidate passages back. Before building the prompt it sends the question and the candidates to a rerank model through the gateway and keeps the top three. The answers get better and the prompt gets shorter, and the rerank calls are logged and costed under the assistant's key next to its searches and completions.
Steps
-
Register a rerank model. In the Admin UI open Models + Endpoints (
/ui/models-and-endpoints), click Add Model, pick the Provider (for example Cohere), choose the model, set the Public Model Name torerank-v3.5and set Mode to Rerank - /rerank so health checks send a rerank request, then click Add Model.model_list:- model_name: rerank-v3.5spanway_params:model: cohere/rerank-v3.5api_key: os.environ/COHERE_API_KEYmodel_info:mode: rerank -
Create a key in Virtual Keys (
/ui/api-keys) and pick the model under Models. -
Rerank a few documents.
curl -X POST http://localhost:4000/v1/rerank \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d '{"model": "rerank-v3.5","query": "How do I rotate a virtual key?","documents": ["Keys are rotated with POST /key/regenerate.","Budgets reset on the schedule in budget_duration.","Teams group users and keys."],"top_n": 2,"return_documents": true}' -
Check the log. Open Logs (
/ui/logs) and find the request with its cost.
Expected result
{
"id": "rr-6a1f9b2c",
"results": [
{"index": 0, "relevance_score": 0.98, "document": {"text": "Keys are rotated with POST /key/regenerate."}},
{"index": 2, "relevance_score": 0.12, "document": {"text": "Teams group users and keys."}}
],
"meta": {"billed_units": {"search_units": 1}}
}
Request fields
| Field | Meaning |
|---|---|
query | The search query. |
documents | Strings, or objects with a text field. |
top_n | Number of results to return. |
return_documents | Include document text in results. |
rank_fields, max_chunks_per_doc | Provider options forwarded where supported. |
/v2/rerank and /rerank are the same endpoint. Rerank calls are cached when rerank is in the cache's supported_call_types. Rerank models are available from Cohere, Jina AI, Voyage, Bedrock, Azure AI, Together AI, Infinity, Hugging Face and others, listed below.
In retrieval pipelines
The RAG query endpoint can rerank vector store results before generation with a rerank block naming one of these models, so you do not have to call this endpoint yourself.
Errors
| Status | What it means | Fix |
|---|---|---|
400 BadRequestError | query or documents is missing, documents is empty, or top_n is larger than the provider allows. | Send a query and at least one document. |
401 AuthenticationError | The key is missing, wrong or expired. | Use a live key. |
403 PermissionDeniedError | The key may not use the model. | Add the model to the key. |
404 NotFoundError | No deployment carries this model name. | Check Models + Endpoints. |
429 RateLimitError | Budget or rate limit reached, or the provider is throttling. | Raise the limit or add a deployment. |
Cost shows 0 | The model has no per-query or per-token price. | Set a price on the deployment with custom pricing. |
Related
- RAG query for rerank inside a retrieval call.
- Search a vector store for getting the candidates.
- Caching for caching rerank calls.
Providers
The gateway's endpoint support matrix marks 11 provider integrations as supporting this endpoint:
AIHubMix (aihubmix), AWS - Bedrock, Cohere, Fireworks AI, Hosted VLLM, Huggingface, Qianwen AI Platform, QwenCloud, Scaleway, VLLM, VoyageAI by MongoDB.