[Model][Frontend] Support DeepSeek-V4.1-Flash in Rust and Python frontends (#56208)

Signed-off-by: Bugen Zhao <i@bugenzhao.com>
Co-authored-by: Isotr0py <Isotr0py@outlook.com>
This commit is contained in:
Bugen Zhao
2026-09-10 01:15:06 -07:00
committed by GitHub
co-authored by Isotr0py
parent 9521c60bdc
commit b47b01cf33
52 changed files with 2986 additions and 113 deletions
+2
View File
@@ -53,6 +53,8 @@ pub type Step3ReasoningParser = DeepSeekR1ReasoningParser;
/// DeepSeek V4 currently shares the DeepSeek V3 reasoning parser.
pub type DeepSeekV4ReasoningParser = DeepSeekV3ReasoningParser;
/// DeepSeek V4.1 uses bare `<think>...</think>` delimiters.
pub type DeepSeekV41ReasoningParser = DeepSeekV3ReasoningParser;
/// GLM-4.7 and GLM-5 use bare reasoning delimiters.
pub type Glm47ReasoningParser = DeepSeekV3ReasoningParser;
/// Kimi K2 currently shares the DeepSeek V3 reasoning parser.
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright contributors to the vLLM project
use super::{DeepSeekDsmlToolParser, DsmlTokens};
use crate::tool::{Result, StructuralTagBuilder, Tool, ToolParser, ToolParserOutput};
mod structural_tag;
/// Tool parser for DeepSeek V4.1's spaced DSML tags.
///
/// Arguments are emitted only after a full `invoke` block is parsed.
pub struct DeepSeekV41ToolParser(DeepSeekDsmlToolParser);
impl ToolParser for DeepSeekV41ToolParser {
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
where
Self: Sized + 'static,
{
Ok(Box::new(Self(DeepSeekDsmlToolParser::new(
tools,
DsmlTokens::V41,
))))
}
fn preserve_special_tokens(&self) -> bool {
true
}
fn structural_tag_builder(&self) -> Option<&dyn StructuralTagBuilder> {
Some(&structural_tag::DeepSeekV41StructuralTagBuilder)
}
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
self.0.parse_into(chunk, output)
}
fn finish(&mut self) -> Result<ToolParserOutput> {
self.0.finish()
}
fn reset(&mut self) -> String {
self.0.reset()
}
}
#[cfg(test)]
mod tests;
@@ -0,0 +1,108 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright contributors to the vLLM project
//! Visible-text grammar for DeepSeek V4.1 tool calls.
//!
//! Adapted from xgrammar-structural-tag 0.2.0's DeepSeek V4 builder, with
//! V4.1's spaced DSML tags and schema-unconstrained parameter values.
use serde_json::Value;
use xgrammar_structural_tag::Result;
use xgrammar_structural_tag::builders::{
StructuralTagBuilder, StructuralTagContext, StructuralTagOptions,
};
use xgrammar_structural_tag::format::{
Format, JsonSchemaFormat, StructuralTag, TagFormat, TriggeredTagsFormat,
};
use xgrammar_structural_tag::tool::{BuilderToolChoice, FunctionToolParam};
use super::DsmlTokens;
const TOKENS: DsmlTokens = DsmlTokens::V41;
pub(super) struct DeepSeekV41StructuralTagBuilder;
impl StructuralTagBuilder for DeepSeekV41StructuralTagBuilder {
fn build(&self, ctx: StructuralTagContext<'_>) -> Result<StructuralTag> {
let tags = ctx
.function_tools
.iter()
.map(|tool| call_tag(tool, ctx.options))
.collect::<Vec<_>>();
let excludes = if ctx.options.exclude_special_tokens {
&["<think>", "</think>"][..]
} else {
&[]
};
let format = match ctx.tool_choice {
BuilderToolChoice::Auto if tags.is_empty() => Format::any_text_excluding(excludes),
BuilderToolChoice::Auto => Format::TriggeredTags(
TriggeredTagsFormat::new(
&[TOKENS.tool_calls_start],
vec![TagFormat::new(
format!("{}\n", TOKENS.tool_calls_start),
Format::tags_with_separator(tags, "", true, false),
TOKENS.tool_calls_end,
)],
)
.with_excludes(excludes),
),
BuilderToolChoice::Forced | BuilderToolChoice::Required => Format::sequence(vec![
Format::const_string(format!("\n\n{}\n", TOKENS.tool_calls_start)),
Format::tags_with_separator(
tags,
"",
true,
ctx.tool_choice == BuilderToolChoice::Forced,
),
Format::const_string(TOKENS.tool_calls_end),
]),
};
// Reasoning boundaries remain owned by the engine reasoner; serving
// calls this visible-text builder with reasoning=false.
Ok(StructuralTag::new(format))
}
}
fn call_tag(tool: &FunctionToolParam, options: StructuralTagOptions) -> TagFormat {
// TODO: enforce tool.parameters and strict schema semantics once V4.1
// argument conversion is available. This builder intentionally ignores
// parameters and strict: it constrains tool names and DSML/value syntax
// only. Parameter names, presence, uniqueness and value schemas remain
// unconstrained, including for strict=true. Serving still uses strict to
// decide whether auto tool choice activates a grammar.
TagFormat::new(
format!("{} name=\"{}\">\n", TOKENS.invoke_start, tool.function.name),
Format::star(parameter(options)),
format!("{}\n", TOKENS.invoke_end),
)
}
fn parameter(options: StructuralTagOptions) -> Format {
Format::tag(
format!("{} name=\"", TOKENS.parameter_start),
Format::sequence(vec![
Format::regex(r#"[^"]+"#),
Format::const_string("\" string=\""),
Format::or(vec![
Format::sequence(vec![
Format::const_string("true\">"),
Format::any_text_excluding(&[
TOKENS.parameter_end,
TOKENS.invoke_end,
TOKENS.tool_calls_end,
]),
]),
Format::sequence(vec![
Format::const_string("false\">"),
Format::JsonSchema(
JsonSchemaFormat::new(Value::Bool(true))
.with_any_order(options.any_order)
.with_max_whitespace_cnt(options.max_whitespace_cnt),
),
]),
]),
]),
format!("{}\n", TOKENS.parameter_end),
)
}
@@ -0,0 +1,113 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright contributors to the vLLM project
use expect_test::expect;
use serde_json::json;
use xgrammar_structural_tag::builders::StructuralTagOptions;
use xgrammar_structural_tag::{
FunctionDefinition, FunctionToolParam, ToolChoice, ToolParam, build_structural_tag,
};
use super::DeepSeekV41ToolParser;
use crate::tool::test_utils::collect_stream;
use crate::tool::{Tool, ToolParser};
fn tools() -> Vec<Tool> {
vec![Tool {
name: "lookup".into(),
description: None,
parameters: json!({
"type": "object",
"properties": {
"query": {"type": "string"},
"limit": {"type": "integer"},
"enabled": {"type": "boolean"},
"data": {"type": "object"},
"items": {"type": "array"},
"label": {"type": "string"}
}
}),
strict: None,
}]
}
#[test]
fn spaced_dsml_streaming_preserves_shared_schema_coercion() {
let wire = concat!(
"summary\n\n<|DSML| calls>\n",
"<|DSML| invoke name=\"lookup\">\n",
"<|DSML| parameter name=\"query\" string=\"true\"> value\n</|DSML| parameter>\n",
"<|DSML| parameter name=\"limit\" string=\"false\">2</|DSML| parameter>\n",
"<|DSML| parameter name=\"enabled\" string=\"false\">true</|DSML| parameter>\n",
"<|DSML| parameter name=\"data\" string=\"false\">{\"x\":1}</|DSML| parameter>\n",
"<|DSML| parameter name=\"items\" string=\"false\">[1,2]</|DSML| parameter>\n",
"<|DSML| parameter name=\"label\" string=\"false\">42</|DSML| parameter>\n",
"<|DSML| parameter name=\"unknown\" string=\"false\">42</|DSML| parameter>\n",
"<|DSML| parameter name=\"nullish\" string=\"false\">null</|DSML| parameter>\n",
"</|DSML| invoke>\n",
"<|DSML| invoke name=\"lookup\">\n",
"<|DSML| parameter name=\"query\" string=\"true\">second</|DSML| parameter>\n",
"</|DSML| invoke>\n</|DSML| calls>",
);
let expected = [
json!({"query":" value\n","limit":2,"enabled":true,"data":{"x":1},"items":[1,2],"label":"42","unknown":"42","nullish":null}),
json!({"query":"second"}),
];
for split in wire.char_indices().map(|(i, _)| i).chain(std::iter::once(wire.len())) {
let mut parser = DeepSeekV41ToolParser::create(&tools()).unwrap();
let output = collect_stream(parser.as_mut(), &[&wire[..split], &wire[split..]]);
// Protocol framing follows the current shared DSML behavior.
assert_eq!(output.normal_text(), "summary", "split {split}");
assert_eq!(output.calls().len(), 2, "split {split}");
for (index, call) in output.calls().iter().enumerate() {
assert_eq!(call.tool_index, index, "split {split}");
assert_eq!(call.name.as_deref(), Some("lookup"), "split {split}");
assert_eq!(
serde_json::from_str::<serde_json::Value>(&call.arguments).unwrap(),
expected[index],
"split {split}"
);
}
}
}
#[test]
fn v4_tags_remain_text_in_v41_dialect() {
let mut parser = DeepSeekV41ToolParser::create(&tools()).unwrap();
let output = collect_stream(parser.as_mut(), &["before<|DSML|tool_calls>after"]);
expect!["before<|DSML|tool_calls>after"].assert_eq(&output.normal_text());
assert!(output.calls().is_empty());
}
#[test]
fn structural_tag_ignores_parameter_schemas_and_strict() {
let parser = DeepSeekV41ToolParser::create(&tools()).unwrap();
let builder = parser.structural_tag_builder().unwrap();
let options = StructuralTagOptions::default().with_reasoning(false);
for choice in [
ToolChoice::auto(),
ToolChoice::required(),
ToolChoice::function("lookup"),
] {
let build = |function| {
build_structural_tag(
builder,
&[ToolParam::Function(FunctionToolParam::new(function))],
choice.clone(),
options,
)
.unwrap()
};
let expected = build(FunctionDefinition::new("lookup"));
for strict in [None, Some(false), Some(true)] {
for parameters in [
json!(false),
json!({"type":"object", "properties":{"query":{"const":"fixed"}}, "required":["query"]}),
] {
let mut function = FunctionDefinition::new("lookup").with_parameters(parameters);
function.strict = strict;
assert_eq!(build(function), expected);
}
}
}
}
+40 -16
View File
@@ -14,14 +14,11 @@ use crate::tool::Tool;
mod deepseek_v32;
mod deepseek_v4;
mod deepseek_v41;
pub use deepseek_v4::DeepSeekV4ToolParser;
pub use deepseek_v32::DeepSeekV32ToolParser;
const INVOKE_START: &str = "<|DSML|invoke";
const INVOKE_END: &str = "</|DSML|invoke>";
const PARAMETER_START: &str = "<|DSML|parameter";
const PARAMETER_END: &str = "</|DSML|parameter>";
pub use deepseek_v41::DeepSeekV41ToolParser;
type DsmlInput<'i> = Partial<&'i str>;
@@ -30,6 +27,10 @@ struct DsmlTokens {
tool_calls_start: &'static str,
framed_tool_calls_start: &'static str,
tool_calls_end: &'static str,
invoke_start: &'static str,
invoke_end: &'static str,
parameter_start: &'static str,
parameter_end: &'static str,
}
impl DsmlTokens {
@@ -37,11 +38,25 @@ impl DsmlTokens {
tool_calls_start: "<|DSML|function_calls>",
framed_tool_calls_start: "\n\n<|DSML|function_calls>",
tool_calls_end: "</|DSML|function_calls>",
invoke_start: "<|DSML|invoke",
invoke_end: "</|DSML|invoke>",
parameter_start: "<|DSML|parameter",
parameter_end: "</|DSML|parameter>",
};
const V4: Self = Self {
tool_calls_start: "<|DSML|tool_calls>",
framed_tool_calls_start: "\n\n<|DSML|tool_calls>",
tool_calls_end: "</|DSML|tool_calls>",
..Self::V32
};
const V41: Self = Self {
tool_calls_start: "<|DSML| calls>",
framed_tool_calls_start: "\n\n<|DSML| calls>",
tool_calls_end: "</|DSML| calls>",
invoke_start: "<|DSML| invoke",
invoke_end: "</|DSML| invoke>",
parameter_start: "<|DSML| parameter",
parameter_end: "</|DSML| parameter>",
};
}
@@ -205,7 +220,7 @@ fn parse_tool_block_event(
) -> ModalResult<DsmlEvent> {
ws0.void().parse_next(input)?;
alt((
|input: &mut DsmlInput<'_>| invoke_event(input, invoke_end_scan),
|input: &mut DsmlInput<'_>| invoke_event(input, tokens, invoke_end_scan),
|input: &mut DsmlInput<'_>| tool_calls_end_event(input, tokens),
))
.parse_next(input)
@@ -243,19 +258,20 @@ fn safe_text_event(input: &mut DsmlInput<'_>, tokens: DsmlTokens) -> ModalResult
/// Parse a DSML invoke block.
fn invoke_event(
input: &mut DsmlInput<'_>,
tokens: DsmlTokens,
invoke_end_scan: &mut MarkerScanState,
) -> ModalResult<DsmlEvent> {
let (name, body) = seq!(
_: literal(INVOKE_START),
_: literal(tokens.invoke_start),
_: ws1,
dsml_name_attr,
_: ws0,
_: ">",
take_until_marker(INVOKE_END, invoke_end_scan),
_: literal(INVOKE_END),
take_until_marker(tokens.invoke_end, invoke_end_scan),
_: literal(tokens.invoke_end),
)
.parse_next(input)?;
let raw_params = parse_invoke_params(body)?;
let raw_params = parse_invoke_params(body, tokens)?;
Ok(DsmlEvent::Invoke {
name: name.to_string(),
raw_params,
@@ -263,23 +279,31 @@ fn invoke_event(
}
/// Parse a DSML invoke body.
fn parse_invoke_params(invoke_body: &str) -> ModalResult<Vec<DsmlParameter>> {
fn parse_invoke_params(invoke_body: &str, tokens: DsmlTokens) -> ModalResult<Vec<DsmlParameter>> {
let mut input = invoke_body;
delimited(ws0, repeat(0.., terminated(parse_parameter, ws0)), eof).parse_next(&mut input)
delimited(
ws0,
repeat(
0..,
terminated(|input: &mut &str| parse_parameter(input, tokens), ws0),
),
eof,
)
.parse_next(&mut input)
}
/// Parse a DSML parameter block.
fn parse_parameter(input: &mut &str) -> ModalResult<DsmlParameter> {
fn parse_parameter(input: &mut &str, tokens: DsmlTokens) -> ModalResult<DsmlParameter> {
seq! {DsmlParameter {
_: literal(PARAMETER_START),
_: literal(tokens.parameter_start),
_: ws1,
name: name_attr.map(|name: &str| name.to_string()),
_: ws1,
is_string: string_attr.map(|value| value == "true"),
_: ws0,
_: ">",
value: take_until(0.., PARAMETER_END).map(str::to_string),
_: literal(PARAMETER_END),
value: take_until(0.., tokens.parameter_end).map(str::to_string),
_: literal(tokens.parameter_end),
}}
.parse_next(input)
}
+1 -1
View File
@@ -20,7 +20,7 @@ mod seed_oss;
pub mod test_utils;
use std::collections::{BTreeMap, btree_map};
pub use deepseek_dsml::{DeepSeekV4ToolParser, DeepSeekV32ToolParser};
pub use deepseek_dsml::{DeepSeekV4ToolParser, DeepSeekV32ToolParser, DeepSeekV41ToolParser};
pub use deepseek_json::{DeepSeekV3ToolParser, DeepSeekV31ToolParser};
pub use error::{Result, ToolParserError};
pub use glm_xml::{Glm45MoeToolParser, Glm47MoeToolParser};