diff --git a/tests/tool_parsers/test_hunyuan_a13b_tool_parser.py b/tests/tool_parsers/test_hunyuan_a13b_tool_parser.py index 90f08bb82e09..8593a5701a97 100644 --- a/tests/tool_parsers/test_hunyuan_a13b_tool_parser.py +++ b/tests/tool_parsers/test_hunyuan_a13b_tool_parser.py @@ -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. + ( + '[{"name": "create_user", "arguments": {"name": "Alice", "age": 30}}]', # noqa: E501 + [make_tool_call("create_user", {"name": "Alice", "age": 30})], + None, + ), ], ) def test_hunyuan_a13b_tool_parser_extract( @@ -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). + ( + [ + '[{"name": "create_user", ', + '"arguments": {"name": "Alice", ', + '"age": 30}}]', + "", + ], + [make_tool_call("create_user", {"name": "Alice", "age": 30})], + ), ], ) def test_hunyuan_a13b_tool_parser_streaming(model_deltas, expected_tool_calls): diff --git a/tests/tool_parsers/test_xlam_tool_parser.py b/tests/tool_parsers/test_xlam_tool_parser.py index 3853d2039a72..fe7ce1c091b9 100644 --- a/tests/tool_parsers/test_xlam_tool_parser.py +++ b/tests/tool_parsers/test_xlam_tool_parser.py @@ -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=[ @@ -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( @@ -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", diff --git a/vllm/tool_parsers/hunyuan_a13b_tool_parser.py b/vllm/tool_parsers/hunyuan_a13b_tool_parser.py index 9723ef45d24e..3dd3789c67de 100644 --- a/vllm/tool_parsers/hunyuan_a13b_tool_parser.py +++ b/vllm/tool_parsers/hunyuan_a13b_tool_parser.py @@ -51,7 +51,12 @@ def __init__(self, tokenizer: TokenizerLike, tools: list[Tool] | None = None): r"([\s\S]*?)", 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*\}' diff --git a/vllm/tool_parsers/xlam_tool_parser.py b/vllm/tool_parsers/xlam_tool_parser.py index 61eaaf952b24..51a0d653d8dd 100644 --- a/vllm/tool_parsers/xlam_tool_parser.py +++ b/vllm/tool_parsers/xlam_tool_parser.py @@ -54,6 +54,13 @@ def __init__(self, tokenizer: TokenizerLike, tools: list[Tool] | None = None): ] self.thinking_tag_pattern = r"([\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, @@ -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) @@ -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