Skip to content

[serve][llm] Parse direct-streaming routing payload for body-aware routers (#64328) - #64488

Merged
elliot-barn merged 1 commit into
releases/2.56.1from
seiji/parse-direct-streaming-routing-payload-2.56.1
Jul 2, 2026
Merged

elliot-barn merged 1 commit into
releases/2.56.1from
seiji/parse-direct-streaming-routing-payload-2.56.1

Conversation

@eicherseiji

Copy link
Copy Markdown
Contributor

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

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.

Related issue number

Fixes #64326
Cherry-pick of #64328

…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]>
@eicherseiji eicherseiji self-assigned this Jul 1, 2026
@eicherseiji eicherseiji added the go add ONLY when ready to merge, run all tests label Jul 1, 2026
@eicherseiji
eicherseiji marked this pull request as ready for review July 1, 2026 23:41
@eicherseiji
eicherseiji requested a review from a team as a code owner July 1, 2026 23:41

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

@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 using default effort and found 1 potential issue.

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 ab3a91f. Configure here.

@ray-gardener ray-gardener Bot added serve Ray Serve Related Issue llm labels Jul 2, 2026
@elliot-barn
elliot-barn merged commit 00b0930 into releases/2.56.1 Jul 2, 2026
5 checks passed
@elliot-barn
elliot-barn deleted the seiji/parse-direct-streaming-routing-payload-2.56.1 branch July 2, 2026 02:57
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