[Frontend] Support count_reasoning_tokens in the Streaming Parser Engine (#45802)

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Chauncey
2026-08-15 06:51:14 -07:00
committed by GitHub
co-authored by mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 5cecfc0137
commit ac2ae8798c
16 changed files with 669 additions and 73 deletions
+30 -4
View File
@@ -9,6 +9,7 @@ from vllm.logger import init_logger
if TYPE_CHECKING:
from vllm.parser.abstract_parser import Parser
from vllm.parser.engine.parser_engine import ParserEngine
from vllm.reasoning import ReasoningParser
from vllm.tool_parsers import ToolParser
@@ -17,10 +18,30 @@ logger = init_logger(__name__)
class ParserManager:
"""
Provides a unified Parser by composing individual reasoning and tool
parsers from their respective registries.
Provides a unified Parser from the reasoning and tool parser registries.
Parser engine adapters backed by the same engine are collapsed back into
that engine. Other parser pairs are composed through ``DelegatingParser``.
"""
@staticmethod
def _get_parser_engine_cls(
parser_cls: type[object] | None,
) -> type[ParserEngine] | None:
if parser_cls is None:
return None
parser_engine_cls = getattr(parser_cls, "_parser_engine_cls", None)
if parser_engine_cls is None:
return None
from vllm.parser.engine.parser_engine import ParserEngine
if not isinstance(parser_engine_cls, type) or not issubclass(
parser_engine_cls, ParserEngine
):
return None
return parser_engine_cls
@classmethod
def get_tool_parser(
cls,
@@ -84,8 +105,8 @@ class ParserManager:
"""
Get a Parser that handles both reasoning and tool parsing.
Composes individual reasoning and tool parsers into a single
DelegatingParser subclass.
Reuses a shared parser engine when possible, otherwise composes the
individual parsers into a ``DelegatingParser`` subclass.
Args:
tool_parser_name: The name of the tool parser.
@@ -116,6 +137,11 @@ class ParserManager:
HarmonyParser.tool_parser_cls = tool_parser_cls
return HarmonyParser
reasoning_engine_cls = cls._get_parser_engine_cls(reasoning_parser_cls)
tool_engine_cls = cls._get_parser_engine_cls(tool_parser_cls)
if reasoning_engine_cls is not None and reasoning_engine_cls is tool_engine_cls:
return reasoning_engine_cls
if reasoning_parser_name == "kimi_k3" or tool_parser_name == "kimi_k3":
from vllm.parser.kimi_k3 import KimiK3Parser