vllm.parser.abstract_parser
¶
Classes:
-
DelegatingParser–A Parser implementation that delegates to separate ReasoningParser and
-
Parser–Parse model output into reasoning, content and tool calls.
-
StreamState–Mutable state for
Parser.parse_delta(). One per stream.
Functions:
-
structured_outputs_to_format–Map StructuredOutputsParams in a XGrammar Format.
DelegatingParser
¶
Bases: Parser
A Parser implementation that delegates to separate ReasoningParser and ToolParser instances.
This is the recommended base class for creating model-specific parsers
that combine existing reasoning and tool parser implementations.
Subclasses should set self._reasoning_parser and self._tool_parser
in their __init__ method.
If either parser is None, the corresponding methods will return default values (no reasoning extraction, no tool calls).
Methods:
-
count_reasoning_tokens–Count reasoning tokens through the configured reasoning parser.
-
finalize_generation–Finalize generation for cases where generation was incomplete.
_append_unstreamed_tool_args(delta_message)
¶
Append parsed-but-unstreamed tool-call arguments to delta_message.
_flush_engine_parsers(delta_message)
¶
Flush buffered state from engine-based parsers at stream end.
count_reasoning_tokens(token_ids)
¶
Count reasoning tokens through the configured reasoning parser.
finalize_generation(delta_message, request, state)
¶
Finalize generation for cases where generation was incomplete. For example, if streaming terminated before reasoning ended
Parser
¶
Parse model output into reasoning, content and tool calls.
The serving layer holds one Parser per request and calls only the
members defined here. ParserEngine implements them over a single
declarative engine; DelegatingParser composes a legacy
ReasoningParser / ToolParser pair.
Methods:
-
adjust_request–Adjust the request parameters for tool calling.
-
count_reasoning_tokens–Return the number of reasoning tokens in generated token IDs.
-
is_reasoning_end–Check if the reasoning content ends in the input_ids.
-
parse–Parse a complete model output, extracting reasoning and tool calls.
-
parse_delta–Parse a single streaming delta, orchestrating reasoning then
-
set_history_tool_call_count–Seed the history count when
request.messagesis not available. -
set_prompt_token_ids–Provide the exact rendered prompt to parsers that need prefix state.
Attributes:
-
reasoning_parser(ReasoningParser | None) –The underlying reasoning parser, if any.
-
tool_parser(ToolParser | None) –The underlying tool parser, if any.
reasoning_parser
property
¶
The underlying reasoning parser, if any.
tool_parser
property
¶
The underlying tool parser, if any.
adjust_request(request)
¶
Adjust the request parameters for tool calling.
Can be overridden by subclasses to modify request parameters (e.g., setting structured output schemas for tool calling).
Parameters:
-
(request¶ChatCompletionRequest | ResponsesRequest) –The original request.
Returns:
-
ChatCompletionRequest | ResponsesRequest–The adjusted request.
count_reasoning_tokens(token_ids)
¶
Return the number of reasoning tokens in generated token IDs.
is_reasoning_end(input_ids)
abstractmethod
¶
Check if the reasoning content ends in the input_ids.
Called with the rendered prompt to decide whether generation starts after reasoning. Must be a pure function of the input_ids.
Parameters:
Returns:
-
bool–True if the reasoning content ends in the input_ids.
parse(model_output, request, enable_auto_tools=False, model_output_token_ids=())
abstractmethod
¶
Parse a complete model output, extracting reasoning and tool calls.
Parameters:
-
(model_output¶str) –The complete model-generated string.
-
(request¶ChatCompletionRequest | ResponsesRequest) –The request object used to generate the output.
-
(enable_auto_tools¶bool, default:False) –Whether to enable automatic tool call parsing.
-
(model_output_token_ids¶Sequence[int], default:()) –The generated raw output token IDs.
Returns:
parse_delta(delta_text, delta_token_ids, request, prompt_token_ids=None, *, finished)
abstractmethod
¶
Parse a single streaming delta, orchestrating reasoning then tool call extraction via internal stream state.
set_history_tool_call_count(count)
¶
Seed the history count when request.messages is not available.
set_prompt_token_ids(prompt_token_ids)
¶
Provide the exact rendered prompt to parsers that need prefix state.
StreamState
dataclass
¶
Mutable state for Parser.parse_delta(). One per stream.
_xgrammar_supports(structured_outputs)
¶
Whether the constraint passes xgrammar validation on its own.
structured_outputs_to_format(params)
¶
Map StructuredOutputsParams in a XGrammar Format.