[Frontend] Make finish_reason and Responses item status truthful (#59324)

Signed-off-by: Flora Feng <4florafeng@gmail.com>
Signed-off-by: sfeng33 <4florafeng@gmail.com>
Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
This commit is contained in:
Flora Feng
2026-10-15 10:12:02 +01:00
committed by GitHub
co-authored by Chauncey
parent 427915bed2
commit 80dd705c1d
14 changed files with 421 additions and 62 deletions
@@ -3,11 +3,18 @@
"""Tests for the /derender endpoints (postprocessing counterpart to /render)."""
from unittest.mock import MagicMock
import httpx
import pytest
import pytest_asyncio
from tests.utils import RemoteLaunchRenderServer
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
from vllm.entrypoints.scale_out.token_in_token_out.protocol import (
GenerateTokensResponse,
)
from vllm.renderers.online_derenderer import OnlineDerenderer
from vllm.tokenizers import get_tokenizer
MODEL_NAME = "hmellor/tiny-random-LlamaForCausalLM"
@@ -1432,3 +1439,55 @@ async def test_e2e_harmony_reasoning(harmony_client, harmony_tokenizer):
assert msg["reasoning"] is not None
assert reasoning_text in msg["reasoning"]
assert answer_text in (msg["content"] or "")
_HERMES_TOOL_CALL_TEXT = (
'<tool_call>\n{"name": "get_weather", "arguments": {"city": "Paris"}}\n</tool_call>'
)
@pytest.mark.parametrize(
("tool_choice", "text", "engine_finish_reason", "expected_finish_reason"),
[
("auto", _HERMES_TOOL_CALL_TEXT, "stop", "tool_calls"),
("auto", _HERMES_TOOL_CALL_TEXT, "length", "length"),
("required", "Hello there", "stop", "stop"),
],
)
def test_derender_chat_finish_reason_matches_chat_completion(
tool_choice, text, engine_finish_reason, expected_finish_reason
):
"""/derender applies the same finish_reason rule as the coupled chat
path: ``tool_calls`` only for a real call at a natural stop."""
tokenizer = get_tokenizer(MODEL_NAME)
renderer = MagicMock()
renderer.get_tokenizer.return_value = tokenizer
model_config = MagicMock()
model_config.model = MODEL_NAME
model_config.hf_config.model_type = "llama"
derenderer = OnlineDerenderer(
model_config,
renderer,
request_logger=None,
chat_template="{{ messages }}",
chat_template_content_format="auto",
enable_auto_tools=True,
tool_parser="hermes",
)
request = ChatCompletionRequest(
model=MODEL_NAME,
messages=[{"role": "user", "content": "Weather in Paris?"}],
tools=_E2E_TOOLS,
tool_choice=tool_choice,
)
response = GenerateTokensResponse.model_validate(
_make_generate_response(
tokenizer.encode(text, add_special_tokens=False),
finish_reason=engine_finish_reason,
)
)
(choice,) = derenderer._derender_chat(response, request)
assert choice.finish_reason == expected_finish_reason
assert bool(choice.message.tool_calls) == (text == _HERMES_TOOL_CALL_TEXT)
@@ -1289,7 +1289,15 @@ class TestDerenderChatStreamParsed:
assert state.last_tool_call_ids == [first_id]
@pytest.mark.asyncio
async def test_finish_reason_rewritten_to_tool_calls(self, parsed_derenderer):
@pytest.mark.parametrize(
("engine_finish_reason", "expected_finish_reason"),
[("stop", "tool_calls"), ("length", "length")],
)
async def test_finish_reason_rewritten_to_tool_calls(
self, parsed_derenderer, engine_finish_reason, expected_finish_reason
):
"""A streamed tool call is reported as ``tool_calls`` only at a
natural stop; a ``length`` cut must stay visible."""
chat_request = _chat_request(
tools=[{"type": "function", "function": {"name": "get_weather"}}],
tool_choice="auto",
@@ -1297,11 +1305,12 @@ class TestDerenderChatStreamParsed:
chunk, _ = await parsed_derenderer.derender_chat_stream(
model=MODEL_NAME,
generate_chunk=_make_stream_chunk(
[_FakeParser.TOOL_START, _FakeParser.TOOL_ARG], finish_reason="stop"
[_FakeParser.TOOL_START, _FakeParser.TOOL_ARG],
finish_reason=engine_finish_reason,
),
chat_request=chat_request,
)
assert chunk.choices[0].finish_reason == "tool_calls"
assert chunk.choices[0].finish_reason == expected_finish_reason
@pytest.mark.asyncio
async def test_finish_reason_stop_for_named_tool_choice(self, parsed_derenderer):