Repository navigation
[Serve][LLM][3/5] Add multi-application OpenAI routing - #66519
Conversation
ee71ee8 to
dfcacc8
Compare
dfcacc8 to
977dfee
Compare
977dfee to
e5db483
Compare
There was a problem hiding this comment.
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.
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
jeffreywang88
left a comment
There was a problem hiding this comment.
Do we need a rebase? Looks like github's stack screwed up the rebase.
|
Correction: I rebuilt the branch directly on current |
|
please double-check that the diffs are as expected before merging. they look quite large now. |
ad445bc to
87a7f0e
Compare
Signed-off-by: Seiji Eicher <[email protected]>
87a7f0e to
595a3ce
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ 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 | ||
| ) |
There was a problem hiding this comment.
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.
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) |
There was a problem hiding this comment.
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.
Reviewed by Cursor Bugbot for commit 595a3ce. Configure here.
…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]>


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
The builder is private, requires HAProxy and LLM direct streaming, and accepts the same dictionary through declarative YAML. It requires exactly one
LLMConfigfor every model-to-application mapping. Updating this router does not replace or delete the independently deployed model applications.Design
RouterApplicationimplements the privatePOST /internal/routeprotocol 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:
GETandHEAD /v1/modelsand/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
KVAwareRouterbehavior in the shared router process.Test plan
test_router_application.pycovers model parsing, authentication, handle lookup, selection, session affinity, errors, and timeouts.test_builder_openai_router_app.pycovers builder validation, declarative arguments, and router-role propagation.test_openai_router_app_direct_streaming.pycovers the end-to-end multi-application path and native OpenAI routes.