[Bugfix][Frontend] Count reasoning tokens for Harmony, DeepSeek-V3 and Step3 parsers (#58626)

Signed-off-by: Samyabrata Maji <116789799+sammaji@users.noreply.github.com>
Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
Samyabrata Maji
2026-09-25 00:12:29 +00:00
committed by GitHub
co-authored by Flora Feng
parent b3a87b2d04
commit 064bc747ab
6 changed files with 136 additions and 3 deletions
+29 -3
View File
@@ -38,6 +38,7 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
)
from vllm.entrypoints.openai.parser.harmony_utils import (
extract_function_from_recipient,
get_encoding,
get_streamable_parser_for_assistant,
is_function_recipient,
)
@@ -114,6 +115,9 @@ class HarmonyParser(DelegatingParser):
self._next_tool_call_index = 0
self._num_processed_messages = 0
self._num_counted_tokens = 0
self._num_reasoning_tokens = 0
# For error recovery
self._current_message_tokens: list[int] = []
@@ -348,9 +352,7 @@ class HarmonyParser(DelegatingParser):
else:
self._current_message_tokens.append(token_id)
if channel == "analysis" or (
channel == "commentary" and recipient is not None
):
if self._is_reasoning_token(token_id, channel, recipient):
reasoning_token_count += 1
segments.append(
@@ -364,11 +366,35 @@ class HarmonyParser(DelegatingParser):
# TODO: Optionally merge and suppress empty Segments
self._num_counted_tokens += len(token_ids)
self._num_reasoning_tokens += reasoning_token_count
return ChunkResult(
segments=segments,
reasoning_token_count=reasoning_token_count,
)
def count_reasoning_tokens(self, token_ids: Sequence[int]) -> int:
if len(token_ids) == self._num_counted_tokens:
return self._num_reasoning_tokens
parser = get_streamable_parser_for_assistant()
count = 0
for token_id in token_ids:
parser.process(token_id)
recipient = self._normalize_recipient(parser.current_recipient)
if self._is_reasoning_token(token_id, parser.current_channel, recipient):
count += 1
return count
@staticmethod
def _is_reasoning_token(
token_id: int, channel: str | None, recipient: str | None
) -> bool:
is_reasoning_channel = channel == "analysis" or (
channel == "commentary" and recipient is not None
)
return is_reasoning_channel and not get_encoding().is_special_token(token_id)
def adjust_request(
self, request: ChatCompletionRequest | ResponsesRequest
) -> ChatCompletionRequest | ResponsesRequest: