Skip to content
Open
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
13 changes: 8 additions & 5 deletions tensorrt_llm/llmapi/reasoning_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,15 @@ def finish(self) -> ReasoningParserResult:
as reasoning_content since we are still in reasoning mode.

If the closing tag was already found (or reasoning was never
entered), flushes any remaining buffer as content."""
entered), flushes any remaining buffer as content.

A buffer holding exactly a complete tag is a delimiter rather than
model output, so it is discarded."""
remaining = self._buffer
self._buffer = ""
if remaining in (self.reasoning_start, self.reasoning_end):
remaining = ""
if self.in_reasoning and not self._found_closing_tag:
remaining = self._buffer
self._buffer = ""
if self._force_nonempty_content:
all_content = self._accumulated_reasoning + remaining
self._accumulated_reasoning = ""
Expand All @@ -648,8 +653,6 @@ def finish(self) -> ReasoningParserResult:
if remaining:
return ReasoningParserResult(reasoning_content=remaining)
return ReasoningParserResult()
remaining = self._buffer
self._buffer = ""
if remaining:
return ReasoningParserResult(content=remaining)
return ReasoningParserResult()
Expand Down
9 changes: 9 additions & 0 deletions tests/unittest/llmapi/test_reasoning_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,15 @@ def test_nano_v3_reasoning_parser_stream(delta_texts: list, content: list,
(["reasoning", "<tool_call>data"], "", "", {
"force_nonempty_content": True
}),
# A stream that ends on a complete tag: the tag is a delimiter,
# not model output.
(["a", R1_END], "", "", None),
(["a", R1_END], "a", "", {
"force_nonempty_content": True
}),
([R1_START], "", "", {
"enable_thinking": False
}),
])
def test_nano_v3_reasoning_parser_finish(delta_texts: list, finish_content: str,
finish_reasoning: str,
Expand Down
Loading