Skip to content

[Serve][LLM][3/5] Add multi-application OpenAI routing - #66519

Merged
eicherseiji merged 1 commit into
masterfrom
celina/router-app-3-llm-router-control
Nov 3, 2026
Merged

eicherseiji merged 1 commit into
masterfrom
celina/router-app-3-llm-router-control

Conversation

@celinaky

@celinaky celinaky commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Why this is needed

Multi-model direct streaming deploys each model as an independent Serve application. This PR adds a lightweight shared OpenAI router application that maps the model ID in each request to one of those applications, then asks the request router for the target application to select a replica. HAProxy sends the original request directly to that replica, preserving the direct streaming data path.

Usage

from ray.llm._internal.serve.core.ingress.builder import build_openai_router_app

build_openai_router_app(
    {
        "model_applications": {
            "qwen-0.5b": "llm-model-qwen",
            "llama-8b": "llm-model-llama",
        },
        "llm_configs": [
            {"model_loading_config": {"model_id": "qwen-0.5b"}},
            {"model_loading_config": {"model_id": "llama-8b"}},
        ],
    }
)

The builder is private, requires HAProxy and LLM direct streaming, and accepts the same dictionary through declarative YAML. It requires exactly one LLMConfig for every model-to-application mapping. Updating this router does not replace or delete the independently deployed model applications.

Design

RouterApplication implements the private POST /internal/route protocol introduced in #66507. It reads the model from JSON or form data, resolves and caches the target application handle, and passes the routing payload and session ID to the request router for that application. The response contains the target application name and full replica ID for HAProxy to validate and dispatch.

Native model routes are delegated without maintaining a duplicate allowlist. The router itself owns only the global model endpoints: GET and HEAD /v1/models and /v1/models/{model}. These endpoints do not wake scale-to-zero model applications. They enforce each target model API key policy, expose only authorized or public models, and return vLLM-compatible errors. CORS preflight is handled before model extraction.

Handle lookup runs in background tasks and retries transient Serve and Ray failures. Lookup and replica selection remain bounded by the HAProxy decision deadline. Client errors, unavailable applications, and router-owned responses use the marked response path from #66507.

This layer uses a fast non-reserving pick and therefore requires an available target replica. #67087 adds bounded reserving selection for scale-to-zero targets, and #67218 preserves KVAwareRouter behavior in the shared router process.

Test plan

  • test_router_application.py covers model parsing, authentication, handle lookup, selection, session affinity, errors, and timeouts.
  • test_builder_openai_router_app.py covers builder validation, declarative arguments, and router-role propagation.
  • test_openai_router_app_direct_streaming.py covers the end-to-end multi-application path and native OpenAI routes.
  • Pinned Black 22.10.0 and pre-commit checks on changed files.

@celinaky
celinaky added this pull request to stack #66518 September 27, 2026 19:26
@celinaky celinaky added the go add ONLY when ready to merge, run all tests label Sep 29, 2026
@celinaky
celinaky force-pushed the celina/router-app-3-llm-router-control branch from ee71ee8 to dfcacc8 Compare October 1, 2026 06:01
@celinaky celinaky changed the title [Serve][3/4] [Serve][LLM][3/4] Add RouterApplication for multi-application OpenAI routing Oct 1, 2026
@celinaky
celinaky force-pushed the celina/router-app-3-llm-router-control branch from dfcacc8 to 977dfee Compare October 1, 2026 22:42
@celinaky
celinaky force-pushed the celina/router-app-3-llm-router-control branch from 977dfee to e5db483 Compare October 5, 2026 00:19
@eicherseiji
eicherseiji marked this pull request as ready for review October 13, 2026 22:11
@eicherseiji
eicherseiji requested a review from a team as a code owner October 13, 2026 22:11

@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 a lightweight, FastAPI-based RouterApplication to route OpenAI requests across independently deployed model applications in Ray Serve, along with comprehensive unit tests. The router resolves application handles in the background, serves static model metadata directly, and implements replica selection with fallback mechanisms. The review feedback suggests enhancing the robustness of the background handle resolution by catching the broader RayError exception, implementing a __del__ destructor to prevent task leaks, and updating the check_health method to monitor and report failures in these background tasks.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

eicherseiji added a commit that referenced this pull request Oct 13, 2026
eicherseiji added a commit that referenced this pull request Oct 13, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

eicherseiji added a commit that referenced this pull request Oct 13, 2026
eicherseiji added a commit that referenced this pull request Oct 13, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

