Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/tool_parsers/test_hunyuan_a13b_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ def make_tool_call(name, arguments):
],
None,
),
# Argument literally named "name" must not be mistaken for a
# second tool call's name field.
(
'<tool_calls>[{"name": "create_user", "arguments": {"name": "Alice", "age": 30}}]</tool_calls>', # noqa: E501
[make_tool_call("create_user", {"name": "Alice", "age": 30})],
None,
),
],
)
def test_hunyuan_a13b_tool_parser_extract(
Expand Down Expand Up @@ -160,6 +167,17 @@ def test_hunyuan_a13b_tool_parser_extract(
reason="stream parsing not support nested json yet."
),
),
# Argument literally named "name" must not spawn a phantom tool
# call or drop the real call's arguments (regression test).
(
[
'<tool_calls>[{"name": "create_user", ',
'"arguments": {"name": "Alice", ',
'"age": 30}}]',
"</tool_calls>",
],
[make_tool_call("create_user", {"name": "Alice", "age": 30})],
),
],
)
def test_hunyuan_a13b_tool_parser_streaming(model_deltas, expected_tool_calls):
Expand Down
38 changes: 38 additions & 0 deletions tests/tool_parsers/test_xlam_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def test_extract_tool_calls_no_tools(xlam_tool_parser):
"single_tool_with_json_code_block",
"single_tool_with_tool_calls_tag",
"single_tool_with_tool_call_xml_tags",
"argument_named_name",
],
argnames=["model_output", "expected_tool_calls", "expected_content"],
argvalues=[
Expand Down Expand Up @@ -218,6 +219,20 @@ def test_extract_tool_calls_no_tools(xlam_tool_parser):
],
"I'll help you check the weather.",
),
(
# Argument literally named "name" must not be mistaken for a
# second tool call's name field.
"""[{"name": "create_user", "arguments": {"name": "Alice", "age": 30}}]""", # noqa: E501
[
ToolCall(
function=FunctionCall(
name="create_user",
arguments=json.dumps({"name": "Alice", "age": 30}),
)
)
],
None,
),
],
)
def test_extract_tool_calls(
Expand Down Expand Up @@ -362,6 +377,29 @@ def test_streaming_with_list_structure(xlam_tool_parser):
assert result.tool_calls[0].function.name == "get_current_weather"


def test_streaming_argument_named_name(xlam_tool_parser, xlam_tokenizer):
"""An argument literally named "name" must not spawn a phantom tool call
or drop the real call's arguments in streaming mode (regression test)."""
model_output = (
"""[{"name": "create_user", "arguments": {"name": "Alice", "age": 30}}]"""
)
request = ChatCompletionRequest(model=MODEL, messages=[])

names: dict[int, str] = {}
args: dict[int, str] = {}
for delta_message in stream_delta_message_generator(
xlam_tool_parser, xlam_tokenizer, model_output, request
):
for tc in delta_message.tool_calls or []:
if tc.function and tc.function.name:
names[tc.index] = names.get(tc.index, "") + tc.function.name
if tc.function and tc.function.arguments:
args[tc.index] = args.get(tc.index, "") + tc.function.arguments

assert list(names.values()) == ["create_user"]
assert json.loads(args[0]) == {"name": "Alice", "age": 30}


@pytest.mark.parametrize(
ids=[
"parallel_tool_calls",
Expand Down
7 changes: 6 additions & 1 deletion vllm/tool_parsers/hunyuan_a13b_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ def __init__(self, tokenizer: TokenizerLike, tools: list[Tool] | None = None):
r"<tool_calls>([\s\S]*?)</tool_calls>", re.DOTALL
)

self.tool_name_reg = re.compile(r'"name"\s*:\s*"([^"]+)"')
# Anchored on the following "arguments" key so this only matches a
# tool's own name field, not an argument literally called "name"
# (e.g. {"name": "create_user", "arguments": {"name": "Alice"}}).
self.tool_name_reg = re.compile(
r'"name"\s*:\s*"([^"]+)"(?=\s*,\s*"arguments"\s*:)'
)

self.tool_empty_arg_reg = re.compile(
r'"name"\s*:\s*"[^"]+"\s*,\s*"arguments"\s*:\s*\{\s*\}'
Expand Down
13 changes: 9 additions & 4 deletions vllm/tool_parsers/xlam_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def __init__(self, tokenizer: TokenizerLike, tools: list[Tool] | None = None):
]
self.thinking_tag_pattern = r"</think>([\s\S]*)"

# Anchored on the following "arguments" key so this only matches a
# tool's own name field, not an argument literally called "name"
# (e.g. {"name": "create_user", "arguments": {"name": "Alice"}}).
self.tool_name_reg = re.compile(
r'"name"\s*:\s*"([^"]+)"(?=\s*,\s*"arguments"\s*:)'
)

# Define streaming state type to be initialized later
self.streaming_state: dict[str, Any] = {
"current_tool_index": -1,
Expand Down Expand Up @@ -270,8 +277,7 @@ def extract_tool_calls_streaming(
and self.current_tools_sent[0] is False
):
# Extract the function name using regex
name_pattern = r'"name"\s*:\s*"([^"]+)"'
name_match = re.search(name_pattern, current_text)
name_match = self.tool_name_reg.search(current_text)
if name_match:
function_name = name_match.group(1)

Expand Down Expand Up @@ -327,8 +333,7 @@ def extract_tool_calls_streaming(
search_text = potential_json

# Try to find complete tool names first
name_pattern = r'"name"\s*:\s*"([^"]+)"'
name_matches = list(re.finditer(name_pattern, search_text))
name_matches = list(self.tool_name_reg.finditer(search_text))
tool_count = len(name_matches)

# If no complete tool names found, check for partial tool names
Expand Down
Loading