[Frontend] Report stop_reason on /inference/v1/generate and pass it through derender (#60251)

Signed-off-by: Hyeonki Hong <hhk7734@gmail.com>
Signed-off-by: Shimi Bandiel <shimib@google.com>
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
Co-authored-by: Hyeonki Hong <hhk7734@gmail.com>
Co-authored-by: Mimir <mimir@nousresearch.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
Shimi Bandiel
2026-10-12 19:37:53 +08:00
committed by GitHub
co-authored by Hyeonki Hong Mimir Claude Bugen Zhao
parent 32b05addc1
commit 8d7b93fec2
8 changed files with 201 additions and 2 deletions
@@ -139,6 +139,7 @@ def _make_request_output(
request_id: str,
token_ids: list[int],
finish_reason: str | None = None,
stop_reason: int | str | None = None,
finished: bool = False,
prompt_token_ids: list[int] | None = None,
logprobs: list[dict[int, Any] | None] | None = None,
@@ -160,6 +161,7 @@ def _make_request_output(
cumulative_logprob=None,
logprobs=logprobs,
finish_reason=finish_reason,
stop_reason=stop_reason,
spec_decode_metrics=spec_decode_metrics,
)
],
@@ -378,6 +380,71 @@ async def test_stream_basic():
assert data_chunks[2]["choices"][0]["finish_reason"] == "stop"
@pytest.mark.asyncio
async def test_non_stream_reports_engine_stop_reason():
"""The matched stop token id from the engine lands on the choice."""
engine = _mock_engine()
async def mock_generate(*args, **kwargs):
yield _make_request_output(
"req-1",
token_ids=[10, 42],
finish_reason="stop",
stop_reason=42,
finished=True,
)
engine.generate = MagicMock(side_effect=mock_generate)
serving = _build_serving_tokens(engine)
request = GenerateRequest(
token_ids=[1, 2, 3],
sampling_params=SamplingParams(max_tokens=10),
model=MODEL_NAME,
stream=False,
)
response = await serving.serve_tokens(request)
choice = response.model_dump()["choices"][0]
assert choice["finish_reason"] == "stop"
assert choice["stop_reason"] == 42
@pytest.mark.asyncio
async def test_stream_reports_engine_stop_reason_on_final_chunk():
"""A matched stop string is reported with finish_reason and null before."""
engine = _mock_engine()
async def mock_generate(*args, **kwargs):
yield _make_request_output("req-1", token_ids=[10])
yield _make_request_output(
"req-1",
token_ids=[20],
finish_reason="stop",
stop_reason="done",
finished=True,
)
engine.generate = MagicMock(side_effect=mock_generate)
serving = _build_serving_tokens(engine)
request = GenerateRequest(
token_ids=[1, 2, 3],
sampling_params=SamplingParams(max_tokens=10),
model=MODEL_NAME,
stream=True,
)
response = await serving.serve_tokens(request)
parsed = _parse_sse_chunks([chunk async for chunk in response])
data_chunks = [c for c in parsed if c != "[DONE]"]
assert len(data_chunks) == 2
assert data_chunks[0]["choices"][0]["stop_reason"] is None
assert data_chunks[1]["choices"][0]["finish_reason"] == "stop"
assert data_chunks[1]["choices"][0]["stop_reason"] == "done"
@pytest.mark.asyncio
@pytest.mark.parametrize("stream", [False, True])
async def test_serve_tokens_returns_spec_decode_metrics(stream: bool):