[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:
co-authored by
Gemini
Artem Perevedentsev
parent
8a8e2baa02
commit
3f466c852c
@@ -274,6 +274,53 @@ class TestNonStreaming:
|
|||||||
' "multiSelect": false, "answer": null}]',
|
' "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:
|
class TestStreaming:
|
||||||
def test_basic_streaming(self, parser, mock_request):
|
def test_basic_streaming(self, parser, mock_request):
|
||||||
@@ -635,6 +682,69 @@ class TestStreaming:
|
|||||||
assert "Bash" in names
|
assert "Bash" in names
|
||||||
assert "Read" 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:
|
class TestArgConverter:
|
||||||
"""Direct tests for the Qwen3 arg_converter with multi-line values."""
|
"""Direct tests for the Qwen3 arg_converter with multi-line values."""
|
||||||
|
|||||||
@@ -169,10 +169,37 @@ def qwen3_config(
|
|||||||
ParserState.TOOL_BETWEEN,
|
ParserState.TOOL_BETWEEN,
|
||||||
(EventType.TOOL_CALL_END,),
|
(EventType.TOOL_CALL_END,),
|
||||||
),
|
),
|
||||||
|
# regression issue 58227
|
||||||
|
# <function=...</tool_call>
|
||||||
|
# function name is not closed, go to content
|
||||||
|
(ParserState.TOOL_NAME, "TOOL_END"): Transition(
|
||||||
|
ParserState.CONTENT,
|
||||||
|
(EventType.TOOL_CALL_END,),
|
||||||
|
),
|
||||||
|
# regression issue 58227
|
||||||
|
# <function=...<tool_call>
|
||||||
|
# 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
|
||||||
|
# <function=...<function=
|
||||||
|
# unclosed function name followed by another function name
|
||||||
|
(ParserState.TOOL_NAME, "FUNC_PREFIX"): Transition(
|
||||||
|
ParserState.TOOL_NAME,
|
||||||
|
(EventType.TOOL_CALL_END, EventType.TOOL_CALL_START),
|
||||||
|
),
|
||||||
(ParserState.TOOL_ARGS, "FUNC_END"): Transition(
|
(ParserState.TOOL_ARGS, "FUNC_END"): Transition(
|
||||||
ParserState.TOOL_BETWEEN,
|
ParserState.TOOL_BETWEEN,
|
||||||
(EventType.TOOL_CALL_END,),
|
(EventType.TOOL_CALL_END,),
|
||||||
),
|
),
|
||||||
|
# regression issue 58227
|
||||||
|
# <function=abc>...<function=
|
||||||
|
(ParserState.TOOL_ARGS, "FUNC_PREFIX"): Transition(
|
||||||
|
ParserState.TOOL_NAME,
|
||||||
|
(EventType.TOOL_CALL_END, EventType.TOOL_CALL_START),
|
||||||
|
),
|
||||||
(ParserState.TOOL_ARGS, "PARAM_START"): Transition(
|
(ParserState.TOOL_ARGS, "PARAM_START"): Transition(
|
||||||
ParserState.TOOL_ARGS,
|
ParserState.TOOL_ARGS,
|
||||||
(EventType.ARG_VALUE_CHUNK,),
|
(EventType.ARG_VALUE_CHUNK,),
|
||||||
|
|||||||
Reference in New Issue
Block a user