[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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user