From 3f466c852cfd3882820af8dd7963ac775174bce4 Mon Sep 17 00:00:00 2001 From: Abhay Joshi <105213625+abhayjoshi201@users.noreply.github.com> Date: Wed, 14 Oct 2026 10:01:25 +0000 Subject: [PATCH] [Bugfix][Parser] Recover genuine tool calls after unclosed Qwen3 Signed-off-by: Artem Perevedentsev Co-authored-by: Gemini Co-authored-by: Artem Perevedentsev --- tests/parser/engine/test_qwen3.py | 110 ++++++++++++++++++++++++++++++ vllm/parser/qwen3.py | 27 ++++++++ 2 files changed, 137 insertions(+) diff --git a/tests/parser/engine/test_qwen3.py b/tests/parser/engine/test_qwen3.py index 7bdddb2faf..fda047d422 100644 --- a/tests/parser/engine/test_qwen3.py +++ b/tests/parser/engine/test_qwen3.py @@ -274,6 +274,53 @@ class TestNonStreaming: ' "multiSelect": false, "answer": null}]', } + @pytest.mark.parametrize( + "unclosed_func", + [""], + ) + def test_regression_issue_58227(self, parser, mock_request, unclosed_func): + """Regression issue 58227 + Check cases: + * + * ...\n{unclosed_func}\n" + "Wait, let me call the real tool via :\n" + "\n" + "\n" + "Tokyo\n" + "\n" + "" + ) + result = parser.extract_tool_calls(text, mock_request) + + assert result.tools_called is True + assert result.tool_calls[-1].function.name == "get_weather" + assert json.loads(result.tool_calls[-1].function.arguments) == { + "city": "Tokyo", + } + + def test_tool_call_tag_is_ok_inside_parameter(self, parser, mock_request): + """Regression issue 58227 + Check that tag is ok inside parameter value + """ + text = ( + "\n" + "\n" + "Document usage\n" + "\n" + "" + ) + result = parser.extract_tool_calls(text, mock_request) + + assert result.tools_called is True + assert len(result.tool_calls) == 1 + assert result.tool_calls[0].function.name == "write_file" + assert json.loads(result.tool_calls[0].function.arguments) == { + "content": "Document usage", + } + class TestStreaming: def test_basic_streaming(self, parser, mock_request): @@ -635,6 +682,69 @@ class TestStreaming: assert "Bash" in names assert "Read" in names + @pytest.mark.parametrize( + "unclosed_func", + [""], + ) + def test_streaming_regression_issue_58227( + self, parser, mock_request, unclosed_func + ): + """Regression issue 58227 + Check cases: + * + * ...\n", + f"{unclosed_func}\n", + "Wait, let me call the real tool via ", + "", + ":\n", + "\n", + "\n", + "Tokyo\n", + "\n", + "", + ] + results = simulate_tool_streaming(parser, mock_request, chunks) + + calls_by_index: dict[int, dict[str, str]] = {} + for delta, _ in results: + if delta and delta.tool_calls: + for tc in delta.tool_calls: + entry = calls_by_index.setdefault( + tc.index, {"name": "", "arguments": ""} + ) + if tc.function: + if tc.function.name: + entry["name"] += tc.function.name + if tc.function.arguments: + entry["arguments"] += tc.function.arguments + + last_call = calls_by_index[max(calls_by_index)] + assert last_call["name"] == "get_weather" + assert json.loads(last_call["arguments"]) == {"city": "Tokyo"} + + def test_streaming_tool_call_tag_is_ok_inside_parameter(self, parser, mock_request): + """Regression issue 58227 + Check that tag is ok inside parameter value + """ + chunks = [ + "\n", + "\n", + "Document ", + "", + " usage\n", + "\n", + "", + ] + results = simulate_tool_streaming(parser, mock_request, chunks) + + assert collect_function_name(results) == "write_file" + assert json.loads(collect_tool_arguments(results)) == { + "content": "Document usage", + } + class TestArgConverter: """Direct tests for the Qwen3 arg_converter with multi-line values.""" diff --git a/vllm/parser/qwen3.py b/vllm/parser/qwen3.py index a11551a209..d9a71f2a46 100644 --- a/vllm/parser/qwen3.py +++ b/vllm/parser/qwen3.py @@ -169,10 +169,37 @@ def qwen3_config( ParserState.TOOL_BETWEEN, (EventType.TOOL_CALL_END,), ), + # regression issue 58227 + # + # function name is not closed, go to content + (ParserState.TOOL_NAME, "TOOL_END"): Transition( + ParserState.CONTENT, + (EventType.TOOL_CALL_END,), + ), + # regression issue 58227 + # + # unclosed tool call followed by another tool call + (ParserState.TOOL_NAME, "TOOL_START"): Transition( + ParserState.TOOL_PREAMBLE, + (EventType.TOOL_CALL_END, EventType.TOOL_CALL_START), + ), + # regression issue 58227 + # ...