Skip to content

[serve][llm] Add /v1/rerank and /v2/rerank to OpenAiIngress - #68623

Draft
mbartholet wants to merge 1 commit into
ray-project:masterfrom
mbartholet:serve-llm-rerank
Draft

mbartholet wants to merge 1 commit into
ray-project:masterfrom
mbartholet:serve-llm-rerank

Conversation

@mbartholet

Copy link
Copy Markdown

Description

OpenAiIngress serves /v1/score, but not vLLM's rerank API. vLLM serves rerank at /v1/rerank (Jina and Cohere v1 clients) and /v2/rerank (Cohere v2 client), with the same handler as score. Clients that call it, such as the Cohere SDK and the rerankers in LangChain and LlamaIndex, get a 404 from a build_openai_app deployment in the default mode today. (Direct-streaming mode already serves vLLM's own rerank routes, so this also brings the two modes to parity.)

This PR adds POST /v1/rerank and POST /v2/rerank, wired the same way as /v1/score:

ingress rerank() → LLMServer.rerank() → VLLMEngine.rerank() → vLLM's ServingScores (the existing _oai_serving_scores handler).

/v2/rerank is a second ingress method, rerank_v2(), that calls rerank(), the same way vLLM's own router does it. RerankRequest and RerankResponse wrap vLLM's models, like ScoreRequest and ScoreResponse, so vLLM's rerank options work unchanged, including top_n, max_tokens_per_doc and truncate_prompt_tokens.

Example, from a build_openai_app deployment of BAAI/bge-reranker-v2-m3:

POST /v1/rerank (or /v2/rerank)
     {"model": "bge", "query": "What is the capital of France?",
      "documents": ["Berlin is the capital of Germany.", "Paris is the capital and largest city of France.", ...],
      "top_n": 2}
master: 404 {"detail": "Not Found"}   (default mode)
PR:     200 {"id": "...", "model": "bge", "usage": {...},
             "results": [{"index": 1, "document": {"text": "Paris is ..."}, "relevance_score": 0.99...}, ...]}

I used an AI coding assistant for parts of the implementation and testing; I reviewed every change and ran the tests myself.

Related issues

None. I found no issue or PR that adds rerank to the default ingress. The only open PR that mentions rerank is #68528 (see the notes). #55914 added /v1/score; this PR follows the same pattern.

Additional information

Notes for reviewers

  • VLLMEngine.rerank() repeats score() with the request and response types swapped. That is the pattern the other endpoints use, and it leaves score() untouched.
  • Why the score error message changes: a model without a scoring handler now gets "The score and rerank endpoints are not available for this model." from both endpoints, instead of a message that names only score.
  • Paths: /v1/rerank and /v2/rerank are vLLM's versioned rerank paths. The unversioned /rerank is left out, as the ingress doesn't serve the unversioned /score either.
  • [serve][llm] Serve only inference routes under direct streaming #68528 (direct-streaming route allowlist): its allowlist has /v1/rerank and /rerank but not /v2/rerank. If both merge, /v2/rerank would work in the default mode and return 404 in direct-streaming mode, and test_rerank_model would fail in the direct-streaming release job. Adding "/v2/rerank" to that allowlist fixes both. No textual conflict.
  • SGLang: RerankRequest and RerankResponse are stubs on the SGLang backend, like the transcription models, so rerank requests to an SGLang deployment return a 500 instead of a 404, as transcription requests do today. The SGLang guide lists /v1/rerank next to score.
  • Compatibility: a subclass of OpenAiIngress that already has a method named rerank now gets it registered at /v1/rerank, and /v2/rerank calls it with a RerankRequest. A custom endpoint_map entry for these paths still wins if it comes before **DEFAULT_ENDPOINTS.

Tests

  • test_vllm_engine.py (new, real VLLMEngine with a fake vLLM scoring handler): VLLMEngine.rerank() passes the request to the scoring handler and parses its response. A model without a scoring handler gets a 400 that names rerank, and a bad request from vLLM maps to a 400.
  • test_router.py (mock engine): /v1/rerank and /v2/rerank through the HTTP ingress, with top_n.
  • test_llm_server.py (mock engine): the rerank path through LLMServer, like the score test.
  • release/llm_tests/serve/test_llm_serve_integration.py: test_rerank_model with BAAI/bge-reranker-base on both paths, next to test_score_model.

Test runs (Ray master 5651cbd nightly wheel + vLLM 0.34.0, 1× A40)

# new tests
pytest python/ray/llm/tests/serve/cpu/deployments/llm/vllm/test_vllm_engine.py \
       python/ray/llm/tests/serve/cpu/deployments/llm/test_llm_server.py \
       python/ray/llm/tests/serve/cpu/deployments/routers/test_router.py -k rerank
# related suites, one file per run (as in CI)
cd python && for f in ray/llm/tests/serve/cpu/configs/test_*.py \
    ray/llm/tests/serve/cpu/deployments/llm/test_llm_{server,engine}.py \
    ray/llm/tests/serve/cpu/deployments/llm/vllm/test_*.py \
    ray/llm/tests/serve/cpu/deployments/llm/vllm/*/test_*.py \
    ray/llm/tests/serve/cpu/deployments/routers/test_*.py \
    ray/llm/tests/serve/utils/test_*.py; do pytest -q "$f"; done
# release tests
cd release/llm_tests/serve && pytest test_llm_serve_integration.py -k "test_score_model or test_rerank_model"
result
New tests on master fail (RerankRequest missing)
New tests with the PR 7 passed
Related suites, 27 files all pass except 4 unrelated tests (below)
Release tests test_score_model + test_rerank_model 2 passed in default mode, 2 passed in direct-streaming mode
GPU E2E, bge-reranker-v2-m3 + Qwen2.5-0.5B-Instruct 14/14 passed

The 4 unrelated tests:

  • test_multi_node_placement_groups.py::test_llm_serve_data_parallel_placement_override and the 2 test_stable_streaming_tpot[0-16-64-True-*] timing tests also fail on unchanged master on this machine.
  • test_builder_ingress.py::test_build_openai_app_with_config hung in serve run on this machine, so I stopped it; the rest of that file passes.

The GPU E2E checks, on both paths:

  • /v2/rerank returns the same results as /v1/rerank, and the scores match /v1/score;
  • top_n cuts the list, and max_tokens_per_doc truncates a 12k-token document;
  • the Cohere SDK v1 and v2 clients work unchanged;
  • a chat model gets a 4xx that names rerank, and an unknown model gets a 404;
  • 128 concurrent requests all return 200.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for document reranking endpoints (/v1/rerank and /v2/rerank) powered by vLLM's scoring and reranking handlers. It adds the necessary request and response schemas, updates the FastAPI ingress router, wires the rerank method through the LLM server to the VLLM engine, and includes comprehensive unit, mock, and integration tests. I have no review comments or feedback to provide.

@mbartholet
mbartholet marked this pull request as draft November 28, 2026 21:27

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant