Skip to content

[Router] Standardize route_request signature across all routers - #1023

Open
Mohith26 wants to merge 2 commits into
vllm-project:mainfrom
Mohith26:refactor/standardize-route-request-signature
Open

Mohith26 wants to merge 2 commits into
vllm-project:mainfrom
Mohith26:refactor/standardize-route-request-signature

Conversation

@Mohith26

Copy link
Copy Markdown

Implements #1022, proposed by @shernshiou.

route_request was inconsistent across the 6 router implementations (some sync, some async, some without request_json), which forced isinstance dispatch guards at every call site and would break silently whenever a new router is added.

Changes:

  • RoutingInterface.route_request is now async def and takes request_json; all 6 routers share the signature. Sync routers just do not await internally; no routing behavior changed.
  • route_general_request (initial + failover call sites) and proxy_multipart_request always await and always pass request_json; the (KvawareRouter, PrefixAwareRouter, SessionRouter) isinstance guards are gone. The DisaggregatedPrefill* isinstance checks stay because they dispatch to different request handlers, not route_request variants.
  • One deviation from the issue text: request_json: Optional[Dict] = None (normalized inside) instead of the mutable dict = {} default, matching the repo's existing typing style and avoiding the shared-default hazard. Happy to change if you prefer the literal proposal.

Tests: new test_route_request_signature.py (16 tests) checks every router's signature plus behavioral probes proving the call sites really await and forward the body (via a router subclass that is not in any old isinstance whitelist). Existing roundrobin/auth-header tests updated for async. Full suite: 186 passed, 0 failed (baseline on main was 170 passed, 0 failed). black/isort/ruff clean.

Relates to #1019: this standardizes the surface that made that bug possible. PRs #1020/#1021 fix the immediate call-site bug; this PR is complementary and rebases cleanly in either order.

Mohith26 added 2 commits July 28, 2026 13:30
Make RoutingInterface.route_request an async method that always accepts
request_json (Optional[Dict] = None, normalized inside the routers that
use it, to avoid a mutable default argument). All six router
implementations now share the same coroutine signature, so call sites in
route_general_request and proxy_multipart_request can always await
route_request and always pass request_json, removing the fragile
isinstance dispatch guards that broke proxy_multipart_request for
SessionRouter (missing request_json and missing await).

Routing behavior is unchanged: previously-sync routers simply do not
await anything internally.

Fixes vllm-project#1022
Fixes vllm-project#1019

Signed-off-by: Mohith Gajjela <[email protected]>
Add tests asserting that every router's route_request is a coroutine
function with a request_json parameter, that RoundRobinRouter routes
correctly via the awaited path, and that route_general_request and
proxy_multipart_request await route_request and pass request_json for
any RoutingInterface implementation (no isinstance-dependent dispatch).

Relates to vllm-project#1022

Signed-off-by: Mohith Gajjela <[email protected]>

@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 standardizes the route_request method signature across all routers to be asynchronous and accept an optional request_json parameter, which simplifies call-site dispatching by removing isinstance checks. Feedback highlights a potential KeyError in PrefixAwareRouter when request_json is empty (e.g., during multipart requests), and suggests omitting the redundant empty dictionary argument in proxy_multipart_request since the parameter is optional.

Comment on lines +485 to +486
if request_json is None:
request_json = {}

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.

high

If 'request_json' is empty (for example, when passed as '{}' from 'proxy_multipart_request' during audio transcription), 'messages' will not be present. This causes the router to fall through to the 'else' block at line 512: 'prompt = request_json["prompt"]', which will raise a 'KeyError' and crash the request. To prevent this, we should ensure that 'prompt' is initialized to an empty string if neither 'messages' nor 'prompt' is present in 'request_json'.

Suggested change
if request_json is None:
request_json = {}
if request_json is None:
request_json = {}
if "messages" not in request_json and "prompt" not in request_json:
request_json["prompt"] = ""

Comment on lines +1229 to 1235
chosen_url = await router.route_request(
endpoints,
engine_stats,
request_stats,
request,
{}, # multipart/form-data requests carry no JSON body
)

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.

medium

Since 'request_json' is an optional parameter with a default value of 'None' (which is internally normalized to '{}' by the routers), we can omit passing '{}' here to keep the call site cleaner.

    chosen_url = await router.route_request(
        endpoints,
        engine_stats,
        request_stats,
        request,
    )

@ruizhang0101

Copy link
Copy Markdown
Collaborator

Hi, could you resolve the conflict when you have time?

chrikrah added a commit to chrikrah/production-stack that referenced this pull request Nov 16, 2026
Make RoutingInterface.route_request a coroutine with an optional
request_json parameter, and give every router that signature, including
PriorityRouter and LoadAwareRouter, which arrived after the original
branch. Both dispatch sites in request.py then await the router directly:
route_general_request and the failover loop drop their isinstance chains,
and proxy_multipart_request makes one awaited call that passes {}.

The DisaggregatedPrefill* handler dispatch in route_general_request stays,
because those routers go to different request handlers.

Rebased from vllm-project#1023.

Fixes vllm-project#1022

Co-authored-by: Mohith Gajjela <[email protected]>
Signed-off-by: Christopher Krah <[email protected]>

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.

2 participants