[Bugfix][Parser] Recover genuine tool calls after unclosed Qwen3 <function= opener in prose (#59928)

Signed-off-by: Abhay Joshi <105213625+abhayjoshi201@users.noreply.github.com>
Signed-off-by: Artem Perevedentsev <aperevedents@nvidia.com>
Co-authored-by: Gemini <noreply@google.com>
Co-authored-by: Artem Perevedentsev <aperevedents@nvidia.com>
This commit is contained in:
Abhay Joshi
2026-10-14 13:01:25 +03:00
committed by GitHub
co-authored by Gemini Artem Perevedentsev
parent 8a8e2baa02
commit 3f466c852c
2 changed files with 137 additions and 0 deletions
+110
View File
@@ -274,6 +274,53 @@ class TestNonStreaming:
' "multiSelect": false, "answer": null}]',
}
@pytest.mark.parametrize(
"unclosed_func",
["<function=function_name", "<function=function_name>"],
)
def test_regression_issue_58227(self, parser, mock_request, unclosed_func):
"""Regression issue 58227
Check cases:
* <function=...<tool_call>
* <function=abc>...<function=
"""
text = (
f"Use <tool_call>\n{unclosed_func}\n"
"Wait, let me call the real tool via <tool_call>:\n"
"<tool_call>\n"
"<function=get_weather>\n"
"<parameter=city>Tokyo</parameter>\n"
"</function>\n"
"</tool_call>"
)
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 <tool_call> tag is ok inside parameter value
"""
text = (
"<tool_call>\n"
"<function=write_file>\n"
"<parameter=content>Document <tool_call> usage</parameter>\n"
"</function>\n"
"</tool_call>"
)
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 <tool_call> 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",
["<function=function_name", "<function=function_name>"],
)
def test_streaming_regression_issue_58227(
self, parser, mock_request, unclosed_func
):
"""Regression issue 58227
Check cases:
* <function=...<tool_call>
* <function=abc>...<function=
"""
chunks = [
"Use <tool_call>\n",
f"{unclosed_func}\n",
"Wait, let me call the real tool via ",
"<tool_call>",
":\n",
"<tool_call>\n",
"<function=get_weather>\n",
"<parameter=city>Tokyo</parameter>\n",
"</function>\n",
"</tool_call>",
]
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 <tool_call> tag is ok inside parameter value
"""
chunks = [
"<tool_call>\n",
"<function=write_file>\n",
"<parameter=content>Document ",
"<tool_call>",
" usage</parameter>\n",
"</function>\n",
"</tool_call>",
]
results = simulate_tool_streaming(parser, mock_request, chunks)
assert collect_function_name(results) == "write_file"
assert json.loads(collect_tool_arguments(results)) == {
"content": "Document <tool_call> usage",
}
class TestArgConverter:
"""Direct tests for the Qwen3 arg_converter with multi-line values."""