[Frontend] Fix the parsing of missing string= in DeepSeek V4 (#56271)
Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Bugen Zhao <i@bugenzhao.com> Signed-off-by: lazymio <mio@lazym.io>
This commit is contained in:
co-authored by
Claude Opus 5
Bugen Zhao
parent
dee37d8911
commit
39e33db7f3
@@ -193,6 +193,34 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_parse_complete_keeps_parameter_without_string_attr() {
|
||||
// The model sometimes omits `string="..."`; the parameter must still
|
||||
// be kept and converted through the schema, like `string="false"`.
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
"<|DSML|function_calls>\n\
|
||||
<|DSML|invoke name=\"convert\">\n\
|
||||
<|DSML|parameter name=\"whole\">5.0</|DSML|parameter>\n\
|
||||
<|DSML|parameter name=\"flag\" >true</|DSML|parameter>\n\
|
||||
<|DSML|parameter name=\"payload\" string=\"true\">{\"nested\":true}</|DSML|parameter>\n\
|
||||
</|DSML|invoke>\n\
|
||||
</|DSML|function_calls>",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls().len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls()[0].arguments).unwrap(),
|
||||
json!({
|
||||
"whole": 5.0,
|
||||
"flag": true,
|
||||
"payload": "{\"nested\":true}",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_parse_complete_preserves_raw_closing_tag_text_in_parameter_value() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
|
||||
@@ -147,6 +147,29 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v4_streaming_keeps_parameter_without_string_attr() {
|
||||
let mut parser = DeepSeekV4ToolParser::new(&test_tools());
|
||||
let output = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"<|DSML|tool_calls>\n<|DSML|invoke name=\"get_weather\">\n",
|
||||
"<|DSML|parameter name=\"loc",
|
||||
"ation\">Par",
|
||||
"is</|DSML|parameter>\n",
|
||||
"<|DSML|parameter name=\"date\" string=\"true\">tomorrow</|DSML|parameter>\n",
|
||||
"</|DSML|invoke>\n</|DSML|tool_calls>",
|
||||
],
|
||||
);
|
||||
|
||||
assert!(output.normal_text().is_empty());
|
||||
assert_eq!(output.calls().len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls()[0].arguments).unwrap(),
|
||||
json!({ "location": "Paris", "date": "tomorrow" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_framing_preserves_body_whitespace_across_chunk_boundaries() {
|
||||
assert_tool_framing_preserves_body_whitespace::<DeepSeekV4ToolParser>(
|
||||
|
||||
@@ -71,6 +71,28 @@ fn spaced_dsml_streaming_preserves_shared_schema_coercion() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spaced_dsml_keeps_parameter_without_string_attr() {
|
||||
let wire = concat!(
|
||||
"<|DSML| calls>\n",
|
||||
"<|DSML| invoke name=\"lookup\">\n",
|
||||
"<|DSML| parameter name=\"query\">value</|DSML| parameter>\n",
|
||||
"<|DSML| parameter name=\"limit\">2</|DSML| parameter>\n",
|
||||
"</|DSML| invoke>\n",
|
||||
"</|DSML| calls>",
|
||||
);
|
||||
let mut parser = DeepSeekV41ToolParser::create(&tools()).unwrap();
|
||||
|
||||
let output = collect_stream(parser.as_mut(), &[wire]);
|
||||
|
||||
assert!(output.normal_text().is_empty());
|
||||
assert_eq!(output.calls().len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<serde_json::Value>(&output.calls()[0].arguments).unwrap(),
|
||||
json!({ "query": "value", "limit": 2 })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v4_tags_remain_text_in_v41_dialect() {
|
||||
let mut parser = DeepSeekV41ToolParser::create(&tools()).unwrap();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
use winnow::ascii::{multispace0 as ws0, multispace1 as ws1};
|
||||
use winnow::combinator::{alt, delimited, eof, repeat, seq, terminated};
|
||||
use winnow::combinator::{alt, delimited, eof, opt, preceded, repeat, seq, terminated};
|
||||
use winnow::prelude::*;
|
||||
use winnow::stream::Partial;
|
||||
use winnow::token::{literal, rest, take_until};
|
||||
@@ -293,13 +293,16 @@ fn parse_invoke_params(invoke_body: &str, tokens: DsmlTokens) -> ModalResult<Vec
|
||||
}
|
||||
|
||||
/// Parse a DSML parameter block.
|
||||
///
|
||||
/// The `string` attribute is optional: the model sometimes omits it, and
|
||||
/// failing the whole invoke would leak the block into the text output. A
|
||||
/// missing or non-`"true"` value is treated like `string="false"`.
|
||||
fn parse_parameter(input: &mut &str, tokens: DsmlTokens) -> ModalResult<DsmlParameter> {
|
||||
seq! {DsmlParameter {
|
||||
_: literal(tokens.parameter_start),
|
||||
_: ws1,
|
||||
name: name_attr.map(|name: &str| name.to_string()),
|
||||
_: ws1,
|
||||
is_string: string_attr.map(|value| value == "true"),
|
||||
is_string: opt(preceded(ws1, string_attr)).map(|value| value == Some("true")),
|
||||
_: ws0,
|
||||
_: ">",
|
||||
value: take_until(0.., tokens.parameter_end).map(str::to_string),
|
||||
@@ -315,7 +318,7 @@ fn name_attr<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
|
||||
/// Parse a string attribute.
|
||||
fn string_attr<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
delimited("string=\"", alt(("true", "false")), "\"").parse_next(input)
|
||||
delimited("string=\"", take_until(0.., "\""), "\"").parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a DSML name attribute.
|
||||
|
||||
Reference in New Issue
Block a user