From 3a0406170105b8710b3754d87bddc2a7cfd81d31 Mon Sep 17 00:00:00 2001 From: Flora Feng <4florafeng@gmail.com> Date: Thu, 11 Jun 2026 00:37:51 -0400 Subject: [PATCH] [Refactor][Parser] Unify Response API to use parser.parse() like Chat Completion API (#45190) Signed-off-by: sfeng33 <4florafeng@gmail.com> --- .../openai/test_responses_parser_unified.py | 4 +- .../openai/test_tool_choice_content_none.py | 4 +- .../openai/parser/responses_parser.py | 13 +- vllm/entrypoints/openai/responses/serving.py | 17 +- vllm/entrypoints/openai/responses/utils.py | 79 ++++++- vllm/parser/abstract_parser.py | 195 +----------------- 6 files changed, 101 insertions(+), 211 deletions(-) diff --git a/tests/entrypoints/openai/test_responses_parser_unified.py b/tests/entrypoints/openai/test_responses_parser_unified.py index ecc857e1aa..231ccf34fc 100644 --- a/tests/entrypoints/openai/test_responses_parser_unified.py +++ b/tests/entrypoints/openai/test_responses_parser_unified.py @@ -3,8 +3,8 @@ """Unit tests for ResponsesParser with the unified Parser interface. These tests verify that ResponsesParser correctly delegates to the unified -Parser (via extract_response_outputs) instead of calling separate -ReasoningParser / ToolParser instances directly. +Parser (via parse) instead of calling separate ReasoningParser / ToolParser +instances directly. """ from collections.abc import Sequence diff --git a/tests/entrypoints/openai/test_tool_choice_content_none.py b/tests/entrypoints/openai/test_tool_choice_content_none.py index 75a5c578cc..ec66ff3ad4 100644 --- a/tests/entrypoints/openai/test_tool_choice_content_none.py +++ b/tests/entrypoints/openai/test_tool_choice_content_none.py @@ -78,9 +78,9 @@ def test_responses_parser_allows_named_tool_choice_with_none_content(): ) parser = _DummyDelegatingParser(tokenizer=None) - tool_calls, content = parser._parse_tool_calls( - request=request, + tool_calls, content = parser._extract_tool_calls( content=None, + request=request, enable_auto_tools=False, ) diff --git a/vllm/entrypoints/openai/parser/responses_parser.py b/vllm/entrypoints/openai/parser/responses_parser.py index 1a3048b8d4..810019a053 100644 --- a/vllm/entrypoints/openai/parser/responses_parser.py +++ b/vllm/entrypoints/openai/parser/responses_parser.py @@ -16,6 +16,7 @@ from vllm.entrypoints.openai.responses.protocol import ( ResponseInputOutputItem, ResponsesRequest, ) +from vllm.entrypoints.openai.responses.utils import build_response_output_items from vllm.entrypoints.serve.utils.constants import MCP_PREFIX from vllm.outputs import CompletionOutput from vllm.parser.abstract_parser import Parser @@ -73,11 +74,15 @@ class ResponsesParser: self.finish_reason = output.finish_reason if self.parser_instance is not None: - output_items = self.parser_instance.extract_response_outputs( - model_output=output.text, - model_output_token_ids=output.token_ids, - request=self.request, + reasoning, content, tool_calls = self.parser_instance.parse( + output.text, + self.request, enable_auto_tools=self.enable_auto_tools, + ) + output_items = build_response_output_items( + reasoning=reasoning, + content=content, + tool_calls=tool_calls, tool_call_id_type=self.tool_call_id_type, ) self.response_messages.extend(output_items) diff --git a/vllm/entrypoints/openai/responses/serving.py b/vllm/entrypoints/openai/responses/serving.py index 7ae57ac357..51831f6083 100644 --- a/vllm/entrypoints/openai/responses/serving.py +++ b/vllm/entrypoints/openai/responses/serving.py @@ -86,6 +86,7 @@ from vllm.entrypoints.openai.responses.streaming_events import ( split_delta, ) from vllm.entrypoints.openai.responses.utils import ( + build_response_output_items, construct_input_messages, construct_tool_dicts, extract_function_tool_names, @@ -1028,19 +1029,23 @@ class OpenAIServingResponses(OpenAIServing): top_logprobs=request.top_logprobs, ) - # Use parser to extract and create response output items + # Use parser to extract reasoning, content, and tool calls if self.parser: chat_template_kwargs = self._effective_chat_template_kwargs(request) parser = self.parser( tokenizer, request.tools, chat_template_kwargs=chat_template_kwargs ) - return parser.extract_response_outputs( - model_output=final_output.text, - model_output_token_ids=final_output.token_ids, - request=request, + reasoning, content, tool_calls = parser.parse( + final_output.text, + request, enable_auto_tools=self.enable_auto_tools, - tool_call_id_type=self.tool_call_id_type, + ) + return build_response_output_items( + reasoning=reasoning, + content=content, + tool_calls=tool_calls, logprobs=logprobs, + tool_call_id_type=self.tool_call_id_type, ) # Fallback when no parser is configured diff --git a/vllm/entrypoints/openai/responses/utils.py b/vllm/entrypoints/openai/responses/utils.py index 9556867f5c..81f60b0663 100644 --- a/vllm/entrypoints/openai/responses/utils.py +++ b/vllm/entrypoints/openai/responses/utils.py @@ -12,23 +12,96 @@ from openai.types.chat import ( from openai.types.chat.chat_completion_message_tool_call_param import ( Function as FunctionCallTool, ) -from openai.types.responses import ResponseFunctionToolCall, ResponseOutputItem +from openai.types.responses import ( + ResponseFunctionToolCall, + ResponseOutputItem, + ResponseOutputMessage, + ResponseOutputText, + ResponseReasoningItem, +) from openai.types.responses.response import ToolChoice from openai.types.responses.response_function_tool_call_output_item import ( ResponseFunctionToolCallOutputItem, ) -from openai.types.responses.response_output_message import ResponseOutputMessage -from openai.types.responses.response_reasoning_item import ResponseReasoningItem +from openai.types.responses.response_output_text import Logprob +from openai.types.responses.response_reasoning_item import ( + Content as ResponseReasoningTextContent, +) from openai.types.responses.tool import Tool from vllm import envs +from vllm.entrypoints.chat_utils import make_tool_call_id from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionMessageParam +from vllm.entrypoints.openai.engine.protocol import FunctionCall from vllm.entrypoints.openai.responses.protocol import ResponseInputOutputItem from vllm.logger import init_logger +from vllm.utils import random_uuid logger = init_logger(__name__) +def build_response_output_items( + reasoning: str | None, + content: str | None, + tool_calls: list[FunctionCall] | None, + logprobs: list[Logprob] | None = None, + tool_call_id_type: str = "random", +) -> list[ResponseOutputItem]: + outputs: list[ResponseOutputItem] = [] + + if reasoning: + outputs.append( + ResponseReasoningItem( + id=f"rs_{random_uuid()}", + summary=[], + type="reasoning", + content=[ + ResponseReasoningTextContent(text=reasoning, type="reasoning_text") + ], + status=None, + ) + ) + + if content: + outputs.append( + ResponseOutputMessage( + id=f"msg_{random_uuid()}", + content=[ + ResponseOutputText( + text=content, + annotations=[], + type="output_text", + logprobs=logprobs, + ) + ], + role="assistant", + status="completed", + type="message", + ) + ) + + if tool_calls: + for idx, tool_call in enumerate(tool_calls): + outputs.append( + ResponseFunctionToolCall( + id=f"fc_{random_uuid()}", + call_id=tool_call.id + if tool_call.id + else make_tool_call_id( + id_type=tool_call_id_type, + func_name=tool_call.name, + idx=idx, + ), + type="function_call", + status="completed", + name=tool_call.name, + arguments=tool_call.arguments, + ) + ) + + return outputs + + def should_continue_final_message( request_input: str | list[ResponseInputOutputItem], ) -> bool: diff --git a/vllm/parser/abstract_parser.py b/vllm/parser/abstract_parser.py index 70fb919fce..48db01c14e 100644 --- a/vllm/parser/abstract_parser.py +++ b/vllm/parser/abstract_parser.py @@ -8,21 +8,9 @@ from collections.abc import Sequence from dataclasses import dataclass, field from functools import cached_property -from openai.types.responses import ( - ResponseFunctionToolCall, - ResponseOutputItem, - ResponseOutputMessage, - ResponseOutputText, - ResponseReasoningItem, - ToolChoiceFunction, -) -from openai.types.responses.response_output_text import Logprob -from openai.types.responses.response_reasoning_item import ( - Content as ResponseReasoningTextContent, -) +from openai.types.responses import ToolChoiceFunction from pydantic import TypeAdapter, ValidationError -from vllm.entrypoints.chat_utils import make_tool_call_id from vllm.entrypoints.openai.chat_completion.protocol import ( ChatCompletionNamedToolChoiceParam, ChatCompletionRequest, @@ -43,7 +31,6 @@ from vllm.tool_parsers.streaming import ( extract_named_tool_call_streaming, extract_required_tool_call_streaming, ) -from vllm.utils import random_uuid logger = init_logger(__name__) @@ -179,36 +166,6 @@ class Parser: The extracted content token IDs. """ - @abstractmethod - def extract_response_outputs( - self, - *, - model_output: str, - model_output_token_ids: Sequence[int], - request: ResponsesRequest, - enable_auto_tools: bool = False, - tool_call_id_type: str = "random", - logprobs: list[Logprob] | None = None, - ) -> list[ResponseOutputItem]: - """ - Extract reasoning, content, and tool calls from a complete - model-generated string and return as ResponseOutputItem objects. - - Used for non-streaming responses where we have the entire model - response available before sending to the client. - - Args: - model_output: The complete model-generated string. - model_output_token_ids: The token IDs of the model output. - request: The request object used to generate the output. - enable_auto_tools: Whether to enable automatic tool call parsing. - tool_call_id_type: Type of tool call ID generation ("random", etc). - logprobs: Pre-computed logprobs for the output text, if any. - - Returns: - A list of ResponseOutputItem objects. - """ - @abstractmethod def extract_reasoning( self, @@ -375,83 +332,6 @@ class DelegatingParser(Parser): return None, model_output return self._reasoning_parser.extract_reasoning(model_output, request) - def extract_response_outputs( - self, - *, - model_output: str, - model_output_token_ids: Sequence[int], - request: ResponsesRequest, - enable_auto_tools: bool = False, - tool_call_id_type: str = "random", - logprobs: list[Logprob] | None = None, - ) -> list[ResponseOutputItem]: - # First extract reasoning - reasoning, content = self.extract_reasoning(model_output, request) - - # Then parse tool calls from the content - tool_calls, content = self._parse_tool_calls( - request=request, - content=content, - enable_auto_tools=enable_auto_tools, - ) - - # Build output items - outputs: list[ResponseOutputItem] = [] - - # Add reasoning item if present - if reasoning: - reasoning_item = ResponseReasoningItem( - id=f"rs_{random_uuid()}", - summary=[], - type="reasoning", - content=[ - ResponseReasoningTextContent(text=reasoning, type="reasoning_text") - ], - status=None, # NOTE: Only the last output item has status. - ) - outputs.append(reasoning_item) - - # Add message item if there's content - if content: - res_text_part = ResponseOutputText( - text=content, - annotations=[], - type="output_text", - logprobs=logprobs, - ) - message_item = ResponseOutputMessage( - id=f"msg_{random_uuid()}", - content=[res_text_part], - role="assistant", - status="completed", - type="message", - ) - outputs.append(message_item) - - if tool_calls: - # We use a simple counter for history_tool_call_count because - # we don't track the history of tool calls in the Responses API yet. - # This means that the tool call index will start from 0 for each - # request. - for history_tool_call_cnt, tool_call in enumerate(tool_calls): - tool_call_item = ResponseFunctionToolCall( - id=f"fc_{random_uuid()}", - call_id=tool_call.id - if tool_call.id - else make_tool_call_id( - id_type=tool_call_id_type, - func_name=tool_call.name, - idx=history_tool_call_cnt, - ), - type="function_call", - status="completed", - name=tool_call.name, - arguments=tool_call.arguments, - ) - outputs.append(tool_call_item) - - return outputs - def _get_function_name( self, request: ChatCompletionRequest | ResponsesRequest ) -> str: @@ -463,79 +343,6 @@ class DelegatingParser(Parser): return request.tool_choice.function.name raise ValueError("Invalid tool_choice for function name extraction.") - def _parse_tool_calls( - self, - request: ResponsesRequest, - content: str | None, - enable_auto_tools: bool, - ) -> tuple[list[FunctionCall], str | None]: - """ - TODO(qandrew): merge _parse_tool_calls_from_content - for ChatCompletions into this function - Parse tool calls from content based on request tool_choice settings. - - Returns: - A tuple of (function_calls, remaining_content) if tool calls - were parsed - """ - function_calls: list[FunctionCall] = [] - - if request.tool_choice and isinstance( - request.tool_choice, - (ToolChoiceFunction, ChatCompletionNamedToolChoiceParam), - ): - # Forced Function Call - if content is None: - return [], None - function_calls.append( - FunctionCall(name=self._get_function_name(request), arguments=content) - ) - return function_calls, None # Clear content since tool is called. - - if request.tool_choice == "required": - # Required tool calls - parse JSON - tool_calls = [] - with contextlib.suppress(ValidationError): - content = content or "" - tool_calls = TypeAdapter(list[FunctionDefinition]).validate_json( - content - ) - for tool_call in tool_calls: - function_calls.append( - FunctionCall( - name=tool_call.name, - arguments=json.dumps(tool_call.parameters, ensure_ascii=False), - ) - ) - return function_calls, None # Clear content since tool is called. - - if ( - self._tool_parser is not None - and enable_auto_tools - and (request.tool_choice == "auto" or request.tool_choice is None) - ): - # Automatic Tool Call Parsing - tool_call_info = self.extract_tool_calls( - content if content is not None else "", - request=request, - ) - if tool_call_info is not None and tool_call_info.tools_called: - function_calls.extend( - FunctionCall( - id=tool_call.id, - name=tool_call.function.name, - arguments=tool_call.function.arguments, - ) - for tool_call in tool_call_info.tool_calls - ) - remaining_content = tool_call_info.content - if remaining_content and remaining_content.strip() == "": - remaining_content = None - return function_calls, remaining_content - - # No tool calls - return [], content - def _extract_tool_calls( self, content: str | None,