Repository navigation
[serve][llm] Parse direct-streaming routing payload for body-aware routers (#64328) - #64488
Conversation
…uters (#64328) ## Why Serve LLM direct streaming breaks with a content-based router such as `PrefixCacheAffinityRouter`. The trigger is `RAY_SERVE_LLM_ENABLE_DIRECT_STREAMING=1` with `RAY_SERVE_ENABLE_HA_PROXY=1`. See #64326. The router reads `.messages` or `.prompt` off the first positional routing arg, where the OpenAI ingress puts the parsed request. Direct streaming forwards the raw body bytes to `choose_replica` instead, so the router gets no routing key. Requests either hang or silently fall back to load-balancing. Introduced by #63280. It routed direct streaming through `choose_replica` but passed the body as a `request_body=` kwarg rather than a parsable positional arg. ## What Normalize the body at the ingress so the router contract is the same on both paths. - Add `_parse_routing_payload(body)`. It `json.loads` the body and wraps it in a `SimpleNamespace` over every field, gated by a frozen tuple of routing-key fields (`messages`, `prompt`). It returns `None` for an empty, non-object, unparseable, or keyless body. - `route()` passes the namespace to `choose_replica` positionally, where body-aware routers already look. With no key, nothing is forwarded and the router load-balances. - Warn once per replica when no key is derived. `PrefixCacheAffinityRouter` is unchanged. ### Why a namespace over the whole body The router contract is that body-aware routers scan `pending_request.args` for an object exposing a routing field by attribute. The namespace mirrors that at `args[0]`, the same slot and access the normal ingress uses, so the direct path is indistinguishable from the normal path to any router, and custom routers that read request fields keep working unchanged. The discarded alternatives: - A plain dict fails attribute access, since `hasattr(some_dict, "messages")` is `False`, so every attribute-reading router would break. - A routing field on the request metadata would bypass routers that scan `args`. - Reconstructing the typed `ChatCompletionRequest` / `CompletionRequest` works only for the two endpoints we enumerate, and the body alone can't identify the type for the others (`messages` is shared by chat-completion, chat-embedding, and tokenize). The namespace carries every field, so it serves any request type that has a routing key and exposes the rest to custom routers, with no endpoint lookup and no per-type branching. Bodies without a key degrade to load-balancing. ## Tests `test_router.py` covers `_parse_routing_payload`, a contract test through the real `PrefixCacheAffinityRouter._extract_text_from_request`, and `route()` and `_pick_replica` forwarding plus the warn-once path. A release test runs the prefix-cache-aware router with direct streaming end to end. ## Related issue number Fixes #64326 Introduced by #63280 ## Checks - [x] I've signed off every commit (`-s`) - [x] I've run `scripts/format.sh` to lint the changes in this PR - [ ] Testing done by reviewers / CI --------- Signed-off-by: Seiji Eicher <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request updates the direct streaming router to parse the request body into a SimpleNamespace routing payload, which is then passed positionally to choose_replica. This enables body-aware routers, such as the PrefixCacheAffinityRouter, to function correctly on the direct-streaming path. Additionally, corresponding unit tests and a nightly release test configuration are added. The reviewer identified a potential crash (TypeError) if the request payload contains the key "dict" during SimpleNamespace instantiation, and suggested wrapping it in a try...except block to gracefully fall back to load-balancing.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ 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 ab3a91f. Configure here.

Cherry-pick of #64328 into
releases/2.56.1.Why
Serve LLM direct streaming breaks with a content-based router such as
PrefixCacheAffinityRouter. The trigger isRAY_SERVE_LLM_ENABLE_DIRECT_STREAMING=1withRAY_SERVE_ENABLE_HA_PROXY=1. See #64326.The router reads
.messagesor.promptoff the first positional routing arg, where the OpenAI ingress puts the parsed request. Direct streaming forwards the raw body bytes tochoose_replicainstead, so the router gets no routing key. Requests either hang or silently fall back to load-balancing.What
Normalize the body at the ingress so the router contract is the same on both paths.
_parse_routing_payload(body). Itjson.loadsthe body and wraps it in aSimpleNamespaceover every field, gated by a frozen tuple of routing-key fields (messages,prompt). It returnsNonefor an empty, non-object, unparseable, or keyless body.route()passes the namespace tochoose_replicapositionally, where body-aware routers already look. With no key, nothing is forwarded and the router load-balances.PrefixCacheAffinityRouteris unchanged.Related issue number
Fixes #64326
Cherry-pick of #64328