[Tool Parser][2/3] Use self.tools instead of request.tools in tool parsers (#38189)

Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
Flora Feng
2026-03-31 13:41:36 +08:00
committed by GitHub
parent 44eef0ca1e
commit d53cb9cb8e
16 changed files with 113 additions and 105 deletions
+10 -1
View File
@@ -10,9 +10,11 @@ from openai.types.responses import (
ResponseFormatTextJSONSchemaConfig,
ResponseTextConfig,
)
from openai.types.responses.function_tool import FunctionTool
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionRequest,
ChatCompletionToolsParam,
)
from vllm.entrypoints.openai.engine.protocol import (
DeltaMessage,
@@ -54,7 +56,14 @@ class ToolParser:
self.streamed_args_for_tool: list[str] = []
self.model_tokenizer = tokenizer
self.tools = tools
if tools:
self.tools: list[ChatCompletionToolsParam | FunctionTool] = [
tool
for tool in tools
if isinstance(tool, (ChatCompletionToolsParam, FunctionTool))
]
else:
self.tools = []
@cached_property
def vocab(self) -> dict[str, int]:
+3 -6
View File
@@ -142,12 +142,11 @@ class DeepSeekV32ToolParser(ToolParser):
self,
function_name: str,
param_dict: dict[str, str],
request: ChatCompletionRequest | None,
) -> dict[str, Any]:
"""Convert raw string param values using the tool schema types."""
param_config: dict = {}
if request and request.tools:
for tool in request.tools:
if self.tools:
for tool in self.tools:
if (
hasattr(tool, "function")
and tool.function.name == function_name
@@ -241,9 +240,7 @@ class DeepSeekV32ToolParser(ToolParser):
invoke_name, invoke_body = complete_invokes[self.current_tool_index]
param_dict = self._parse_invoke_params(invoke_body)
converted = self._convert_params_with_schema(
invoke_name, param_dict, request
)
converted = self._convert_params_with_schema(invoke_name, param_dict)
args_json = json.dumps(converted, ensure_ascii=False)
idx = self.current_tool_index
self.current_tool_index += 1
+2 -2
View File
@@ -189,7 +189,7 @@ class Glm4MoeModelToolParser(ToolParser):
for key, value in pairs:
arg_key = key.strip()
arg_val = value.strip()
if not self._is_string_type(tc_name, arg_key, request.tools):
if not self._is_string_type(tc_name, arg_key, self.tools):
arg_val = self._deserialize(arg_val)
logger.debug("arg_key = %s, arg_val = %s", arg_key, arg_val)
arg_dct[arg_key] = arg_val
@@ -330,7 +330,7 @@ class Glm4MoeModelToolParser(ToolParser):
key = (self._pending_key or "").strip()
is_string = self._is_string_type(
self._current_tool_name, key, request.tools
self._current_tool_name, key, self.tools
)
if is_string:
+1 -1
View File
@@ -200,7 +200,7 @@ class Internlm2ToolParser(ToolParser):
request: ChatCompletionRequest,
) -> ExtractedToolCallInformation:
text = model_output
tools = request.tools
tools = self.tools
if "<|action_start|><|plugin|>" in text:
text, action = text.split("<|action_start|><|plugin|>")
action = action.split("<|action_end|>".strip())[0]
+2 -4
View File
@@ -308,7 +308,7 @@ class MinimaxM2ToolParser(ToolParser):
invoke_str = complete_invokes[self.current_tool_index]
tool_call = self._parse_single_invoke(
invoke_str,
request.tools if request else None,
self.tools,
)
if not tool_call:
self.current_tool_index += 1
@@ -358,9 +358,7 @@ class MinimaxM2ToolParser(ToolParser):
for tool_call_match in self.tool_call_complete_regex.findall(model_output):
# Find all invokes within this tool_call
for invoke_match in self.invoke_complete_regex.findall(tool_call_match):
tool_call = self._parse_single_invoke(
invoke_match, request.tools if request else None
)
tool_call = self._parse_single_invoke(invoke_match, self.tools)
if tool_call:
tool_calls.append(tool_call)
+3 -5
View File
@@ -314,7 +314,7 @@ class Qwen3CoderToolParser(ToolParser):
)
tool_calls = [
self._parse_xml_function_call(function_call_str, request.tools)
self._parse_xml_function_call(function_call_str, self.tools)
for function_call_str in function_calls
]
# Populate prev_tool_call_arr for serving layer to set finish_reason
@@ -607,7 +607,7 @@ class Qwen3CoderToolParser(ToolParser):
param_config = self._get_arguments_config(
self.current_function_name or "",
self.streaming_request.tools if self.streaming_request else None,
self.tools,
)
converted_value = self._convert_param_value(
@@ -666,9 +666,7 @@ class Qwen3CoderToolParser(ToolParser):
try:
parsed_tool = self._parse_xml_function_call(
func_content,
self.streaming_request.tools
if self.streaming_request
else None,
self.tools,
)
if parsed_tool and self.current_tool_index < len(
self.prev_tool_call_arr
+2 -4
View File
@@ -1188,8 +1188,7 @@ class Qwen3XMLToolParser(ToolParser):
# Reset tool call tracking arrays for new extraction
self.prev_tool_call_arr = []
self.streamed_args_for_tool = []
if request:
self.parser.set_tools(request.tools)
self.parser.set_tools(self.tools)
result = self.parser.parse_single_streaming_chunks(model_output)
if not result.tool_calls:
return ExtractedToolCallInformation(
@@ -1260,8 +1259,7 @@ class Qwen3XMLToolParser(ToolParser):
# Reset tool call tracking arrays for new streaming session
self.prev_tool_call_arr = []
self.streamed_args_for_tool = []
if request:
self.parser.set_tools(request.tools)
self.parser.set_tools(self.tools)
# Model sometimes outputs separately causing delta_text to be empty.
# If there were tool_calls before and all current tool_calls have ended,
+2 -2
View File
@@ -312,7 +312,7 @@ class SeedOssToolParser(ToolParser):
)
tool_calls = [
self._parse_xml_function_call(function_call_str, request.tools)
self._parse_xml_function_call(function_call_str, self.tools)
for function_call_str in function_calls
]
@@ -566,7 +566,7 @@ class SeedOssToolParser(ToolParser):
# Parse to get the complete arguments
try:
parsed_tool = self._parse_xml_function_call(
func_content, request.tools if request else None
func_content, self.tools
)
if parsed_tool:
# Update existing entry in prev_tool_call_arr with complete arguments
+2 -4
View File
@@ -82,9 +82,8 @@ class Step3ToolParser(ToolParser):
self,
func_name: str,
params: dict[str, Any],
request: ChatCompletionRequest,
) -> dict[str, Any]:
for tool in request.tools or []:
for tool in self.tools or []:
if tool.function.name == func_name:
schema = tool.function.parameters or {}
properties = schema.get("properties", {})
@@ -234,7 +233,6 @@ class Step3ToolParser(ToolParser):
final_args = self._cast_arguments(
function_name,
tool_call_arr.get("parameters", {}), # type: ignore
request,
)
if final_args:
final_args_json = json.dumps(final_args, ensure_ascii=False)
@@ -291,7 +289,7 @@ class Step3ToolParser(ToolParser):
function_name, params_dict = self._parse_steptml_invoke(invoke_part)
if function_name and params_dict is not None:
params_dict = self._cast_arguments(function_name, params_dict, request)
params_dict = self._cast_arguments(function_name, params_dict)
params_str = json.dumps(params_dict, ensure_ascii=False)
tool_calls.append(
ToolCall(
+2 -4
View File
@@ -1385,8 +1385,7 @@ class Step3p5ToolParser(ToolParser):
# Reset tool call tracking arrays for new extraction
self.prev_tool_call_arr = []
self.streamed_args_for_tool = []
if request:
self.parser.set_tools(request.tools)
self.parser.set_tools(self.tools)
result = self.parser.parse_single_streaming_chunks(model_output)
if not result.tool_calls:
return ExtractedToolCallInformation(
@@ -1457,8 +1456,7 @@ class Step3p5ToolParser(ToolParser):
# Reset tool call tracking arrays for new streaming session
self.prev_tool_call_arr = []
self.streamed_args_for_tool = []
if request:
self.parser.set_tools(request.tools)
self.parser.set_tools(self.tools)
# Model sometimes outputs separately causing delta_text to be empty.
# If there were tool_calls before and all current tool_calls have ended,