Repository navigation
fix: await router.route_request and pass request_json in proxy_multipart_request [FaaFyfxR9WAQrL7FcAgEHJvztd8cVMxvjHRS55rw1nwH] - #1020
Conversation
…er/KvawareRouter/PrefixAwareRouter in proxy_multipart_request
There was a problem hiding this comment.
Code Review
This pull request updates proxy_multipart_request to support asynchronous routing for KvawareRouter, PrefixAwareRouter, and SessionRouter. However, a critical issue was identified where passing an empty dictionary as the request JSON will cause a KeyError in PrefixAwareRouter because it attempts to access the "prompt" key.
| if isinstance(router, (KvawareRouter, PrefixAwareRouter, SessionRouter)): | ||
| chosen_url = await router.route_request( | ||
| endpoints, | ||
| engine_stats, | ||
| request_stats, | ||
| request, | ||
| {}, | ||
| ) | ||
| else: | ||
| chosen_url = router.route_request( | ||
| endpoints, | ||
| engine_stats, | ||
| request_stats, | ||
| request, | ||
| ) |
There was a problem hiding this comment.
There are two issues with the current implementation of proxy_multipart_request when routing requests:
- KeyError in
PrefixAwareRouter: Passing an empty dictionary{}asrequest_jsonwill cause aKeyErrorinPrefixAwareRouter.route_request(line 500) because it attempts to accessrequest_json["prompt"]when `
|
Duplicate with #1021 |
ErenAta16
left a comment
There was a problem hiding this comment.
Confirmed the crash rather than inferring it from the signature. Reproducing the call exactly as proxy_multipart_request makes it on main:
SessionRouter.route_request signature:
(self, endpoints, engine_stats, request_stats, request, request_json) -> str
is coroutine function: True
sr.route_request(endpoints, engine_stats, request_stats, request)
TypeError: SessionRouter.route_request() missing 1 required positional argument: 'request_json'
So it fails at the call, before the missing await even becomes relevant. Every multipart request, audio transcription and image edit, is a hard 500 whenever the router is one of the three async ones. RoundRobinRouter.route_request is not a coroutine and takes four arguments, which is why the plain path never noticed.
The isinstance dispatch matches what services/request_service/request.py already does around line 561, so this keeps the two call sites in step rather than inventing a second convention. That is the right call, though it does mean the tuple (KvawareRouter, PrefixAwareRouter, SessionRouter) now appears in two places and will need updating in both when a fourth async router lands. A small helper, or an is_async flag on RoutingInterface, would remove that trap. Not a blocker, and probably better as a follow-up than bundled here.
One behavioural consequence worth writing down. Passing {} is correct in the sense that extract_session_id calls request_json.get(...) and would raise on None, so it has to be a mapping. But it means session extraction on these endpoints is header-only:
val = request.headers.get(session_key)
return val if val is not None else request_json.get(session_key, None)For a JSON endpoint the body is the fallback. For multipart there is no JSON body to read, so {} is honest, but a client that puts its session id in a multipart form field rather than a header will now silently get QPS routing instead of session affinity. Silently, because a missing session id is a valid state that falls through to _qps_routing, not an error.
If transcription is expected to participate in session affinity at all, parsing the session key out of the form data and passing {session_key: value} would close that. If it is not expected to, a line in the docstring saying multipart requests are header-only would save someone the debugging session. Either way I would not hold the PR for it, since header-only affinity is strictly better than a 500.
Fix looks right to me.
Fixes #1019
What does this PR do?
Fixes a bug in
proxy_multipart_requestwhereSessionRouter.route_request()was called without the requiredrequest_jsonargument, and withoutawait.Root Cause
The
proxy_multipart_requestfunction (used for audio transcription and image edit requests) calledrouter.route_request()synchronously withoutrequest_json:However,
SessionRouter,KvawareRouter, andPrefixAwareRouterall have an asyncroute_requestmethod that requires arequest_jsonargument. The regularroute_general_requestfunction already handles this correctly with anisinstanceguard.Fix
Mirrors the pattern used in
route_general_request:SessionRouter,KvawareRouter, orPrefixAwareRouter, callawait router.route_request(...)with{}asrequest_json(multipart/form-data requests have no JSON body).router.route_request(...)synchronously withoutrequest_json.Testing
route_general_request(lines 561-570), which is already well-tested./v1/audio/transcriptions,/v1/images/edits) when using session/kv-aware/prefix-aware routing.roundrobinrouting is unaffected.