How to beginner · 3 min read

How to use FlashRank for reranking

Quick answer
Use FlashRank by sending a list of candidate texts or documents to its reranking API endpoint, which scores and orders them by relevance. Typically, you provide the original query and candidate items, then receive a reranked list based on semantic relevance using FlashRank models.

PREREQUISITES

  • Python 3.8+
  • FlashRank API access or compatible reranking API
  • pip install requests

Setup

Install the requests library to call the FlashRank reranking API. Set your API key as an environment variable for secure authentication.

bash
pip install requests

Step by step

This example demonstrates how to call the FlashRank reranking API with a query and candidate documents, then print the reranked results.

python
import os
import requests

API_KEY = os.environ["FLASHRANK_API_KEY"]
API_URL = "https://api.flashrank.ai/v1/rerank"

query = "What are the benefits of AI in healthcare?"
candidates = [
    "AI improves diagnostics accuracy.",
    "AI can automate repetitive tasks.",
    "AI helps in personalized treatment plans.",
    "AI is used in autonomous vehicles."
]

headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json"
}

payload = {
    "query": query,
    "candidates": candidates
}

response = requests.post(API_URL, json=payload, headers=headers)
response.raise_for_status()
reranked = response.json().get("reranked_candidates", [])

print("Reranked candidates:")
for rank, item in enumerate(reranked, 1):
    print(f"{rank}. {item}")
output
Reranked candidates:
1. AI improves diagnostics accuracy.
2. AI helps in personalized treatment plans.
3. AI can automate repetitive tasks.
4. AI is used in autonomous vehicles.

Common variations

You can use async HTTP clients like httpx for asynchronous reranking calls. Some FlashRank SDKs or providers may offer direct Python clients. You can also rerank embeddings by sending vectors instead of raw text if supported.

python
import os
import httpx

async def async_rerank():
    API_KEY = os.environ["FLASHRANK_API_KEY"]
    API_URL = "https://api.flashrank.ai/v1/rerank"

    query = "Benefits of AI in healthcare"
    candidates = [
        "AI improves diagnostics accuracy.",
        "AI automates repetitive tasks.",
        "AI helps personalized treatment.",
        "AI in autonomous vehicles."
    ]

    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    payload = {"query": query, "candidates": candidates}

    async with httpx.AsyncClient() as client:
        response = await client.post(API_URL, json=payload, headers=headers)
        response.raise_for_status()
        reranked = response.json().get("reranked_candidates", [])

    print("Async reranked candidates:")
    for i, item in enumerate(reranked, 1):
        print(f"{i}. {item}")

# To run async_rerank, use an async event loop like asyncio.run(async_rerank())
output
Async reranked candidates:
1. AI improves diagnostics accuracy.
2. AI helps personalized treatment.
3. AI automates repetitive tasks.
4. AI in autonomous vehicles.

Troubleshooting

  • If you receive a 401 Unauthorized error, verify your FLASHRANK_API_KEY environment variable is set correctly.
  • If the response is empty or missing reranked_candidates, check the API payload format and ensure the query and candidates are non-empty strings.
  • For network timeouts, increase your HTTP client timeout or retry with exponential backoff.

Key Takeaways

  • Use FlashRank by sending a query and candidate list to its reranking API endpoint.
  • Always authenticate with your API key via environment variables for security.
  • You can implement reranking synchronously with requests or asynchronously with httpx.
  • Check API response structure carefully to extract reranked results.
  • Handle common errors like authorization failures and empty responses by validating inputs and keys.
Verified 2026-04 · FlashRank reranking API
Verify ↗