[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>
This commit is contained in:
co-authored by
mosya415
Ben Browning
parent
877dae9c68
commit
11d291511a
@@ -454,10 +454,10 @@ class TestStreaming:
|
||||
|
||||
args_after_partial_tag = collect_tool_arguments(results[:4])
|
||||
assert "<param" not in args_after_partial_tag
|
||||
assert args_after_partial_tag == '{"query": "hello'
|
||||
assert args_after_partial_tag == '{"query": "hello '
|
||||
|
||||
args_text = collect_tool_arguments(results)
|
||||
assert json.loads(args_text) == {"query": "hello", "limit": "10"}
|
||||
assert json.loads(args_text) == {"query": "hello ", "limit": "10"}
|
||||
|
||||
def test_streaming_numeric_values(self, parser, mock_request):
|
||||
chunks = [
|
||||
|
||||
@@ -733,3 +733,55 @@ def test_extract_tool_calls_streaming_multiple(parser: ToolParser) -> 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 = (
|
||||
'<function name="write_file">'
|
||||
'<param name="content"><![CDATA[ def foo():\n pass\n]]></param>'
|
||||
"</function>\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 = [
|
||||
'<function name="write_file">',
|
||||
'<param name="content"><![CDATA[ def foo():\n',
|
||||
" pass\n]]></param></function>\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"
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
[
|
||||
'<minimax:tool_call><invoke name="echo">'
|
||||
'<parameter name="msg"> hi </parameter>'
|
||||
"</invoke></minimax:tool_call>",
|
||||
],
|
||||
)
|
||||
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 </parameter> arrives, i.e. the partial path."""
|
||||
results = _feed(
|
||||
parser,
|
||||
[
|
||||
'<minimax:tool_call><invoke name="write">'
|
||||
'<parameter name="content"> def foo():\n',
|
||||
" return 1\n",
|
||||
"</parameter></invoke></minimax:tool_call>",
|
||||
],
|
||||
)
|
||||
tc = _collect_tool_calls(results)
|
||||
assert len(tc) == 1
|
||||
assert json.loads(tc[0]["arguments"]) == {
|
||||
"content": " def foo():\n return 1\n"
|
||||
}
|
||||
|
||||
@@ -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 = (
|
||||
"<tool_call>\n<function=write_file>\n"
|
||||
"<parameter=content>\n"
|
||||
" def foo():\n return 1\n\n"
|
||||
"</parameter>\n</function>\n</tool_call>"
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
@@ -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 `</parameter>`, 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)
|
||||
|
||||
|
||||
+11
-2
@@ -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)
|
||||
|
||||
|
||||
@@ -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("<![CDATA[") and val_text.endswith("]]>"):
|
||||
val_text = val_text[len("<![CDATA[") : -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("<![CDATA[") and val_text.endswith("]]>"):
|
||||
val_text = val_text[len("<![CDATA[") : -len("]]>")]
|
||||
val_text = val_text.strip()
|
||||
else:
|
||||
val_text = val_text.strip()
|
||||
_add_argument(
|
||||
func_name,
|
||||
key,
|
||||
|
||||
Reference in New Issue
Block a user