eicherseiji added a commit that referenced this pull request Oct 14, 2026
eicherseiji added a commit that referenced this pull request Oct 14, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@ray-gardener ray-gardener Bot added serve Ray Serve Related Issue llm labels Oct 14, 2026
eicherseiji added a commit that referenced this pull request Oct 14, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@eicherseiji eicherseiji changed the title [Serve][LLM][3/4] Add RouterApplication for multi-application OpenAI routing [Serve][LLM][3/5] Add RouterApplication for multi-application OpenAI routing Oct 15, 2026
@eicherseiji eicherseiji changed the title [Serve][LLM][3/5] Add RouterApplication for multi-application OpenAI routing [Serve][LLM][3/4] Add multi-application OpenAI routing Oct 15, 2026
@eicherseiji
eicherseiji removed this pull request from stack #66518 October 15, 2026 18:58
@eicherseiji
eicherseiji added this pull request to stack #67088 October 15, 2026 18:58

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@eicherseiji eicherseiji changed the title [Serve][LLM][3/4] Add multi-application OpenAI routing [Serve][LLM][3/5] Add multi-application OpenAI routing Oct 20, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Base automatically changed from celina/router-app-2-haproxy-dispatch to master October 21, 2026 18:35
@eicherseiji
eicherseiji requested review from a team, edoakes and richardliaw as code owners October 21, 2026 18:35

@jeffreywang88 jeffreywang88 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.

Do we need a rebase? Looks like github's stack screwed up the rebase.

@eicherseiji

eicherseiji commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Correction: I rebuilt the branch directly on current master and force-pushed it with lease. GitHub now shows a single commit containing only the seven intended files (1,649 additions, 17 deletions).

@jeffreywang88

Copy link
Copy Markdown
Contributor

please double-check that the diffs are as expected before merging. they look quite large now.

@eicherseiji
eicherseiji force-pushed the celina/router-app-3-llm-router-control branch from ad445bc to 87a7f0e Compare October 27, 2026 19:58
@eicherseiji
eicherseiji removed request for a team, edoakes and richardliaw October 27, 2026 19:59
@eicherseiji
eicherseiji enabled stacked auto-merge (squash) October 27, 2026 22:08
@eicherseiji
eicherseiji force-pushed the celina/router-app-3-llm-router-control branch from 87a7f0e to 595a3ce Compare November 2, 2026 21:53
@github-actions
github-actions Bot disabled stacked auto-merge November 2, 2026 21:53

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 595a3ce. Configure here.

return await self.models(request)
return await self.model_data(
unquote(path.removeprefix("/v1/models/")), request
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Trailing slashes 404 model endpoints

Medium Severity

Manual path matching treats /v1/models/ as a model lookup instead of the list endpoint, and a trailing slash on /v1/models/{id}/ is kept in the model id. Clients and proxies that add a trailing slash get 404 InvalidModel instead of the router-owned model catalog.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 595a3ce. Configure here.

api_keys = [api_key] if api_key else []
elif isinstance(api_keys, str):
api_keys = [api_keys]
return tuple(hashlib.sha256(key.encode()).digest() for key in api_keys if key)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Engine config lookup can crash router

Medium Severity

_api_key_hashes calls get_engine_config() for every config at replica startup. That conversion rejects any engine_kwargs key the router’s vLLM build does not recognize, so one model-specific flag can fail the whole router before any request is served.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 595a3ce. Configure here.

@eicherseiji
eicherseiji merged commit dec986c into master Nov 3, 2026
6 checks passed
@eicherseiji
eicherseiji deleted the celina/router-app-3-llm-router-control branch November 3, 2026 00:44
dstrodtman added a commit that referenced this pull request Nov 3, 2026
…67741)

## Why are these changes needed?

The Ray docs build on `master` has failed on every commit since #66519
merged. Sphinx aborts during autosummary setup with `no module named
ray.serve.llm` (for example, [RtD build
4665012](https://app.readthedocs.com/projects/anyscale-ray/builds/4665012/)).
That message masks the real import error:

```
ray/serve/llm/__init__.py
 -> ingress/builder.py           from ...router_application import RouterApplication  (module level, added in #66519)
 -> ingress/router_application.py from ...ingress.router import (...)
 -> ingress/router.py:75         @serve.ingress(router_app)
 -> serve/api.py:446             isinstance(app, (FastAPI, APIRouter))
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
```

The docs build mocks `fastapi` (`doc/source/api_mock_imports.py`), so
`FastAPI` isn't a real type there. `builder.py` already avoids this by
importing `LLMRouter` lazily inside
`_build_openai_ingress_request_router`. The new top-level
`RouterApplication` import pulled `router.py` back in at import time.

This PR moves the `RouterApplication` import into
`build_openai_router_app`, the only place that uses it. #66519 changed
nothing under `doc/`, so its RtD previews were skipped by the doc-change
guard and the break first showed up on `master`. The commit carries an
`@rtd-build` line so this PR builds a preview.

## Duplicate check

I searched open PRs for `RouterApplication`, `serve llm import docs`,
and the #66519 author's open PRs, and found nothing that addresses this
break.

## Testing

- Reproduced the failure by importing `ray.serve.llm` from source with
Sphinx's `sphinx.ext.autodoc.mock.mock()` active over the docs mock
list, in a venv built from `doc/requirements-doc.lock.txt`. The import
fails at `dec986ce` (#66519) with the traceback above, succeeds at its
parent `c2adf94c`, and succeeds with this change.
- I didn't run the Serve LLM unit tests locally. Premerge CI covers
`test_builder_openai_router_app.py` and the router tests, and the RtD
preview on this PR confirms the docs build.

## AI assistance

Claude Code helped diagnose the build failure and draft this change. I
reviewed the change and the analysis.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests llm serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants