[Bugfix][Frontend] Honor parallel_tool_calls=false in the Responses API (#59298)
Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
@@ -37,6 +37,9 @@ from vllm.entrypoints.openai.responses.utils import (
|
||||
build_response_output_items,
|
||||
construct_tool_dicts,
|
||||
)
|
||||
from vllm.entrypoints.serve.utils.tool_calls_utils import (
|
||||
maybe_filter_parallel_tool_calls,
|
||||
)
|
||||
from vllm.outputs import RequestOutput
|
||||
from vllm.parser.abstract_parser import Parser
|
||||
from vllm.tokenizers import TokenizerLike
|
||||
@@ -369,7 +372,9 @@ class ParsableContext(ConversationContext):
|
||||
build_response_output_items(
|
||||
reasoning=reasoning,
|
||||
content=content,
|
||||
tool_calls=tool_calls,
|
||||
tool_calls=maybe_filter_parallel_tool_calls(
|
||||
tool_calls or [], self.request
|
||||
),
|
||||
tools=self.request.tools,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -73,6 +73,9 @@ from vllm.entrypoints.openai.responses.utils import (
|
||||
from vllm.entrypoints.serve.engine.protocol import ErrorResponse
|
||||
from vllm.entrypoints.serve.utils.api_utils import get_max_tokens
|
||||
from vllm.entrypoints.serve.utils.request_logger import RequestLogger
|
||||
from vllm.entrypoints.serve.utils.tool_calls_utils import (
|
||||
maybe_filter_parallel_tool_calls,
|
||||
)
|
||||
from vllm.exceptions import GenerationError, VLLMValidationError
|
||||
from vllm.inputs import EngineInput
|
||||
from vllm.logger import init_logger
|
||||
@@ -1028,7 +1031,7 @@ class OpenAIServingResponses(GenerateBaseServing):
|
||||
return build_response_output_items(
|
||||
reasoning=reasoning,
|
||||
content=content,
|
||||
tool_calls=tool_calls,
|
||||
tool_calls=maybe_filter_parallel_tool_calls(tool_calls or [], request),
|
||||
logprobs=logprobs,
|
||||
tools=request.tools,
|
||||
)
|
||||
@@ -1231,6 +1234,7 @@ class OpenAIServingResponses(GenerateBaseServing):
|
||||
|
||||
if not delta_message:
|
||||
continue
|
||||
delta_message = maybe_filter_parallel_tool_calls(delta_message, request)
|
||||
|
||||
for dm in split_delta(delta_message):
|
||||
target_state, tool_call = processor.resolve_target_state(dm)
|
||||
|
||||
@@ -2,35 +2,42 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
from typing import TypeVar
|
||||
|
||||
from vllm.entrypoints.generate.base.protocol import DeltaMessage, FunctionCall
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatCompletionRequest,
|
||||
ChatCompletionResponseChoice,
|
||||
ChatCompletionResponseStreamChoice,
|
||||
)
|
||||
from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
|
||||
|
||||
# Used internally
|
||||
_ChatCompletionResponseChoiceT = TypeVar(
|
||||
"_ChatCompletionResponseChoiceT",
|
||||
_ToolCallsContainerT = TypeVar(
|
||||
"_ToolCallsContainerT",
|
||||
ChatCompletionResponseChoice,
|
||||
ChatCompletionResponseStreamChoice,
|
||||
DeltaMessage,
|
||||
list[FunctionCall],
|
||||
)
|
||||
|
||||
|
||||
def maybe_filter_parallel_tool_calls(
|
||||
choice: _ChatCompletionResponseChoiceT, request: ChatCompletionRequest
|
||||
) -> _ChatCompletionResponseChoiceT:
|
||||
item: _ToolCallsContainerT,
|
||||
request: ChatCompletionRequest | ResponsesRequest,
|
||||
) -> _ToolCallsContainerT:
|
||||
"""Filter to first tool call only when parallel_tool_calls is explicitly False."""
|
||||
if request.parallel_tool_calls is not False:
|
||||
return choice
|
||||
return item
|
||||
|
||||
if isinstance(choice, ChatCompletionResponseChoice) and choice.message.tool_calls:
|
||||
choice.message.tool_calls = choice.message.tool_calls[:1]
|
||||
elif (
|
||||
isinstance(choice, ChatCompletionResponseStreamChoice)
|
||||
and choice.delta.tool_calls
|
||||
):
|
||||
choice.delta.tool_calls = [
|
||||
tool_call for tool_call in choice.delta.tool_calls if tool_call.index == 0
|
||||
if isinstance(item, list):
|
||||
return item[:1]
|
||||
if isinstance(item, ChatCompletionResponseChoice):
|
||||
if item.message.tool_calls:
|
||||
item.message.tool_calls = item.message.tool_calls[:1]
|
||||
return item
|
||||
|
||||
delta = item.delta if isinstance(item, ChatCompletionResponseStreamChoice) else item
|
||||
if delta.tool_calls:
|
||||
delta.tool_calls = [
|
||||
tool_call for tool_call in delta.tool_calls if tool_call.index == 0
|
||||
]
|
||||
|
||||
return choice
|
||||
return item
|
||||
|
||||
Reference in New Issue
Block a user