[Bugfix][Responses] Add tests for Chat Completions Responses API Render Parity (#50334)
Signed-off-by: Yifan Zong <yzong@redhat.com> Co-authored-by: Ben Browning <56071+bbrowning@users.noreply.github.com>
This commit is contained in:
@@ -179,6 +179,7 @@ class ChatCompletionToolsParam(OpenAIBaseModel):
|
||||
@model_serializer(mode="wrap")
|
||||
def _serialize(self, handler):
|
||||
data = handler(self)
|
||||
data = {k: v for k, v in data.items() if k in type(self).model_fields}
|
||||
if self.defer_loading is None:
|
||||
data.pop("defer_loading", None)
|
||||
return data
|
||||
|
||||
@@ -296,6 +296,7 @@ class FunctionDefinition(OpenAIBaseModel):
|
||||
@model_serializer(mode="wrap")
|
||||
def _serialize(self, handler):
|
||||
data = handler(self)
|
||||
data = {k: v for k, v in data.items() if k in type(self).model_fields}
|
||||
if self.strict is None:
|
||||
data.pop("strict", None)
|
||||
if self.defer_loading is None:
|
||||
|
||||
@@ -610,7 +610,13 @@ class OpenAIServingResponses(GenerateBaseServing):
|
||||
request: ResponsesRequest,
|
||||
prev_response: ResponsesResponse | None,
|
||||
):
|
||||
tool_dicts = construct_tool_dicts(request.tools, request.tool_choice)
|
||||
tool_dicts = construct_tool_dicts(
|
||||
request.tools,
|
||||
request.tool_choice,
|
||||
exclude_tools_when_tool_choice_none=(
|
||||
self.online_renderer.exclude_tools_when_tool_choice_none
|
||||
),
|
||||
)
|
||||
# Construct the input messages.
|
||||
messages = construct_input_messages(
|
||||
request_instructions=request.instructions,
|
||||
|
||||
@@ -31,8 +31,11 @@ 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.chat_completion.protocol import (
|
||||
ChatCompletionMessageParam,
|
||||
ChatCompletionToolsParam,
|
||||
)
|
||||
from vllm.entrypoints.openai.engine.protocol import FunctionCall, FunctionDefinition
|
||||
from vllm.entrypoints.openai.responses.protocol import ResponseInputOutputItem
|
||||
from vllm.logger import init_logger
|
||||
from vllm.tool_parsers.utils import (
|
||||
@@ -363,27 +366,30 @@ def extract_tool_types(tools: list[Tool]) -> set[str]:
|
||||
return tool_types
|
||||
|
||||
|
||||
def convert_tool_responses_to_completions_format(tool: dict) -> dict:
|
||||
def convert_tool_responses_to_completions_format(
|
||||
tool: dict,
|
||||
) -> ChatCompletionToolsParam:
|
||||
"""
|
||||
Convert a flat tool schema:
|
||||
Convert a flat Responses tool schema:
|
||||
{"type": "function", "name": "...", "description": "...", "parameters": {...}}
|
||||
into:
|
||||
{"type": "function", "function": {...}}
|
||||
into a Chat Completions tool param for chat-template rendering.
|
||||
"""
|
||||
return {
|
||||
"type": "function",
|
||||
"function": tool,
|
||||
}
|
||||
return ChatCompletionToolsParam(
|
||||
type="function",
|
||||
function=FunctionDefinition.model_validate(
|
||||
{k: v for k, v in tool.items() if k != "type"}
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def construct_tool_dicts(
|
||||
tools: list[Tool], tool_choice: ToolChoice
|
||||
tools: list[Tool],
|
||||
tool_choice: ToolChoice,
|
||||
exclude_tools_when_tool_choice_none: bool = False,
|
||||
) -> list[dict[str, Any]] | None:
|
||||
if not tools or (tool_choice == "none"):
|
||||
tool_dicts = None
|
||||
else:
|
||||
tool_dicts = [
|
||||
convert_tool_responses_to_completions_format(tool)
|
||||
for tool in iter_response_function_tool_dicts(tools)
|
||||
]
|
||||
return tool_dicts
|
||||
if not tools or (tool_choice == "none" and exclude_tools_when_tool_choice_none):
|
||||
return None
|
||||
return [
|
||||
convert_tool_responses_to_completions_format(tool).model_dump()
|
||||
for tool in iter_response_function_tool_dicts(tools)
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user