Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tensorrt_llm/commands/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ def serve(
f"Cannot auto-detect tool parser for model '{model}'. "
f"Supported model types for auto-detection: qwen2, qwen3, "
f"qwen3_moe, qwen3_5, qwen3_5_moe, qwen3_next, deepseek_v3, "
f"deepseek_v32, kimi_k2, kimi_k25, glm4. "
f"deepseek_v32, deepseek_v4, kimi_k2, kimi_k25, glm4. "
f"Please specify a parser explicitly: "
f"{list(ToolParserFactory.parsers.keys())}",
param_hint="--tool_parser")
Expand All @@ -835,7 +835,7 @@ def serve(
f"Cannot auto-detect reasoning parser for model '{model}'. "
f"Supported model types for auto-detection: qwen3, qwen3_moe, "
f"qwen3_5, qwen3_5_moe, qwen3_next, deepseek_v3 (R1 only), "
f"deepseek_v32 (R1 only), nemotron_h. "
f"deepseek_v32 (R1 only), deepseek_v4, nemotron_h. "
f"Please specify a parser explicitly: "
f"{list(ReasoningParserFactory.keys())}",
param_hint="--reasoning_parser")
Expand Down
50 changes: 50 additions & 0 deletions tensorrt_llm/llmapi/reasoning_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ def finish(self) -> ReasoningParserResult:
return ReasoningParserResult()


class IdentityReasoningParser(BaseReasoningParser):
"""Reasoning parser that treats all model output as visible content."""

reasoning_start = "<think>"
reasoning_end = "</think>"

def parse(self, text: str) -> ReasoningParserResult:
return ReasoningParserResult(content=text)

def parse_delta(self, delta_text: str) -> ReasoningParserResult:
return ReasoningParserResult(content=delta_text)


@register_reasoning_parser("deepseek-r1", reasoning_at_start=True)
@register_reasoning_parser("qwen3")
@register_reasoning_parser("minimax_m2", reasoning_at_start=True)
Expand Down Expand Up @@ -173,6 +186,42 @@ def parse_delta(self, delta_text: str) -> ReasoningParserResult:
"Unreachable code reached in `DeepSeekR1Parser.parse_delta`")


@register_reasoning_parser("deepseek_v4")
class DeepSeekV4ReasoningParser(BaseReasoningParser):
"""DeepSeek-V4 parser selected by thinking-mode chat template kwargs."""

reasoning_start = "<think>"
reasoning_end = "</think>"

def __init__(
self,
*,
chat_template_kwargs: Optional[dict[str, Any]] = None,
) -> None:
super().__init__(chat_template_kwargs=chat_template_kwargs)
chat_template_kwargs = chat_template_kwargs or {}
thinking = bool(
chat_template_kwargs.get("thinking", False)
or chat_template_kwargs.get("enable_thinking", False))
if thinking:
self._parser = DeepSeekR1Parser(
reasoning_at_start=True,
chat_template_kwargs=chat_template_kwargs,
)
else:
self._parser = IdentityReasoningParser(
chat_template_kwargs=chat_template_kwargs)

def parse(self, text: str) -> ReasoningParserResult:
return self._parser.parse(text)

def parse_delta(self, delta_text: str) -> ReasoningParserResult:
return self._parser.parse_delta(delta_text)

def finish(self) -> ReasoningParserResult:
return self._parser.finish()


MODEL_TYPE_TO_REASONING_PARSER: dict[str, str] = {
"qwen3": "qwen3",
"qwen3_moe": "qwen3",
Expand All @@ -181,6 +230,7 @@ def parse_delta(self, delta_text: str) -> ReasoningParserResult:
"qwen3_next": "qwen3",
"deepseek_v3": "deepseek-r1",
"deepseek_v32": "deepseek-r1",
"deepseek_v4": "deepseek_v4",
"nemotron_h": "nano-v3",
}

Expand Down
2 changes: 1 addition & 1 deletion tensorrt_llm/serve/tool_parser/deepseekv32_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def detect_and_parse(self, text: str, tools: List[Tool]) -> StreamingParseResult
try:
# Extract content between function_calls tags
function_calls_match = re.search(
r"<|DSML|function_calls>(.*?)</|DSML|function_calls>",
re.escape(self.bot_token) + r"(.*?)" + re.escape(self.eot_token),
text,
re.DOTALL,
)
Expand Down
25 changes: 25 additions & 0 deletions tensorrt_llm/serve/tool_parser/deepseekv4_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .deepseekv32_parser import DeepSeekV32Parser


class DeepSeekV4Parser(DeepSeekV32Parser):
"""Tool parser for the DeepSeek V4 DSML tool call format."""

def __init__(self) -> None:
super().__init__()
self.bot_token = "<|DSML|tool_calls>" # nosec B105
self.eot_token = "</|DSML|tool_calls>" # nosec B105
3 changes: 3 additions & 0 deletions tensorrt_llm/serve/tool_parser/tool_parser_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .base_tool_parser import BaseToolParser
from .deepseekv3_parser import DeepSeekV3Parser
from .deepseekv4_parser import DeepSeekV4Parser
from .deepseekv31_parser import DeepSeekV31Parser
from .deepseekv32_parser import DeepSeekV32Parser
from .glm4_parser import Glm4ToolParser
Expand All @@ -21,6 +22,7 @@
"qwen3_next": "qwen3",
"deepseek_v3": "deepseek_v3",
"deepseek_v32": "deepseek_v32",
"deepseek_v4": "deepseek_v4",
"kimi_k2": "kimi_k2",
"kimi_k25": "kimi_k2",
"glm4": "glm4",
Expand Down Expand Up @@ -48,6 +50,7 @@ class ToolParserFactory:
"deepseek_v3": DeepSeekV3Parser,
"deepseek_v31": DeepSeekV31Parser,
"deepseek_v32": DeepSeekV32Parser,
"deepseek_v4": DeepSeekV4Parser,
"glm4": Glm4ToolParser,
"minimax_m2": MiniMaxM2ToolParser,
}
Expand Down
Loading
Loading