[Frontend] Use XGrammar schema constraints for DeepSeek V4.1 (#56408)

Signed-off-by: Ubospica <ubospica@gmail.com>
Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
Yixin Dong
2026-09-15 04:36:16 -04:00
committed by GitHub
co-authored by Flora Feng
parent 676650397f
commit 2c2cbd84fd
2 changed files with 90 additions and 4 deletions
+13 -1
View File
@@ -10,6 +10,7 @@ from openai.types.responses.tool import Tool as ResponsesTool
from openai.types.responses.tool_choice_allowed import ToolChoiceAllowed
from openai.types.responses.tool_choice_function import ToolChoiceFunction
from xgrammar import StructuralTag, normalize_tool_choice
from xgrammar import builtin_structural_tag as xgrammar_builtin_structural_tag
from xgrammar import get_model_structural_tag as get_xgrammar_model_structural_tag
from xgrammar.openai_tool_call_schema import (
BuiltinToolParam,
@@ -242,9 +243,20 @@ def get_deepseek_v41_structural_tag(
token_suffix: str = "",
) -> StructuralTag:
# Serving enables this visible-text grammar after the reasoning boundary.
builder = getattr(
xgrammar_builtin_structural_tag, "get_deepseek_v4_1_structural_tag", None
)
if builder is not None:
return builder(
tools=tools,
builtin_tools=builtin_tools,
tool_choice=tool_choice,
reasoning="disabled",
)
del builtin_tools, reasoning, token_suffix
# TODO: lower parameter schemas into DSML constraints. This builder constrains
# Compatibility with xgrammar releases without the V4.1 builtin. This constrains
# tool names and DSML/value syntax only. Parameter names, presence, uniqueness
# and value schemas remain unconstrained, including for strict=true. The request
# layer still uses strict to decide whether auto tool choice activates a grammar.