[Frontend] Support CustomToolCall in the non-Harmony Responses API (#57322)

Signed-off-by: Shijin Zhang <75300765+Dovis01@users.noreply.github.com>
Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
This commit is contained in:
Shijin Zhang
2026-10-12 17:22:55 +08:00
committed by GitHub
co-authored by Chauncey
parent a33d46b471
commit 83773ecdf3
22 changed files with 698 additions and 72 deletions
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from openai.types.responses import ToolChoiceFunction
from openai.types.responses import ToolChoiceCustom, ToolChoiceFunction
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionNamedToolChoiceParam,
@@ -29,7 +29,12 @@ class Gemma4EngineToolParser(Gemma4ParserToolAdapter): # type: ignore[valid-typ
if request.tools:
tc = request.tool_choice
if tc == "required" or isinstance(
tc, (ChatCompletionNamedToolChoiceParam, ToolChoiceFunction)
tc,
(
ChatCompletionNamedToolChoiceParam,
ToolChoiceFunction,
ToolChoiceCustom,
),
):
request.skip_special_tokens = False
return request
+2 -2
View File
@@ -6,7 +6,7 @@ from collections.abc import Sequence
from typing import Any
import regex as re
from openai.types.responses import ToolChoiceFunction
from openai.types.responses import ToolChoiceCustom, ToolChoiceFunction
from vllm.entrypoints.generate.base.protocol import (
DeltaFunctionCall,
@@ -184,7 +184,7 @@ class K2HorizonToolParser(ToolParser):
tool_choice = request.tool_choice
if isinstance(tool_choice, ChatCompletionNamedToolChoiceParam):
return tool_choice.function.name
if isinstance(tool_choice, ToolChoiceFunction):
if isinstance(tool_choice, (ToolChoiceFunction, ToolChoiceCustom)):
return tool_choice.name
return None
+6 -2
View File
@@ -35,7 +35,7 @@ import json
from collections.abc import Sequence
import regex as re
from openai.types.responses import ToolChoiceFunction
from openai.types.responses import ToolChoiceCustom, ToolChoiceFunction
from vllm.entrypoints.generate.base.protocol import (
DeltaFunctionCall,
@@ -149,7 +149,11 @@ class KimiK3ToolParser(ToolParser):
) -> ChatCompletionRequest | ResponsesRequest:
named = isinstance(
request.tool_choice,
(ChatCompletionNamedToolChoiceParam, ToolChoiceFunction),
(
ChatCompletionNamedToolChoiceParam,
ToolChoiceFunction,
ToolChoiceCustom,
),
)
structured_outputs = getattr(request, "structured_outputs", None)
has_structural_tag = (
@@ -38,7 +38,7 @@ import json
from collections.abc import Iterator, Sequence
import regex as re
from openai.types.responses import ToolChoiceFunction
from openai.types.responses import ToolChoiceCustom, ToolChoiceFunction
from transformers import PreTrainedTokenizerBase
from vllm.entrypoints.generate.base.protocol import (
@@ -228,7 +228,12 @@ class MuseGlimmerToolParser(ToolParser):
if request.tools and (
tool_choice == "required"
or isinstance(
tool_choice, (ChatCompletionNamedToolChoiceParam, ToolChoiceFunction)
tool_choice,
(
ChatCompletionNamedToolChoiceParam,
ToolChoiceFunction,
ToolChoiceCustom,
),
)
):
return request
+7 -2
View File
@@ -16,7 +16,7 @@ from typing import Any
import partial_json_parser.core.complete
import regex as re
from openai.types.responses import ToolChoiceFunction
from openai.types.responses import ToolChoiceCustom, ToolChoiceFunction
from partial_json_parser.core.options import Allow
from vllm.entrypoints.generate.base.protocol import (
@@ -171,7 +171,12 @@ class PoolsideV1ToolParser(ToolParser):
if request.tools:
tc = request.tool_choice
if tc == "required" or isinstance(
tc, (ChatCompletionNamedToolChoiceParam, ToolChoiceFunction)
tc,
(
ChatCompletionNamedToolChoiceParam,
ToolChoiceFunction,
ToolChoiceCustom,
),
):
request.skip_special_tokens = False
return request
+12 -3
View File
@@ -6,7 +6,7 @@ import json
from collections.abc import Callable, Sequence
from typing import Any, Literal, TypeAlias
from openai.types.responses import FunctionTool
from openai.types.responses import CustomTool, FunctionTool, ToolChoiceCustom
from openai.types.responses.response import ToolChoice as ResponsesToolChoice
from openai.types.responses.tool import Tool as ResponsesTool
from openai.types.responses.tool_choice_allowed import ToolChoiceAllowed
@@ -39,7 +39,11 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionToolsParam,
)
from vllm.tool_parsers.tool_strict_level import ToolStrictLevel
from vllm.tool_parsers.utils import get_function_tools, require_function_tools
from vllm.tool_parsers.utils import (
custom_tool_parameters,
get_function_tools,
require_function_tools,
)
ToolChoice: TypeAlias = (
Literal["none", "auto", "required"]
@@ -287,6 +291,11 @@ def _dump_tool_for_xgrammar(
if tool.strict is not None:
function["strict"] = tool.strict
return {"type": "function", "function": function}
if isinstance(tool, CustomTool):
function = {"name": tool.name, "parameters": custom_tool_parameters()}
if tool.description is not None:
function["description"] = tool.description
return {"type": "function", "function": function}
dumped_tool = tool.model_dump(mode="json", exclude_none=True)
if isinstance(tool, ChatCompletionToolsParam):
return dumped_tool
@@ -306,7 +315,7 @@ def _dump_tool_choice_for_xgrammar(
if isinstance(tool_choice, ChatCompletionNamedToolChoiceParam):
return tool_choice.model_dump(mode="json", exclude_none=True)
if isinstance(tool_choice, ToolChoiceFunction):
if isinstance(tool_choice, (ToolChoiceFunction, ToolChoiceCustom)):
return {
"type": "function",
"function": {"name": tool_choice.name},
+42 -7
View File
@@ -13,8 +13,10 @@ from typing import Any, TypeAlias
import partial_json_parser
from openai.types.responses import (
CustomTool,
FunctionTool,
NamespaceTool,
ToolChoiceCustom,
ToolChoiceFunction,
)
from openai.types.responses.tool import Tool as ResponsesTool
@@ -37,6 +39,32 @@ Tool: TypeAlias = ChatCompletionToolsParam | ResponsesTool
logger = init_logger(__name__)
CUSTOM_TOOL_INPUT_KEY = "input"
def custom_tool_parameters() -> dict[str, Any]:
"""Schema of the single-string function shim a ``custom`` tool is shown as."""
return {
"type": "object",
"properties": {
CUSTOM_TOOL_INPUT_KEY: {
"type": "string",
"description": "The freeform text payload for this tool.",
}
},
"required": [CUSTOM_TOOL_INPUT_KEY],
"additionalProperties": False,
}
def custom_tool_as_function_dict(tool: CustomTool) -> dict[str, Any]:
return {
"type": "function",
"name": tool.name,
"description": tool.description,
"parameters": custom_tool_parameters(),
}
def safe_literal_eval(text: str):
with warnings.catch_warnings():
@@ -188,8 +216,9 @@ def get_function_tools(
This is the single source for both the chat template's tool list and the
tools a tool-call grammar constrains, so the two cannot drift apart.
Namespace functions are flattened to ``<namespace>__<name>``; other
Responses tool types are never rendered for the model and are dropped.
Namespace functions are flattened to ``<namespace>__<name>``; custom tools
become a single-string function shim. Other Responses tool types are never
rendered for the model and are dropped.
"""
function_tools: list[ChatCompletionToolsParam | FunctionTool] = []
for tool in tools:
@@ -206,6 +235,10 @@ def get_function_tools(
for namespaced_tool in tool.tools
if namespaced_tool.type == "function"
)
elif isinstance(tool, CustomTool):
function_tools.append(
FunctionTool.model_validate(custom_tool_as_function_dict(tool))
)
elif isinstance(tool, (FunctionTool, ChatCompletionToolsParam)):
function_tools.append(tool)
return function_tools
@@ -307,7 +340,7 @@ def find_tool_properties(
if not tools:
return {}
for tool in tools:
if isinstance(tool, (FunctionTool, NamespaceTool)):
if isinstance(tool, (FunctionTool, NamespaceTool, CustomTool)):
for name, params in iter_response_function_tool_info(tool):
if name == tool_name:
return (params or {}).get("properties", {})
@@ -328,7 +361,7 @@ def find_tool_name(
if not tools:
return False
for tool in tools:
if isinstance(tool, (FunctionTool, NamespaceTool)):
if isinstance(tool, (FunctionTool, NamespaceTool, CustomTool)):
for name, _ in iter_response_function_tool_info(tool):
if name == tool_name:
return True
@@ -414,7 +447,9 @@ def _get_json_schema_from_tools(
def get_json_schema_from_tools(
tool_choice: str | ToolChoiceFunction | ChatCompletionNamedToolChoiceParam,
tool_choice: (
str | ToolChoiceFunction | ToolChoiceCustom | ChatCompletionNamedToolChoiceParam
),
tools: list[Tool] | None,
parallel_tool_calls: bool | None = None,
) -> str | dict | None:
@@ -423,12 +458,12 @@ def get_json_schema_from_tools(
return None
# tool_choice: Forced Function (Responses)
if (not isinstance(tool_choice, str)) and isinstance(
tool_choice, ToolChoiceFunction
tool_choice, (ToolChoiceFunction, ToolChoiceCustom)
):
tool_name = tool_choice.name
responses_tool_map: dict[str, dict[str, Any] | None] = {}
for tool in tools:
if not isinstance(tool, (FunctionTool, NamespaceTool)):
if not isinstance(tool, (FunctionTool, NamespaceTool, CustomTool)):
continue
for name, params in iter_response_function_tool_info(tool):
responses_tool_map[name] = params