[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,6 +2,7 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import asyncio
import io
import json
import time
from collections.abc import AsyncGenerator, AsyncIterator
@@ -9,6 +10,8 @@ from collections.abc import Sequence as GenericSequence
from http import HTTPStatus
from typing import TYPE_CHECKING, Any, Final
import numpy as np
import pybase64 as base64
from fastapi import Request
from vllm.engine.protocol import EngineClient
@@ -1086,6 +1089,18 @@ class OpenAIServingChat(OpenAIServing):
content=content,
)
# 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 = ChatCompletionResponseChoice(
index=output.index,
message=message,
@@ -1101,6 +1116,7 @@ class OpenAIServingChat(OpenAIServing):
token_ids=(
as_list(output.token_ids) if request.return_token_ids else None
),
routed_experts=routed_experts_b64,
)
choices.append(choice_data)
continue
@@ -1309,6 +1325,16 @@ class OpenAIServingChat(OpenAIServing):
and output.finish_reason == "stop"
)
# 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 = ChatCompletionResponseChoice(
index=output.index,
message=message,
@@ -1322,6 +1348,7 @@ class OpenAIServingChat(OpenAIServing):
token_ids=(
as_list(output.token_ids) if request.return_token_ids else None
),
routed_experts=routed_experts_b64,
)
choice_data = maybe_filter_parallel_tool_calls(choice_data, request)