[R3] Add routed experts to openai entrypoint (#38939)

Signed-off-by: ahao-anyscale <ahao@anyscale.com>
Co-authored-by: Isotr0py <mozf@mail2.sysu.edu.cn>
This commit is contained in:
Aaron Hao
2026-05-20 09:07:59 -07:00
committed by GitHub
co-authored by Isotr0py
parent 9c78c99995
commit 2d6b3489b9
6 changed files with 208 additions and 0 deletions
@@ -2,11 +2,14 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import asyncio
import io
import time
from collections.abc import AsyncGenerator, AsyncIterator
from collections.abc import Sequence as GenericSequence
from typing import TYPE_CHECKING, cast
import numpy as np
import pybase64 as base64
from fastapi import Request
from vllm.engine.protocol import EngineClient
@@ -538,6 +541,18 @@ class OpenAIServingCompletion(OpenAIServing):
else:
logprobs = None
# Encode routed_experts for transport. JSON can't carry raw
# bytes, so we write the ndarray as a ``.npy`` byte stream
# and base64-encode it. ``pybase64`` is ~3x faster than the
# stdlib ``base64`` on large payloads thanks to SIMD.
routed_experts_b64 = None
if output.routed_experts is not None:
buf = io.BytesIO()
np.save(buf, output.routed_experts)
routed_experts_b64 = base64.b64encode(buf.getvalue()).decode(
"ascii"
)
choice_data = CompletionResponseChoice(
index=len(choices),
text=output_text,
@@ -551,6 +566,7 @@ class OpenAIServingCompletion(OpenAIServing):
token_ids=(
as_list(output.token_ids) if request.return_token_ids else None
),
routed_experts=routed_experts_b64,
)
choices.append(choice_data)