Skip to content

Commit 38d2c3a

Browse files
authored
[None][fix] DeepSeek DSML tool parsers: keep string parameter values as written (#21025)
1 parent af32051 commit 38d2c3a

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎tensorrt_llm/serve/tool_parser/deepseekv32_parser.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ def _parse_parameters_from_xml(self, invoke_content: str) -> dict:
110110
for param_name, param_type, param_value in param_matches:
111111
# Convert value based on type
112112
if param_type == "true": # string type
113-
parameters[param_name] = param_value.strip()
113+
# Taken as written: the encoder renders a string value verbatim
114+
# between the tags, so its whitespace (a file's last newline, a
115+
# line's indentation) is part of the value.
116+
parameters[param_name] = param_value
114117
else:
115118
# Try to parse as JSON for other types
116119
try:

‎tests/unittest/llmapi/apps/test_tool_parsers.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,6 +3160,53 @@ def test_deepseek_streaming_keeps_markdown_fences(
31603160
assert parser_cls().detect_and_parse(text, sample_tools).normal_text == text
31613161

31623162

3163+
def _dsml_tool_call(tag_prefix: str, block_name: str, name: str,
3164+
string_arguments: dict[str, str]) -> str:
3165+
"""A tool call as the DSML encoders render it: each string value verbatim."""
3166+
parameters = "\n".join(
3167+
f'<{tag_prefix}parameter name="{key}" string="true">{value}'
3168+
f"</{tag_prefix}parameter>" for key, value in string_arguments.items())
3169+
return (f"<{tag_prefix}{block_name}>\n"
3170+
f'<{tag_prefix}invoke name="{name}">\n{parameters}\n'
3171+
f"</{tag_prefix}invoke>\n</{tag_prefix}{block_name}>")
3172+
3173+
3174+
@pytest.mark.parametrize("parser_cls,tag_prefix,block_name", [
3175+
(DeepSeekV32Parser, "|DSML|", "function_calls"),
3176+
(DeepSeekV4Parser, "|DSML|", "tool_calls"),
3177+
(DeepSeekV41Parser, "|DSML| ", "calls"),
3178+
])
3179+
@pytest.mark.parametrize("streaming", [False, True])
3180+
def test_dsml_string_parameter_values_are_kept_as_written(
3181+
sample_tools: list[ChatCompletionToolsParam],
3182+
parser_cls: type[BaseToolParser], tag_prefix: str, block_name: str,
3183+
streaming: bool) -> None:
3184+
"""A string value's leading and trailing whitespace is part of the value.
3185+
3186+
An edit's ``old_string`` must match the file including its indentation,
3187+
and a written file keeps its last newline.
3188+
"""
3189+
arguments = {
3190+
"location": " indented line\n second line",
3191+
"unit": "file content\n",
3192+
}
3193+
text = _dsml_tool_call(tag_prefix, block_name, "get_weather", arguments)
3194+
3195+
parser = parser_cls()
3196+
if streaming:
3197+
results = [
3198+
parser.parse_streaming_increment(text[i:i + 7], sample_tools)
3199+
for i in range(0, len(text), 7)
3200+
]
3201+
calls = [call for result in results for call in result.calls]
3202+
else:
3203+
calls = parser.detect_and_parse(text, sample_tools).calls
3204+
3205+
assert [call.name for call in calls if call.name] == ["get_weather"]
3206+
assert json.loads("".join(call.parameters or ""
3207+
for call in calls)) == arguments
3208+
3209+
31633210
@pytest.mark.parametrize(
31643211
"parser_cls, tool_call_text",
31653212
[

0 commit comments

Comments
 (0)