From 11d291511a35bfa2a9ecb8fd21ee1c48b3a78d6b Mon Sep 17 00:00:00 2001 From: mosya415 Date: Fri, 17 Jul 2026 19:45:41 +0300 Subject: [PATCH] [Bugfix][Tool Parser] Preserve whitespace in parameter values (MiniMax M2, Qwen3, MiniCPM5 XML) (#48846) Signed-off-by: mosya415 <263250241+mosya415@users.noreply.github.com> Signed-off-by: Ben Browning <56071+bbrowning@users.noreply.github.com> Co-authored-by: mosya415 <263250241+mosya415@users.noreply.github.com> Co-authored-by: Ben Browning <56071+bbrowning@users.noreply.github.com> --- tests/parser/engine/test_qwen3.py | 4 +- .../test_minicpm5xml_tool_parser.py | 52 ++++++++++++++++++ .../test_minimax_m2_tool_parser.py | 34 ++++++++++++ .../test_qwen3coder_tool_parser.py | 55 +++++++++++++++++++ vllm/parser/minimax_m2.py | 7 ++- vllm/parser/qwen3.py | 13 ++++- vllm/tool_parsers/minicpm5xml_tool_parser.py | 8 ++- 7 files changed, 164 insertions(+), 9 deletions(-) diff --git a/tests/parser/engine/test_qwen3.py b/tests/parser/engine/test_qwen3.py index 3845076242..07a025876a 100644 --- a/tests/parser/engine/test_qwen3.py +++ b/tests/parser/engine/test_qwen3.py @@ -454,10 +454,10 @@ class TestStreaming: args_after_partial_tag = collect_tool_arguments(results[:4]) assert " None: "nums": [7, 8, 9], "exact": False, } + + +def make_tools_write() -> list[ChatCompletionToolsParam]: + return [ + _tool( + "write_file", + { + "type": "object", + "properties": {"content": {"type": "string"}}, + "required": ["content"], + }, + ) + ] + + +class TestParameterWhitespace: + """CDATA is verbatim, so its whitespace must survive.""" + + def test_cdata_whitespace_preserved(self, parser: ToolParser) -> None: + request = make_request(make_tools_write()) + text = ( + '' + '' + "\n" + ) + + out = parser.extract_tool_calls(text, request) + + assert len(out.tool_calls) == 1 + args = json.loads(out.tool_calls[0].function.arguments) + assert args["content"] == " def foo():\n pass\n" + + def test_cdata_whitespace_preserved_streaming(self, parser: ToolParser) -> None: + """Value split across chunks, i.e. the partial path.""" + request = make_request(make_tools_write()) + chunks = [ + '', + '\n", + ] + + reconstructor = run_tool_extraction_streaming( + parser, + chunks, + request, + assert_one_tool_per_delta=False, + ) + + assert len(reconstructor.tool_calls) == 1 + assert json.loads(reconstructor.tool_calls[0].function.arguments) == { + "content": " def foo():\n pass\n" + } diff --git a/tests/tool_parsers/test_minimax_m2_tool_parser.py b/tests/tool_parsers/test_minimax_m2_tool_parser.py index 029ee21ae1..bb4ffdc437 100644 --- a/tests/tool_parsers/test_minimax_m2_tool_parser.py +++ b/tests/tool_parsers/test_minimax_m2_tool_parser.py @@ -567,3 +567,37 @@ class TestNoneStringPreservation: assert len(tc) == 1 parsed = json.loads(tc[0]["arguments"]) assert parsed["value"] == "nil" + + +class TestParameterWhitespace: + """Parameter values must preserve surrounding whitespace.""" + + def test_whitespace_preserved(self, parser): + results = _feed( + parser, + [ + '' + ' hi ' + "", + ], + ) + tc = _collect_tool_calls(results) + assert len(tc) == 1 + assert json.loads(tc[0]["arguments"]) == {"msg": " hi "} + + def test_whitespace_preserved_across_chunks(self, parser): + """Value split before arrives, i.e. the partial path.""" + results = _feed( + parser, + [ + '' + ' def foo():\n', + " return 1\n", + "", + ], + ) + tc = _collect_tool_calls(results) + assert len(tc) == 1 + assert json.loads(tc[0]["arguments"]) == { + "content": " def foo():\n return 1\n" + } diff --git a/tests/tool_parsers/test_qwen3coder_tool_parser.py b/tests/tool_parsers/test_qwen3coder_tool_parser.py index 1f5e51412b..7222625dbb 100644 --- a/tests/tool_parsers/test_qwen3coder_tool_parser.py +++ b/tests/tool_parsers/test_qwen3coder_tool_parser.py @@ -1482,3 +1482,58 @@ def test_adjust_request_required_prefers_structural_tag( out = TestParser(MagicMock(), tools=sample_tools).adjust_request(req) assert out.structured_outputs is not None assert out.structured_outputs.structural_tag is not None + + +WRITE_FILE_TOOLS = [ + ChatCompletionToolsParam( + type="function", + function={ + "name": "write_file", + "parameters": { + "type": "object", + "properties": {"content": {"type": "string"}}, + }, + }, + ) +] + +WRITE_FILE_OUTPUT = ( + "\n\n" + "\n" + " def foo():\n return 1\n\n" + "\n\n" +) + +EXPECTED_CONTENT = " def foo():\n return 1\n" + + +class TestParameterWhitespace: + """Wrapping newlines are markup; the rest of the value is preserved.""" + + def test_whitespace_preserved(self, qwen3_tokenizer): + parser = Qwen3EngineToolParser(qwen3_tokenizer, tools=WRITE_FILE_TOOLS) + request = ChatCompletionRequest( + model=MODEL, messages=[], tools=WRITE_FILE_TOOLS + ) + + extracted = parser.extract_tool_calls(WRITE_FILE_OUTPUT, request=request) + + args = json.loads(extracted.tool_calls[0].function.arguments) + assert args["content"] == EXPECTED_CONTENT + + def test_whitespace_preserved_streaming(self, qwen3_tokenizer): + """The partial path must not strip the value either.""" + parser = Qwen3EngineToolParser(qwen3_tokenizer, tools=WRITE_FILE_TOOLS) + request = ChatCompletionRequest( + model=MODEL, messages=[], tools=WRITE_FILE_TOOLS + ) + + streamed = "" + for delta_message in stream_delta_message_generator( + parser, qwen3_tokenizer, WRITE_FILE_OUTPUT, request + ): + for tool_call in delta_message.tool_calls or []: + if tool_call.function and tool_call.function.arguments: + streamed += tool_call.function.arguments + + assert json.loads(streamed)["content"] == EXPECTED_CONTENT diff --git a/vllm/parser/minimax_m2.py b/vllm/parser/minimax_m2.py index 86fa1d1bad..ab1b7eb114 100644 --- a/vllm/parser/minimax_m2.py +++ b/vllm/parser/minimax_m2.py @@ -69,7 +69,9 @@ def _minimax_m2_arg_converter(raw_args: str, partial: bool) -> str: ).strip() if not name: continue - params[name] = match.group("value").strip() + # Keep the value verbatim (like the glm47 converter): it is inline + # between `>` and ``, so surrounding whitespace is data. + params[name] = match.group("value") if partial: remaining = _PARAM_RE.sub("", raw_args) @@ -82,7 +84,8 @@ def _minimax_m2_arg_converter(raw_args: str, partial: bool) -> str: or "" ).strip() if name: - params[name] = match.group("value").strip() + # Verbatim, same as the complete-match loop above. + params[name] = match.group("value") return json.dumps(params, ensure_ascii=False) diff --git a/vllm/parser/qwen3.py b/vllm/parser/qwen3.py index f80aa6ff7a..0041a46eaa 100644 --- a/vllm/parser/qwen3.py +++ b/vllm/parser/qwen3.py @@ -56,13 +56,22 @@ _PARAM_RE = re.compile( _PARTIAL_PARAM_RE = re.compile(r"<\s*parameter\s*=\s*([^>]+)>(.*)$", re.DOTALL) +def _trim_wrapping_newlines(value: str) -> str: + """Strip one leading and one trailing newline (the Qwen3 template markup).""" + if value.startswith("\n"): + value = value[1:] + if value.endswith("\n"): + value = value[:-1] + return value + + def _qwen3_arg_converter(raw_args: str, partial: bool) -> str: params: dict[str, object] = {} for match in _PARAM_RE.finditer(raw_args): name = match.group(1) value = match.group(2) - params[name] = value.strip() + params[name] = _trim_wrapping_newlines(value) if partial: remaining = _PARAM_RE.sub("", raw_args) @@ -71,7 +80,7 @@ def _qwen3_arg_converter(raw_args: str, partial: bool) -> str: name = m.group(1) value = m.group(2) if name: - params[name] = value.strip() + params[name] = _trim_wrapping_newlines(value) return json.dumps(params, ensure_ascii=False) diff --git a/vllm/tool_parsers/minicpm5xml_tool_parser.py b/vllm/tool_parsers/minicpm5xml_tool_parser.py index a5b5252415..c41eb7c2de 100644 --- a/vllm/tool_parsers/minicpm5xml_tool_parser.py +++ b/vllm/tool_parsers/minicpm5xml_tool_parser.py @@ -360,7 +360,7 @@ def _parse_function_block( if not key: has_invalid_param = True break - val_text = (param.text or "").strip() + val_text = param.text or "" if not _add_argument( func_name or "", key, @@ -392,7 +392,8 @@ def _parse_function_block( val_text = pm.group(2) or "" if val_text.startswith(""): val_text = val_text[len("")] - val_text = val_text.strip() + else: + val_text = val_text.strip() if not _add_argument( func_name or "", key, @@ -445,7 +446,8 @@ def _parse_partial_params( val_text = pm.group(2) or "" if val_text.startswith(""): val_text = val_text[len("")] - val_text = val_text.strip() + else: + val_text = val_text.strip() _add_argument( func_name, key,