A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 5, 18:30 UTC
[Refactor] Consolidate Tool type alias in tool_parsers/utils.py (#38265)
Signed-off-by: sfeng33 <4florafeng@gmail.com>
Flora Feng committed
Mar 30, 2026 at 20:55 UTC
29e48707e8144b78dd5d756f793c26a405043f3d
2 files changed
+12
-13
vllm/tool_parsers/abstract_tool_parser.py
+3
-6
@@ -5,17 +5,14 @@ import importlib
5
import os
6
from collections.abc import Callable, Sequence
7
from functools import cached_property
8
-from typing import TypeAlias
8
9
from openai.types.responses import (
10
ResponseFormatTextJSONSchemaConfig,
11
ResponseTextConfig,
12
)
14
-from openai.types.responses.tool import Tool as ResponsesTool
13
14
from vllm.entrypoints.openai.chat_completion.protocol import (
15
ChatCompletionRequest,
18
- ChatCompletionToolsParam,
16
)
17
from vllm.entrypoints.openai.engine.protocol import (
18
DeltaMessage,
@@ -29,13 +26,13 @@ from vllm.sampling_params import (
26
StructuredOutputsParams,
27
)
28
from vllm.tokenizers import TokenizerLike
32
-from vllm.tool_parsers.utils import get_json_schema_from_tools
29
+from vllm.tool_parsers.utils import Tool, get_json_schema_from_tools
30
from vllm.utils.collection_utils import is_list_of
31
from vllm.utils.import_utils import import_from_path
32
36
-logger = init_logger(__name__)
33
+__all__ = ["Tool"]
34
38
-Tool: TypeAlias = ChatCompletionToolsParam | ResponsesTool
35
+logger = init_logger(__name__)
36
37
38
class ToolParser:
vllm/tool_parsers/utils.py
+9
-7
@@ -4,14 +4,14 @@
4
import ast
5
import json
6
from json import JSONDecodeError, JSONDecoder
7
-from typing import Any
7
+from typing import Any, TypeAlias
8
9
import partial_json_parser
10
from openai.types.responses import (
11
FunctionTool,
12
ToolChoiceFunction,
13
)
14
-from openai.types.responses.tool import Tool
14
+from openai.types.responses.tool import Tool as ResponsesTool
15
from partial_json_parser.core.options import Allow
16
17
from vllm.entrypoints.openai.chat_completion.protocol import (
@@ -26,6 +26,8 @@ from vllm.entrypoints.openai.engine.protocol import (
26
)
27
from vllm.logger import init_logger
28
29
+Tool: TypeAlias = ChatCompletionToolsParam | ResponsesTool
30
+
31
logger = init_logger(__name__)
32
33
@@ -130,7 +132,7 @@ def consume_space(i: int, s: str) -> int:
132
133
134
def _extract_tool_info(
133
- tool: Tool | ChatCompletionToolsParam,
135
+ tool: Tool,
136
) -> tuple[str, dict[str, Any] | None]:
137
if isinstance(tool, FunctionTool):
138
return tool.name, tool.parameters
@@ -140,7 +142,7 @@ def _extract_tool_info(
142
raise TypeError(f"Unsupported tool type: {type(tool)}")
143
144
143
-def _get_tool_schema_from_tool(tool: Tool | ChatCompletionToolsParam) -> dict:
145
+def _get_tool_schema_from_tool(tool: Tool) -> dict:
146
name, params = _extract_tool_info(tool)
147
params = params if params else {"type": "object", "properties": {}}
148
return {
@@ -153,7 +155,7 @@ def _get_tool_schema_from_tool(tool: Tool | ChatCompletionToolsParam) -> dict:
155
156
157
def _get_tool_schema_defs(
156
- tools: list[Tool | ChatCompletionToolsParam],
158
+ tools: list[Tool],
159
) -> dict:
160
all_defs: dict[str, dict[str, Any]] = {}
161
for tool in tools:
@@ -172,7 +174,7 @@ def _get_tool_schema_defs(
174
175
176
def _get_json_schema_from_tools(
175
- tools: list[Tool | ChatCompletionToolsParam],
177
+ tools: list[Tool],
178
) -> dict:
179
json_schema = {
180
"type": "array",
@@ -190,7 +192,7 @@ def _get_json_schema_from_tools(
192
193
def get_json_schema_from_tools(
194
tool_choice: str | ToolChoiceFunction | ChatCompletionNamedToolChoiceParam,
193
- tools: list[FunctionTool | ChatCompletionToolsParam] | None,
195
+ tools: list[Tool] | None,
196
) -> str | dict | None:
197
# tool_choice: "none"
198
if tool_choice in ("none", None) or tools is None: