diff --git a/python/sglang/srt/entrypoints/http_server.py b/python/sglang/srt/entrypoints/http_server.py
index 40db7569f5..86457b4bc7 100644
--- a/python/sglang/srt/entrypoints/http_server.py
+++ b/python/sglang/srt/entrypoints/http_server.py
@@ -91,6 +91,7 @@ from sglang.srt.entrypoints.ollama.protocol import (
OllamaShowRequest,
)
from sglang.srt.entrypoints.ollama.serving import OllamaServing
+from sglang.srt.entrypoints.openai.chat_encoding_adapters import native_text_parser
from sglang.srt.entrypoints.openai.protocol import (
ChatCompletionRequest,
ClassifyRequest,
@@ -1716,15 +1717,27 @@ async def parse_function_call_request(
"""
A native API endpoint to parse function calls from a text.
"""
- # 1) Initialize the parser based on the request body
- parser = FunctionCallParser(
- tools=obj.tools,
- tool_call_parser=obj.tool_call_parser,
- tokenizer=get_global_state().tokenizer_manager.tokenizer,
- )
+ chat = request.app.state.openai_serving_chat
+ try:
+ text_parser = native_text_parser(
+ obj.tool_call_parser,
+ chat_encoding_spec=chat.chat_encoding_spec,
+ adapter=chat.encoding_adapter,
+ )
+ except ValueError as error:
+ raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(error))
+ if text_parser is not None:
+ normal_text, calls = text_parser.parse_function_call_text(obj.text, obj.tools)
+ else:
+ # 1) Initialize the parser based on the request body
+ parser = FunctionCallParser(
+ tools=obj.tools,
+ tool_call_parser=obj.tool_call_parser,
+ tokenizer=get_global_state().tokenizer_manager.tokenizer,
+ )
- # 2) Call the non-stream parsing method (non-stream)
- normal_text, calls = parser.parse_non_stream(obj.text)
+ # 2) Call the non-stream parsing method (non-stream)
+ normal_text, calls = parser.parse_non_stream(obj.text)
# 3) Organize the response content
response_data = {
@@ -1744,22 +1757,38 @@ async def separate_reasoning_request(
"""
A native API endpoint to separate reasoning from a text.
"""
- # 1) Initialize the parser based on the request body
- parser = ReasoningParser(
- model_type=obj.reasoning_parser,
- request=request,
- tokenizer=get_global_state().tokenizer_manager.tokenizer,
- )
-
- # 2) Call the non-stream parsing method (non-stream)
- if obj.return_blocks:
- blocks = parser.parse_non_stream_blocks(obj.text)
- reasoning_blocks = [b["text"] for b in blocks if b["type"] == "reasoning"]
- text_blocks = [b["text"] for b in blocks if b["type"] == "text"]
- reasoning_text = "".join(reasoning_blocks)
- normal_text = "".join(text_blocks)
+ chat = request.app.state.openai_serving_chat
+ try:
+ text_parser = native_text_parser(
+ obj.reasoning_parser,
+ chat_encoding_spec=chat.chat_encoding_spec,
+ adapter=chat.encoding_adapter,
+ )
+ except ValueError as error:
+ raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(error))
+ if text_parser is not None:
+ reasoning_text, normal_text = text_parser.separate_reasoning_text(obj.text)
+ blocks = (
+ [{"type": "reasoning", "text": reasoning_text}] if reasoning_text else []
+ )
+ blocks.append({"type": "text", "text": normal_text})
else:
- reasoning_text, normal_text = parser.parse_non_stream(obj.text)
+ # 1) Initialize the parser based on the request body
+ parser = ReasoningParser(
+ model_type=obj.reasoning_parser,
+ request=request,
+ tokenizer=get_global_state().tokenizer_manager.tokenizer,
+ )
+
+ # 2) Call the non-stream parsing method (non-stream)
+ if obj.return_blocks:
+ blocks = parser.parse_non_stream_blocks(obj.text)
+ reasoning_text = "".join(
+ b["text"] for b in blocks if b["type"] == "reasoning"
+ )
+ normal_text = "".join(b["text"] for b in blocks if b["type"] == "text")
+ else:
+ reasoning_text, normal_text = parser.parse_non_stream(obj.text)
# 3) Organize the response content
response_data = {
@@ -1767,8 +1796,12 @@ async def separate_reasoning_request(
"text": normal_text,
}
if obj.return_blocks:
- response_data["reasoning_blocks"] = reasoning_blocks
- response_data["text_blocks"] = text_blocks
+ response_data["reasoning_blocks"] = [
+ b["text"] for b in blocks if b["type"] == "reasoning"
+ ]
+ response_data["text_blocks"] = [
+ b["text"] for b in blocks if b["type"] == "text"
+ ]
response_data["blocks"] = blocks
return ORJSONResponse(content=response_data, status_code=200)
diff --git a/python/sglang/srt/entrypoints/openai/chat_encoding_adapters.py b/python/sglang/srt/entrypoints/openai/chat_encoding_adapters.py
index 49e1cbacef..0cffe3bae9 100644
--- a/python/sglang/srt/entrypoints/openai/chat_encoding_adapters.py
+++ b/python/sglang/srt/entrypoints/openai/chat_encoding_adapters.py
@@ -11,6 +11,7 @@ if TYPE_CHECKING:
from sglang.srt.entrypoints.openai.protocol import (
ChatCompletionRequest,
ResponsesRequest,
+ Tool,
ToolCall,
)
from sglang.srt.function_call.core_types import ToolCallItem
@@ -106,6 +107,46 @@ class ChatEncodingAdapter(Protocol):
...
+class NativeTextParser(Protocol):
+ """The native /separate_reasoning and /parse_function_call endpoints, for a
+ parser whose output the served adapter parses."""
+
+ def separate_reasoning_text(self, text: str) -> tuple[str, str]: ...
+
+ def parse_function_call_text(
+ self, text: str, tools: list[Tool]
+ ) -> tuple[str, list[ToolCallItem]]: ...
+
+
+# The reasoning and tool-call parser names whose output the spec's adapter parses.
+ADAPTER_OUTPUT_PARSERS: dict[str, tuple[str, str]] = {
+ "dsv4": ("deepseek-v4", "deepseekv4"),
+ "dsv41": ("deepseek-v41", "deepseekv41"),
+}
+
+
+def native_text_parser(
+ parser_name: Optional[str],
+ *,
+ chat_encoding_spec: Optional[str],
+ adapter: Optional[ChatEncodingAdapter],
+) -> Optional[NativeTextParser]:
+ """The served adapter when it parses `parser_name` output; None for the text
+ parsers. A parser whose adapter is not served raises ValueError: the adapter
+ parses with the served checkpoint's tokenizer."""
+ owners = [
+ spec for spec, names in ADAPTER_OUTPUT_PARSERS.items() if parser_name in names
+ ]
+ if not owners:
+ return None
+ if adapter is None or chat_encoding_spec not in owners:
+ raise ValueError(
+ f"{parser_name!r} output is parsed with its model's tokenizer; this "
+ "server does not serve that model"
+ )
+ return adapter
+
+
def select_new_output_ids(
*,
output_ids: list[int],
diff --git a/python/sglang/srt/entrypoints/openai/serving_chat.py b/python/sglang/srt/entrypoints/openai/serving_chat.py
index 986badd29c..426cc1a916 100644
--- a/python/sglang/srt/entrypoints/openai/serving_chat.py
+++ b/python/sglang/srt/entrypoints/openai/serving_chat.py
@@ -478,6 +478,10 @@ class OpenAIServingChat(OpenAIServingBase):
tool_call_parser=self.tool_call_parser,
)
+ @property
+ def encoding_adapter(self) -> Optional[chat_encoding_adapters.ChatEncodingAdapter]:
+ return self._encoding_adapter
+
def _build_chat_encoding_adapter(
self,
) -> Optional[chat_encoding_adapters.ChatEncodingAdapter]:
diff --git a/python/sglang/srt/entrypoints/openai/serving_deepseek_recipe.py b/python/sglang/srt/entrypoints/openai/serving_deepseek_recipe.py
index e1dfa7f8c8..8bcf8b204e 100644
--- a/python/sglang/srt/entrypoints/openai/serving_deepseek_recipe.py
+++ b/python/sglang/srt/entrypoints/openai/serving_deepseek_recipe.py
@@ -8,6 +8,7 @@ from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, Optional, Union
from sglang.srt.entrypoints.openai.chat_encoding_adapters import (
+ ADAPTER_OUTPUT_PARSERS,
select_new_output_ids,
token_output_sse_chunks,
)
@@ -21,6 +22,7 @@ from sglang.srt.parser.deepseek_recipe_output import (
DeepSeekRecipeOutputParser,
RecipeToolCall,
RecipeToolCallDelta,
+ parse_output_text,
whole_tool_calls,
)
from sglang.srt.parser.deepseek_recipe_renderer import (
@@ -35,16 +37,13 @@ if TYPE_CHECKING:
from sglang.srt.entrypoints.openai.protocol import (
ChatCompletionRequest,
ResponsesRequest,
+ Tool,
)
DEFAULT_EFFORT_ENVS = {
"dsv4": envs.SGLANG_DSV4_REASONING_EFFORT,
"dsv41": envs.SGLANG_DSV41_REASONING_EFFORT,
}
-PARSERS = {
- "dsv4": ("deepseek-v4", "deepseekv4"),
- "dsv41": ("deepseek-v41", "deepseekv41"),
-}
JSON_RESPONSE_FORMATS = ("json_object", "json_schema")
@@ -77,7 +76,7 @@ class DeepSeekRecipeChatEncodingAdapter:
)
# recipe parses the output once a DeepSeek parser is configured; other
# parsers keep the text path.
- reasoning_name, tool_name = PARSERS[chat_encoding_spec]
+ reasoning_name, tool_name = ADAPTER_OUTPUT_PARSERS[chat_encoding_spec]
self._token_output = (
reasoning_parser == reasoning_name or tool_call_parser == tool_name
)
@@ -293,6 +292,30 @@ class DeepSeekRecipeChatEncodingAdapter:
)
)
+ def separate_reasoning_text(self, text: str) -> tuple[str, str]:
+ delta = parse_output_text(self.deepseek_recipe, text, parse_tool_calls=False)
+ return delta.reasoning, delta.content
+
+ def parse_function_call_text(
+ self, text: str, tools: list[Tool]
+ ) -> tuple[str, list[ToolCallItem]]:
+ if not tools:
+ return text, [] # as the text parsers: nothing to call
+ delta = parse_output_text(
+ self.deepseek_recipe, text, parse_tool_calls=True, thinking=False
+ )
+ # As in chat, a call to a tool outside `tools` is returned, at index -1.
+ indices = {tool.function.name: i for i, tool in enumerate(tools)}
+ calls = [
+ ToolCallItem(
+ tool_index=indices.get(call.name, -1),
+ name=call.name,
+ parameters=call.arguments,
+ )
+ for call in whole_tool_calls(delta)
+ ]
+ return delta.content, calls
+
def _new_output_parser(
self, request: ChatCompletionRequest
) -> DeepSeekRecipeOutputParser:
diff --git a/python/sglang/srt/function_call/deepseekv41_detector.py b/python/sglang/srt/function_call/deepseekv41_detector.py
index b6d7e1f595..681bdc22c6 100644
--- a/python/sglang/srt/function_call/deepseekv41_detector.py
+++ b/python/sglang/srt/function_call/deepseekv41_detector.py
@@ -12,12 +12,6 @@ class DeepSeekV41Detector(DeepSeekV32Detector):
tool_calls_block_name = " calls"
invoke_tag_name = " invoke"
parameter_tag_name = " parameter"
- strip_string_param_value: bool = False
-
- # The encoder joins an assistant turn's content and its calls block with a
- # blank line, and renders it even when there is no content.
- tool_calls_prefix = "\n\n"
- think_end_token = ""
def get_structural_tag_name(self) -> Optional[str]:
return "deepseek_v4_1"
diff --git a/python/sglang/srt/parser/deepseek_recipe_output.py b/python/sglang/srt/parser/deepseek_recipe_output.py
index 619e39c9be..74ff161215 100644
--- a/python/sglang/srt/parser/deepseek_recipe_output.py
+++ b/python/sglang/srt/parser/deepseek_recipe_output.py
@@ -11,6 +11,8 @@ from typing import Any, Optional
import msgspec
from sglang.srt.parser.deepseek_recipe_renderer import (
+ THINK_END_TOKEN,
+ THINK_START_TOKEN,
DeepSeekRecipe,
load_deepseek_recipe,
)
@@ -220,3 +222,28 @@ class DeepSeekRecipeOutputParser:
)
)
return delta
+
+
+def parse_output_text(
+ deepseek_recipe: DeepSeekRecipe,
+ text: str,
+ *,
+ parse_tool_calls: bool,
+ thinking: Optional[bool] = None,
+) -> RecipeOutputDelta:
+ """`text` parsed as a whole output; the markers it spells count as the tokens
+ the model would have sampled. With `thinking` None, text that opens with
+ `` or closes a thinking block is reasoning up to ``."""
+ tokenizer = deepseek_recipe.hf_tokenizer
+ output_ids = tokenizer.encode(text, add_special_tokens=False).ids
+ if thinking is None:
+ thinking = output_ids[:1] == [tokenizer.token_to_id(THINK_START_TOKEN)]
+ if thinking:
+ output_ids = output_ids[1:]
+ thinking = thinking or tokenizer.token_to_id(THINK_END_TOKEN) in output_ids
+ parser = DeepSeekRecipeOutputParser(
+ deepseek_recipe,
+ thinking_stage=deepseek_recipe.parse_start(thinking=thinking),
+ parse_tool_calls=parse_tool_calls,
+ )
+ return parser.finish(output_ids, {"type": "stop"})
diff --git a/python/sglang/srt/parser/reasoning_parser.py b/python/sglang/srt/parser/reasoning_parser.py
index c09090eb83..36ccaf28ab 100644
--- a/python/sglang/srt/parser/reasoning_parser.py
+++ b/python/sglang/srt/parser/reasoning_parser.py
@@ -1536,11 +1536,6 @@ class DeepSeekV4Detector(BaseReasoningFormatDetector):
DSV4_THINK_START_TOKEN,
DSV4_THINK_END_TOKEN,
think_excluded_tokens=[DSV4_EOS_TOKEN, DSV4_DSML_TOKEN],
- # The reference parser splits reasoning at `\n\n<|DSML|tool_calls`;
- # anchoring on the full tag at a line start keeps a mid-sentence
- # mention of the tag inside reasoning from ending the block.
- tool_start_token=f"<{DSV4_DSML_TOKEN}tool_calls>",
- tool_start_at_line_start=True,
force_reasoning=force_reasoning,
stream_reasoning=stream_reasoning,
continue_final_message=continue_final_message,
@@ -1551,15 +1546,6 @@ class DeepSeekV4Detector(BaseReasoningFormatDetector):
)
-class DeepSeekV41ReasoningDetector(DeepSeekV4Detector):
- """V4.1 emits the calls block with a spaced tag name (`<|DSML| calls>`),
- so the unspaced V4 tool-start anchor never interrupts its reasoning."""
-
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self.tool_start_token = f"<{DSV4_DSML_TOKEN} calls>"
-
-
class _MimoDetector(Qwen3Detector):
"""MIMO reuses Qwen3 tokens but requires explicit enable_thinking=True to enable."""
@@ -2271,7 +2257,7 @@ class ReasoningParser:
"deepseek-r1": DeepSeekR1Detector,
"deepseek-v3": _DeepSeekV3Detector,
"deepseek-v4": DeepSeekV4Detector,
- "deepseek-v41": DeepSeekV41ReasoningDetector,
+ "deepseek-v41": DeepSeekV4Detector,
"dots": Qwen3Detector,
"glm45": Glm45Detector,
"ling3": Ling3Detector,
diff --git a/test/registered/unit/entrypoints/openai/test_serving_deepseek_recipe.py b/test/registered/unit/entrypoints/openai/test_serving_deepseek_recipe.py
index 934edf424f..a203efb391 100644
--- a/test/registered/unit/entrypoints/openai/test_serving_deepseek_recipe.py
+++ b/test/registered/unit/entrypoints/openai/test_serving_deepseek_recipe.py
@@ -401,6 +401,33 @@ class TestHandlerRequestSemantics(CustomTestCase):
{"city": "Paris"},
)
+ def test_native_endpoints_parse_through_recipe(self):
+ from sglang.srt.entrypoints.openai.chat_encoding_adapters import (
+ native_text_parser,
+ )
+ from sglang.srt.entrypoints.openai.protocol import Tool
+
+ chat = _serving(self, self.tokenizer)
+ adapter = native_text_parser(
+ "deepseek-v41",
+ chat_encoding_spec=chat.chat_encoding_spec,
+ adapter=chat.encoding_adapter,
+ )
+ # V4 output never opens with ; the text parsers left it unsplit.
+ self.assertEqual(adapter.separate_reasoning_text("ab"), ("a", "b"))
+ call = (
+ '\n\n<|DSML| calls>\n<|DSML| invoke name="f">\n'
+ '<|DSML| parameter name="x" string="true">1|DSML| parameter>\n'
+ "|DSML| invoke>\n|DSML| calls>"
+ )
+ tool = Tool(type="function", function={"name": "f", "parameters": {}})
+ text, (item,) = adapter.parse_function_call_text("Sure." + call, [tool])
+ self.assertEqual(
+ (text, item.name, item.parameters), ("Sure.", "f", '{"x": "1"}')
+ )
+ with self.assertRaises(ValueError):
+ native_text_parser("deepseek-v41", chat_encoding_spec=None, adapter=None)
+
if __name__ == "__main__":
unittest.main()
diff --git a/test/registered/unit/function_call/test_deepseekv41_detector.py b/test/registered/unit/function_call/test_deepseekv41_detector.py
index 53b15d4e04..ee67f6d885 100644
--- a/test/registered/unit/function_call/test_deepseekv41_detector.py
+++ b/test/registered/unit/function_call/test_deepseekv41_detector.py
@@ -17,13 +17,11 @@ from sglang.srt.entrypoints.openai.protocol import (
from sglang.srt.function_call.deepseekv41_detector import DeepSeekV41Detector
from sglang.srt.function_call.function_call_parser import FunctionCallParser
from sglang.srt.parser.deepseek_recipe_renderer import load_deepseek_recipe
-from sglang.srt.parser.reasoning_parser import ReasoningParser
from sglang.test.ci.ci_register import register_cpu_ci
from sglang.test.test_utils import CustomTestCase
register_cpu_ci(est_time=2, suite="base-a-test-cpu")
-CHUNK_SIZES = [1, 2, 3, 5, 7, 11, 23, 1000]
DSML = "|DSML|"
EOS = "<|end▁of▁sentence|>"
@@ -88,90 +86,6 @@ def _render_assistant(message, *, thinking):
return turn.removeprefix("" if thinking else "")
-def _assemble(calls):
- """Streamed ToolCallItems -> [(name, parsed arguments)] per tool_index."""
- by_index = {}
- for call in calls:
- entry = by_index.setdefault(call.tool_index, {"name": None, "args": ""})
- if call.name:
- entry["name"] = call.name
- entry["args"] += call.parameters or ""
- return [
- (entry["name"], json.loads(entry["args"]))
- for _, entry in sorted(by_index.items())
- ]
-
-
-class TestDeepSeekV41RoundTrip(CustomTestCase):
- """Encoder-rendered assistant tool calls parse back to the same arguments,
- in one shot and at every chunk size."""
-
- ARGUMENTS = {"query": '{"a": 1}', "limit": 2, "flags": [1, True, None]}
-
- def setUp(self):
- self.tools = _tools()
- self.completion = _render_assistant(
- {
- "role": "assistant",
- "reasoning_content": "reason",
- "content": "summary",
- "tool_calls": [
- {
- "type": "function",
- "function": {
- "name": "lookup",
- "arguments": json.dumps(self.ARGUMENTS),
- },
- }
- ],
- },
- thinking=True,
- )
- self.completion = "" + self.completion
- self.expected = [("lookup", self.ARGUMENTS)]
-
- def test_one_shot(self):
- parser = FunctionCallParser(self.tools, "deepseekv41")
- reasoning, content = ReasoningParser("deepseek-v41").parse_non_stream(
- self.completion
- )
- self.assertEqual(reasoning, "reason")
- normal, calls = parser.parse_non_stream(content)
- self.assertEqual(normal, "summary")
- self.assertEqual(
- [(c.name, json.loads(c.parameters)) for c in calls], self.expected
- )
-
- def test_streaming_at_every_chunk_size(self):
- for chunk_size in CHUNK_SIZES:
- with self.subTest(chunk_size=chunk_size):
- reasoning_parser = ReasoningParser("deepseek-v41")
- tool_parser = FunctionCallParser(self.tools, "deepseekv41")
- reasoning, normal, calls = "", "", []
- for i in range(0, len(self.completion), chunk_size):
- reason, content = reasoning_parser.parse_stream_chunk(
- self.completion[i : i + chunk_size]
- )
- reasoning += reason or ""
- text, delta = tool_parser.parse_stream_chunk(content or "")
- normal += text
- calls.extend(delta)
- reason, content = reasoning_parser.parse_stream_end()
- reasoning += reason or ""
- text, delta = tool_parser.parse_stream_chunk(content or "")
- normal += text
- calls.extend(delta)
- text, delta = tool_parser.parse_stream_end()
- normal += text
- calls.extend(delta)
- self.assertEqual(reasoning, "reason")
- # The blank line before the block is released or trimmed depending
- # on where the chunk boundary falls; the shared base behaves the
- # same for V4, so only the prose itself is pinned here.
- self.assertEqual(normal.strip(), "summary")
- self.assertEqual(_assemble(calls), self.expected)
-
-
class TestDeepSeekV41ConstrainedDecoding(CustomTestCase):
"""A forced call must open the calls block before the first invoke; the
per-tool legacy tag started the grammar at the invoke trigger, the model
@@ -272,17 +186,13 @@ class TestDeepSeekV41ParameterGrammar(CustomTestCase):
self.assertEqual(constraint[0], "structural_tag")
return xgr.Grammar.from_structural_tag(constraint[1])
- def test_encoder_output_matches_and_round_trips(self):
+ def test_encoder_output_matches_the_grammar(self):
arguments = {"query": '{"a": 1}', "limit": 2, "flags": [True, None, 1.5]}
for thinking in (False, True):
with self.subTest(thinking=thinking):
output = self._render(arguments, thinking=thinking, name="lookup")
grammar = self._grammar(thinking=thinking)
self.assertTrue(_is_grammar_accept_string(grammar, output))
- if thinking:
- output = output.split("", 1)[1]
- parsed = DeepSeekV41Detector().detect_and_parse(output, self.tools)
- self.assertEqual(json.loads(parsed.calls[0].parameters), arguments)
def test_strict_schema_rejects_missing_extra_and_wrong_type(self):
for choice in (
diff --git a/test/registered/unit/function_call/test_function_call_parser.py b/test/registered/unit/function_call/test_function_call_parser.py
index 1865b74d1a..43037ee9db 100644
--- a/test/registered/unit/function_call/test_function_call_parser.py
+++ b/test/registered/unit/function_call/test_function_call_parser.py
@@ -2059,91 +2059,6 @@ class TestDeepSeekV4Detector(unittest.TestCase):
),
]
self.detector = DeepSeekV4Detector()
- self.tokenizer = _shared_tokenizer("deepseek-ai/DeepSeek-V3.2")
- self.interval = 1
-
- def test_detect_and_parse_xml_format(self):
- """Test parsing standard XML format (DSML)"""
- text = """I'll help you with information about San Francisco and get its favorite tourist spot for you.\n\n
- <|DSML|tool_calls>\n
- <|DSML|invoke name="get_favorite_tourist_spot">\n
- <|DSML|parameter name="city" string="true">San Francisco|DSML|parameter>\n
- |DSML|invoke>\n
- <|DSML|invoke name="search">
- <|DSML|parameter name="query" string="true">WebNav benchmark|DSML|parameter>
- <|DSML|parameter name="topn" string="false">10|DSML|parameter>
- <|DSML|parameter name="source" string="true">web|DSML|parameter>
- |DSML|invoke>
- |DSML|tool_calls>
- """
- result = self.detector.detect_and_parse(text, self.tools)
-
- self.assertIn("I'll help you with information", result.normal_text)
- self.assertEqual(len(result.calls), 2)
-
- # Check first call
- call1 = result.calls[0]
- self.assertEqual(call1.name, "get_favorite_tourist_spot")
- params1 = json.loads(call1.parameters)
- self.assertEqual(params1["city"], "San Francisco")
-
- # Check second call
- call2 = result.calls[1]
- self.assertEqual(call2.name, "search")
- params2 = json.loads(call2.parameters)
- self.assertEqual(params2["query"], "WebNav benchmark")
- self.assertEqual(params2["topn"], 10)
- self.assertEqual(params2["source"], "web")
-
- def test_streaming_xml_format(self):
- """Test streaming parsing of XML format"""
- text = """<|DSML|tool_calls>
- <|DSML|invoke name="get_favorite_tourist_spot">
- <|DSML|parameter name="city" string="true">San Francisco|DSML|parameter>
- <|DSML|parameter name="another_city" string="true">London|DSML|parameter>
- <|DSML|parameter name="topn" string="false">10|DSML|parameter>
- <|DSML|parameter name="obj" string="false">{"name": "John", "age": 30}|DSML|parameter>
- |DSML|invoke>
- |DSML|tool_calls>"""
-
- input_ids = self.tokenizer.encode(text, add_special_tokens=False)
- chunk_ids = [
- input_ids[i : i + self.interval]
- for i in range(0, len(input_ids), self.interval)
- ]
- chunks = [self.tokenizer.decode(chunk_id) for chunk_id in chunk_ids]
-
- tool_calls_by_index = {}
-
- num_tool_call_chunks = 0
- for chunk in chunks:
- result = self.detector.parse_streaming_increment(chunk, self.tools)
- for call in result.calls:
- num_tool_call_chunks += 1
- if call.tool_index is not None:
- if call.tool_index not in tool_calls_by_index:
- tool_calls_by_index[call.tool_index] = {
- "name": "",
- "parameters": "",
- }
-
- if call.name:
- tool_calls_by_index[call.tool_index]["name"] = call.name
- if call.parameters:
- tool_calls_by_index[call.tool_index]["parameters"] += (
- call.parameters
- )
-
- self.assertEqual(num_tool_call_chunks, 1)
-
- self.assertEqual(len(tool_calls_by_index), 1)
- self.assertEqual(tool_calls_by_index[0]["name"], "get_favorite_tourist_spot")
- params = json.loads(tool_calls_by_index[0]["parameters"])
- self.assertEqual(params["city"], "San Francisco")
- self.assertEqual(params["another_city"], "London")
- self.assertEqual(params["topn"], 10)
- self.assertEqual(params["obj"]["name"], "John")
- self.assertEqual(params["obj"]["age"], 30)
def test_get_model_structural_tag(self):
import xgrammar as xgr
@@ -2197,96 +2112,6 @@ class TestDeepSeekV4Detector(unittest.TestCase):
grammar = xgr.Grammar.from_structural_tag(structural_tag)
self.assertIsInstance(grammar, xgr.Grammar)
- def test_self_closing_zero_arg_invoke(self):
- """V4 emits `<|DSML|invoke name="x"/>` for zero-arg tools; the
- detector must parse it as a complete tool call with empty params
- instead of leaking the raw markup back into normal_text."""
- submit_tool = Tool(
- type="function",
- function=Function(
- name="submit",
- description="Submit the final answer.",
- parameters={"type": "object", "properties": {}},
- ),
- )
-
- text = (
- "Final answer.\n"
- '<|DSML|tool_calls>\n<|DSML|invoke name="submit"/>\n'
- "|DSML|tool_calls>"
- )
- result = self.detector.detect_and_parse(text, [submit_tool])
- self.assertEqual(len(result.calls), 1)
- self.assertEqual(result.calls[0].name, "submit")
- self.assertEqual(json.loads(result.calls[0].parameters), {})
- self.assertNotIn("DSML", result.normal_text)
-
- def test_self_closing_mixed_with_long_form(self):
- """Mix of long-form (with params) and self-closing tags in one block."""
- submit_tool = Tool(
- type="function",
- function=Function(
- name="submit",
- parameters={"type": "object", "properties": {}},
- ),
- )
- text = (
- "<|DSML|tool_calls>\n"
- '<|DSML|invoke name="get_favorite_tourist_spot">\n'
- '<|DSML|parameter name="city" string="true">SF|DSML|parameter>\n'
- "|DSML|invoke>\n"
- '<|DSML|invoke name="submit"/>\n'
- "|DSML|tool_calls>"
- )
- result = self.detector.detect_and_parse(text, self.tools + [submit_tool])
- self.assertEqual(len(result.calls), 2)
- self.assertEqual(result.calls[0].name, "get_favorite_tourist_spot")
- self.assertEqual(json.loads(result.calls[0].parameters), {"city": "SF"})
- self.assertEqual(result.calls[1].name, "submit")
- self.assertEqual(json.loads(result.calls[1].parameters), {})
-
- def test_streaming_self_closing_invoke(self):
- """Self-closing invoke must terminate cleanly even when `/>` arrives
- after the `name=` attribute crosses chunk boundaries."""
- submit_tool = Tool(
- type="function",
- function=Function(
- name="submit",
- parameters={"type": "object", "properties": {}},
- ),
- )
- # Build the prompt and feed it through the tokenizer to exercise the
- # same chunk shapes the runtime sees.
- text = (
- "<|DSML|tool_calls>\n"
- '<|DSML|invoke name="submit"/>\n'
- "|DSML|tool_calls>"
- )
- self.detector = DeepSeekV4Detector()
- input_ids = self.tokenizer.encode(text, add_special_tokens=False)
- chunks = [
- self.tokenizer.decode(input_ids[i : i + self.interval])
- for i in range(0, len(input_ids), self.interval)
- ]
-
- tool_calls_by_index = {}
- for chunk in chunks:
- result = self.detector.parse_streaming_increment(chunk, [submit_tool])
- for call in result.calls:
- if call.tool_index is None:
- continue
- slot = tool_calls_by_index.setdefault(
- call.tool_index, {"name": "", "parameters": ""}
- )
- if call.name:
- slot["name"] = call.name
- if call.parameters:
- slot["parameters"] += call.parameters
-
- self.assertEqual(len(tool_calls_by_index), 1)
- self.assertEqual(tool_calls_by_index[0]["name"], "submit")
- self.assertEqual(json.loads(tool_calls_by_index[0]["parameters"]), {})
-
class TestQwen3CoderDetector(unittest.TestCase):
"""Test suite for Qwen3CoderDetector."""
diff --git a/test/registered/unit/parser/test_reasoning_parser.py b/test/registered/unit/parser/test_reasoning_parser.py
index 93f81a543e..d0942f1d8a 100644
--- a/test/registered/unit/parser/test_reasoning_parser.py
+++ b/test/registered/unit/parser/test_reasoning_parser.py
@@ -245,74 +245,6 @@ class TestDeepSeekV4Detector(CustomTestCase):
self.assertEqual(detector.reasoning_default, "explicit_thinking")
self.assertTrue(detector.thinks_internally)
- def test_dsml_block_is_routed_out_of_reasoning(self):
- """A DSML block that starts a line inside reasoning is routed to
- normal_text so the tool call detector sees it, even without ."""
- detector = ReasoningParser(model_type="deepseek-v4").detector
- self.assertEqual(detector.tool_start_token, "<|DSML|tool_calls>")
- self.assertTrue(detector.tool_start_at_line_start)
-
- result = detector.parse_streaming_increment(
- 'pick a tool\n\n<|DSML|tool_calls><|DSML|invoke name="s">'
- )
- self.assertEqual(result.reasoning_text, "pick a tool\n\n")
- self.assertTrue(result.normal_text.startswith("<|DSML|tool_calls>"))
-
- def test_dsml_mention_mid_sentence_stays_in_reasoning(self):
- """The system prompt shows the model the DSML tag, so it may talk about
- it while thinking. A mid-sentence mention must not end the block."""
- detector = ReasoningParser(model_type="deepseek-v4").detector
-
- result = detector.parse_streaming_increment(
- "I'll output a <|DSML|tool_calls> block with JSON.done"
- )
- self.assertEqual(
- result.reasoning_text, "I'll output a <|DSML|tool_calls> block with JSON."
- )
- self.assertEqual(result.normal_text, "done")
-
- def test_truncated_calls_opener_is_not_flushed_into_reasoning(self):
- """A generation cut off mid-calls-opener (e.g. max_tokens before `>`)
- must not surface the partial tag in reasoning_content."""
- for model, partial in (
- ("deepseek-v4", "<|DSML|tool_calls"),
- ("deepseek-v41", "<|DSML| calls"),
- ):
- with self.subTest(model=model):
- text = "Choosing a tool\n\n" + partial
- detector = ReasoningParser(
- model_type=model, force_reasoning=True
- ).detector
- streamed = detector.parse_streaming_increment(text)
- flushed = detector.finish()
- reasoning = (streamed.reasoning_text or "") + (
- flushed.reasoning_text or ""
- )
- normal = (streamed.normal_text or "") + (flushed.normal_text or "")
- self.assertEqual(reasoning, "Choosing a tool\n\n")
- self.assertEqual(normal, "")
-
- result = ReasoningParser(
- model_type=model, force_reasoning=True
- ).detector.detect_and_parse(text)
- self.assertEqual(result.reasoning_text, "Choosing a tool\n\n")
- self.assertFalse(result.normal_text)
-
- def test_mid_sentence_partial_calls_opener_stays_in_reasoning(self):
- """A partial calls opener that does not begin a line is a mention,
- not a call, and must survive the EOS flush."""
- detector = ReasoningParser(
- model_type="deepseek-v4", force_reasoning=True
- ).detector
- text = "mentioning <|DSML|tool"
-
- streamed = detector.parse_streaming_increment(text)
- flushed = detector.finish()
-
- self.assertEqual(
- (streamed.reasoning_text or "") + (flushed.reasoning_text or ""), text
- )
-
class TestInklingDetector(CustomTestCase):
def test_streaming_routes_blocks_across_all_string_boundaries(self):
@@ -1177,74 +1109,6 @@ class TestStreamingChunkSizeInvariance(CustomTestCase):
(one_shot.reasoning_text, one_shot.normal_text), ("leadr", "tail")
)
- def test_dsv4_reasoning_quoting_dsml_is_chunk_invariant(self):
- """A mid-sentence mention of the DSML tag stays in reasoning at every
- chunk width and in one-shot parsing. Reachable because the DSV4 system
- prompt shows that marker to the model."""
- text = f"format is <{self.DSML}tool_calls>answer"
- self._assert_invariant(
- DeepSeekV4Detector, text, (f"format is <{self.DSML}tool_calls>", "answer")
- )
-
- def test_dsv4_reasoning_mentioning_dsml_then_real_block(self):
- """Mention inside reasoning, then a real block after : the
- mention stays in reasoning and the block reaches normal_text intact."""
- tool_call = (
- f"<{self.DSML}tool_calls>\n"
- f'<{self.DSML}invoke name="s">{self.DSML}invoke>\n'
- f"{self.DSML}tool_calls>"
- )
- text = (
- f"I'll use a <{self.DSML}tool_calls> block.\n\n{tool_call}"
- )
- self._assert_invariant(
- DeepSeekV4Detector,
- text,
- (f"I'll use a <{self.DSML}tool_calls> block.", f"\n\n{tool_call}"),
- )
-
- def test_dsv4_tool_block_after_think_end(self):
- tool_call = (
- f"<{self.DSML}tool_calls>"
- f'<{self.DSML}invoke name="s">{self.DSML}invoke>'
- f"{self.DSML}tool_calls>"
- )
- self._assert_invariant(
- DeepSeekV4Detector,
- f"my reasoning{tool_call}",
- ("my reasoning", tool_call),
- )
-
- def test_dsv4_tool_block_without_think_end(self):
- """A DSML block starting a line directly after reasoning must still be
- routed to normal_text so the tool call detector can see it, including
- when the preceding newline streamed out in an earlier chunk."""
- tool_call = (
- f"<{self.DSML}tool_calls>"
- f'<{self.DSML}invoke name="s">{self.DSML}invoke>'
- f"{self.DSML}tool_calls>"
- )
- for chunk_size in self.CHUNK_SIZES:
- with self.subTest(chunk_size=chunk_size):
- self.assertEqual(
- self._feed(
- DeepSeekV4Detector(),
- f"my reasoning\n\n{tool_call}",
- chunk_size,
- ),
- ("my reasoning\n\n", tool_call),
- )
-
- def test_dsv4_tool_block_mid_line_without_think_end_stays_in_reasoning(self):
- """Accepted tradeoff: a block that neither follows nor starts a
- line is indistinguishable from a mention, so it is left in reasoning."""
- tool_call = f"<{self.DSML}tool_calls>{self.DSML}tool_calls>"
- self._assert_invariant(
- DeepSeekV4Detector,
- f"my reasoning {tool_call}",
- (f"my reasoning {tool_call}", ""),
- )
-
class TestGptOssDetector(CustomTestCase):
"""Test cases for GptOssDetector which delegates to HarmonyParser."""