[Refactor] Deprecate ResponsesParser wrapper, inline parsing into ParsableContext (#45431)
Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
+72
-99
@@ -1,10 +1,9 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Unit tests for ResponsesParser with the unified Parser interface.
|
||||
"""Unit tests for ParsableContext's parsing behavior.
|
||||
|
||||
These tests verify that ResponsesParser correctly delegates to the unified
|
||||
Parser (via parse) instead of calling separate ReasoningParser / ToolParser
|
||||
instances directly.
|
||||
These tests verify that ParsableContext correctly delegates to the unified
|
||||
Parser (via parse) and properly builds response output items.
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
@@ -18,12 +17,9 @@ from vllm.entrypoints.openai.engine.protocol import (
|
||||
FunctionCall,
|
||||
ToolCall,
|
||||
)
|
||||
from vllm.entrypoints.openai.parser.responses_parser import (
|
||||
ResponsesParser,
|
||||
get_responses_parser_for_simple_context,
|
||||
)
|
||||
from vllm.entrypoints.openai.responses.context import ParsableContext
|
||||
from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
|
||||
from vllm.outputs import CompletionOutput
|
||||
from vllm.outputs import CompletionOutput, RequestOutput
|
||||
from vllm.parser.abstract_parser import DelegatingParser
|
||||
|
||||
pytestmark = pytest.mark.skip_global_cleanup
|
||||
@@ -162,32 +158,42 @@ def _make_request(**overrides) -> ResponsesRequest:
|
||||
return ResponsesRequest.model_validate(defaults)
|
||||
|
||||
|
||||
def _make_output(
|
||||
def _make_request_output(
|
||||
text: str = "Hello, world!",
|
||||
token_ids: Sequence[int] = (1, 2, 3),
|
||||
finish_reason: str = "stop",
|
||||
) -> CompletionOutput:
|
||||
return CompletionOutput(
|
||||
index=0,
|
||||
text=text,
|
||||
token_ids=list(token_ids),
|
||||
cumulative_logprob=None,
|
||||
logprobs=None,
|
||||
finish_reason=finish_reason,
|
||||
) -> RequestOutput:
|
||||
return RequestOutput(
|
||||
request_id="test",
|
||||
prompt=None,
|
||||
prompt_token_ids=[],
|
||||
prompt_logprobs=None,
|
||||
outputs=[
|
||||
CompletionOutput(
|
||||
index=0,
|
||||
text=text,
|
||||
token_ids=list(token_ids),
|
||||
cumulative_logprob=None,
|
||||
logprobs=None,
|
||||
finish_reason=finish_reason,
|
||||
)
|
||||
],
|
||||
finished=True,
|
||||
)
|
||||
|
||||
|
||||
def _make_parser(parser_cls, **overrides):
|
||||
def _make_context(parser_cls, **overrides):
|
||||
defaults = dict(
|
||||
tokenizer=MagicMock(),
|
||||
parser_cls=parser_cls,
|
||||
response_messages=[],
|
||||
request=_make_request(),
|
||||
available_tools=None,
|
||||
chat_template=None,
|
||||
chat_template_content_format="auto",
|
||||
)
|
||||
defaults.update(overrides)
|
||||
return ResponsesParser(**defaults)
|
||||
return ParsableContext(**defaults)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -197,22 +203,22 @@ def _make_parser(parser_cls, **overrides):
|
||||
|
||||
def test_process_text_with_parser():
|
||||
"""Parser with no reasoning/tools returns a single message item."""
|
||||
parser = _make_parser(_NoOpParser)
|
||||
parser.process(_make_output(text="Hello!"))
|
||||
ctx = _make_context(_NoOpParser)
|
||||
ctx.append_output(_make_request_output(text="Hello!"))
|
||||
|
||||
assert len(parser.response_messages) == 1
|
||||
msg = parser.response_messages[0]
|
||||
assert len(ctx.response_messages) == 1
|
||||
msg = ctx.response_messages[0]
|
||||
assert msg.type == "message"
|
||||
assert msg.content[0].text == "Hello!"
|
||||
|
||||
|
||||
def test_process_text_without_parser():
|
||||
"""parser_cls=None falls back to plain text wrapping."""
|
||||
parser = _make_parser(None)
|
||||
parser.process(_make_output(text="Hello!"))
|
||||
ctx = _make_context(None)
|
||||
ctx.append_output(_make_request_output(text="Hello!"))
|
||||
|
||||
assert len(parser.response_messages) == 1
|
||||
msg = parser.response_messages[0]
|
||||
assert len(ctx.response_messages) == 1
|
||||
msg = ctx.response_messages[0]
|
||||
assert msg.type == "message"
|
||||
assert msg.content[0].text == "Hello!"
|
||||
|
||||
@@ -224,18 +230,18 @@ def test_process_text_without_parser():
|
||||
|
||||
def test_process_empty_text_without_parser():
|
||||
"""Empty text with no parser produces no output items."""
|
||||
parser = _make_parser(None)
|
||||
parser.process(_make_output(text=""))
|
||||
ctx = _make_context(None)
|
||||
ctx.append_output(_make_request_output(text=""))
|
||||
|
||||
assert len(parser.response_messages) == 0
|
||||
assert len(ctx.response_messages) == 0
|
||||
|
||||
|
||||
def test_process_empty_text_with_parser():
|
||||
"""Empty text with parser produces no output items."""
|
||||
parser = _make_parser(_NoOpParser)
|
||||
parser.process(_make_output(text=""))
|
||||
ctx = _make_context(_NoOpParser)
|
||||
ctx.append_output(_make_request_output(text=""))
|
||||
|
||||
assert len(parser.response_messages) == 0
|
||||
assert len(ctx.response_messages) == 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -245,26 +251,28 @@ def test_process_empty_text_with_parser():
|
||||
|
||||
def test_process_extracts_reasoning():
|
||||
"""Parser that finds reasoning produces both reasoning and message items."""
|
||||
parser = _make_parser(_ReasoningOnlyParser)
|
||||
parser.process(_make_output(text="<think>Let me check</think>The answer is 42"))
|
||||
ctx = _make_context(_ReasoningOnlyParser)
|
||||
ctx.append_output(
|
||||
_make_request_output(text="<think>Let me check</think>The answer is 42")
|
||||
)
|
||||
|
||||
types = [m.type for m in parser.response_messages]
|
||||
types = [m.type for m in ctx.response_messages]
|
||||
assert "reasoning" in types
|
||||
assert "message" in types
|
||||
|
||||
reasoning_item = next(m for m in parser.response_messages if m.type == "reasoning")
|
||||
reasoning_item = next(m for m in ctx.response_messages if m.type == "reasoning")
|
||||
assert reasoning_item.content[0].text == "Let me check"
|
||||
|
||||
message_item = next(m for m in parser.response_messages if m.type == "message")
|
||||
message_item = next(m for m in ctx.response_messages if m.type == "message")
|
||||
assert message_item.content[0].text == "The answer is 42"
|
||||
|
||||
|
||||
def test_process_reasoning_only_no_content():
|
||||
"""When reasoning consumes all text, only a reasoning item is produced."""
|
||||
parser = _make_parser(_ReasoningOnlyParser)
|
||||
parser.process(_make_output(text="<think>Just thinking</think>"))
|
||||
ctx = _make_context(_ReasoningOnlyParser)
|
||||
ctx.append_output(_make_request_output(text="<think>Just thinking</think>"))
|
||||
|
||||
types = [m.type for m in parser.response_messages]
|
||||
types = [m.type for m in ctx.response_messages]
|
||||
assert "reasoning" in types
|
||||
assert "message" not in types
|
||||
|
||||
@@ -286,13 +294,13 @@ def test_process_extracts_tool_calls():
|
||||
}
|
||||
],
|
||||
)
|
||||
parser = _make_parser(_ToolCallingParser, request=request, enable_auto_tools=True)
|
||||
parser.process(_make_output(text="calling tool"))
|
||||
ctx = _make_context(_ToolCallingParser, request=request, enable_auto_tools=True)
|
||||
ctx.append_output(_make_request_output(text="calling tool"))
|
||||
|
||||
types = [m.type for m in parser.response_messages]
|
||||
types = [m.type for m in ctx.response_messages]
|
||||
assert "function_call" in types
|
||||
|
||||
tool_item = next(m for m in parser.response_messages if m.type == "function_call")
|
||||
tool_item = next(m for m in ctx.response_messages if m.type == "function_call")
|
||||
assert tool_item.name == "get_weather"
|
||||
assert tool_item.arguments == '{"location": "Paris"}'
|
||||
assert tool_item.status == "completed"
|
||||
@@ -304,15 +312,15 @@ def test_process_extracts_tool_calls():
|
||||
|
||||
|
||||
def test_finish_reason_tracked():
|
||||
"""finish_reason from CompletionOutput is stored on the parser."""
|
||||
parser = _make_parser(_NoOpParser)
|
||||
assert parser.finish_reason is None
|
||||
"""finish_reason from CompletionOutput is stored on the context."""
|
||||
ctx = _make_context(_NoOpParser)
|
||||
assert ctx.finish_reason is None
|
||||
|
||||
parser.process(_make_output(finish_reason="stop"))
|
||||
assert parser.finish_reason == "stop"
|
||||
ctx.append_output(_make_request_output(finish_reason="stop"))
|
||||
assert ctx.finish_reason == "stop"
|
||||
|
||||
parser.process(_make_output(finish_reason="length"))
|
||||
assert parser.finish_reason == "length"
|
||||
ctx.append_output(_make_request_output(finish_reason="length"))
|
||||
assert ctx.finish_reason == "length"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -321,62 +329,27 @@ def test_finish_reason_tracked():
|
||||
|
||||
|
||||
def test_multi_turn_accumulation():
|
||||
"""Multiple process() calls accumulate response_messages."""
|
||||
parser = _make_parser(_NoOpParser)
|
||||
"""Multiple append_output() calls accumulate response_messages."""
|
||||
ctx = _make_context(_NoOpParser)
|
||||
|
||||
parser.process(_make_output(text="First turn"))
|
||||
parser.process(_make_output(text="Second turn"))
|
||||
ctx.append_output(_make_request_output(text="First turn"))
|
||||
ctx.append_output(_make_request_output(text="Second turn"))
|
||||
|
||||
assert len(parser.response_messages) == 2
|
||||
texts = [m.content[0].text for m in parser.response_messages]
|
||||
assert len(ctx.response_messages) == 2
|
||||
texts = [m.content[0].text for m in ctx.response_messages]
|
||||
assert texts == ["First turn", "Second turn"]
|
||||
|
||||
|
||||
def test_num_init_messages_offset():
|
||||
"""Initial messages are preserved and offset works correctly."""
|
||||
init_messages = [MagicMock(type="message")]
|
||||
parser = _make_parser(_NoOpParser, response_messages=init_messages)
|
||||
ctx = _make_context(_NoOpParser, response_messages=init_messages)
|
||||
|
||||
assert parser.num_init_messages == 1
|
||||
assert ctx.num_init_messages == 1
|
||||
|
||||
parser.process(_make_output(text="New output"))
|
||||
ctx.append_output(_make_request_output(text="New output"))
|
||||
|
||||
assert len(parser.response_messages) == 2
|
||||
items = parser.make_response_output_items_from_parsable_context()
|
||||
assert len(ctx.response_messages) == 2
|
||||
items = ctx.make_response_output_items()
|
||||
assert len(items) == 1
|
||||
assert items[0].type == "message"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tests: factory function
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_factory_function_creates_parser():
|
||||
"""get_responses_parser_for_simple_context returns a working parser."""
|
||||
rp = get_responses_parser_for_simple_context(
|
||||
tokenizer=MagicMock(),
|
||||
parser_cls=_NoOpParser,
|
||||
response_messages=[],
|
||||
request=_make_request(),
|
||||
chat_template=None,
|
||||
chat_template_content_format="auto",
|
||||
)
|
||||
assert isinstance(rp, ResponsesParser)
|
||||
|
||||
rp.process(_make_output(text="Works!"))
|
||||
assert len(rp.response_messages) == 1
|
||||
|
||||
|
||||
def test_factory_function_none_parser():
|
||||
"""Factory function works with parser_cls=None."""
|
||||
rp = get_responses_parser_for_simple_context(
|
||||
tokenizer=MagicMock(),
|
||||
parser_cls=None,
|
||||
response_messages=[],
|
||||
request=_make_request(),
|
||||
chat_template=None,
|
||||
chat_template_content_format="auto",
|
||||
)
|
||||
assert isinstance(rp, ResponsesParser)
|
||||
assert rp.parser_instance is None
|
||||
Reference in New Issue
Block a user