vllm.parser ¶
Modules:
| Name | Description |
|---|---|
abstract_parser | |
minimax_m2_parser | MiniMax M2 Parser - A unified parser for MiniMax M2 models. |
parser_manager | |
_PARSERS_TO_REGISTER module-attribute ¶
__all__ module-attribute ¶
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).
Source code in vllm/parser/abstract_parser.py
extract_reasoning ¶
extract_reasoning(
model_output: str,
request: ChatCompletionRequest | ResponsesRequest,
) -> tuple[str | None, str | None]
Source code in vllm/parser/abstract_parser.py
extract_reasoning_streaming ¶
extract_reasoning_streaming(
previous_text: str,
current_text: str,
delta_text: str,
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> DeltaMessage | None
Source code in vllm/parser/abstract_parser.py
extract_tool_calls ¶
extract_tool_calls(
model_output: str, request: ChatCompletionRequest
) -> ExtractedToolCallInformation
Source code in vllm/parser/abstract_parser.py
extract_tool_calls_streaming ¶
extract_tool_calls_streaming(
previous_text: str,
current_text: str,
delta_text: str,
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
request: ChatCompletionRequest,
) -> DeltaMessage | None
Source code in vllm/parser/abstract_parser.py
Parser ¶
Abstract Parser class that unifies ReasoningParser and ToolParser into a single interface for parsing model output.
This class provides a unified way to handle both reasoning extraction (e.g., chain-of-thought content in
Subclasses can either: 1. Override the abstract methods directly for custom parsing logic 2. Set reasoning_parser and tool_parser properties to delegate to existing parser implementations
Class Attributes
reasoning_parser_cls: The ReasoningParser class to use (for compatibility with code that needs the class, not instance). tool_parser_cls: The ToolParser class to use (for compatibility with code that needs the class, not instance).
Source code in vllm/parser/abstract_parser.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
reasoning_parser property writable ¶
reasoning_parser: ReasoningParser | None
The underlying reasoning parser, if any.
reasoning_parser_cls class-attribute instance-attribute ¶
reasoning_parser_cls: type[ReasoningParser] | None = None
tool_parser_cls class-attribute instance-attribute ¶
tool_parser_cls: type[ToolParser] | None = None
__init__ ¶
__init__(tokenizer: TokenizerLike, *args, **kwargs)
Initialize the Parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer | TokenizerLike | The tokenizer used by the model. This is required for token-based parsing operations. | required |
Source code in vllm/parser/abstract_parser.py
adjust_request ¶
adjust_request(
request: ChatCompletionRequest,
) -> ChatCompletionRequest
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:
| Name | Type | Description | Default |
|---|---|---|---|
request | ChatCompletionRequest | The original request. | required |
Returns:
| Type | Description |
|---|---|
ChatCompletionRequest | The adjusted request. |
Source code in vllm/parser/abstract_parser.py
extract_content_ids abstractmethod ¶
Extract content token IDs from the input_ids.
This extracts the non-reasoning content (e.g., everything after the tag).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids | list[int] | The token IDs of the model output. | required |
Returns:
| Type | Description |
|---|---|
list[int] | The extracted content token IDs. |
Source code in vllm/parser/abstract_parser.py
extract_reasoning abstractmethod ¶
extract_reasoning(
model_output: str,
request: ChatCompletionRequest | ResponsesRequest,
) -> tuple[str | None, str | None]
Extract reasoning content from a complete model-generated string.
Used for non-streaming responses where we have the entire model response available before sending to the client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_output | str | The complete model-generated string. | required |
request | ChatCompletionRequest | ResponsesRequest | The request object used to generate the output. | required |
Returns:
| Type | Description |
|---|---|
tuple[str | None, str | None] | A tuple of (reasoning_content, response_content). |
Source code in vllm/parser/abstract_parser.py
extract_reasoning_streaming abstractmethod ¶
extract_reasoning_streaming(
previous_text: str,
current_text: str,
delta_text: str,
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> DeltaMessage | None
Extract reasoning content from a streaming delta message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
previous_text | str | Text from all previous tokens. | required |
current_text | str | Text including the current delta. | required |
delta_text | str | The new text in this delta. | required |
previous_token_ids | Sequence[int] | Token IDs from previous generation. | required |
current_token_ids | Sequence[int] | All token IDs including current. | required |
delta_token_ids | Sequence[int] | The new token IDs in this delta. | required |
Returns:
| Type | Description |
|---|---|
DeltaMessage | None | A DeltaMessage with reasoning and/or content fields, or None. |
Source code in vllm/parser/abstract_parser.py
extract_tool_calls abstractmethod ¶
extract_tool_calls(
model_output: str, request: ChatCompletionRequest
) -> ExtractedToolCallInformation
Extract tool calls from a complete model-generated string.
Used for non-streaming responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_output | str | The complete model-generated string. | required |
request | ChatCompletionRequest | The request object used to generate the output. | required |
Returns:
| Type | Description |
|---|---|
ExtractedToolCallInformation | ExtractedToolCallInformation containing the tool calls. |
Source code in vllm/parser/abstract_parser.py
extract_tool_calls_streaming abstractmethod ¶
extract_tool_calls_streaming(
previous_text: str,
current_text: str,
delta_text: str,
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
request: ChatCompletionRequest,
) -> DeltaMessage | None
Extract tool calls from a streaming delta message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
previous_text | str | Text from all previous tokens. | required |
current_text | str | Text including the current delta. | required |
delta_text | str | The new text in this delta. | required |
previous_token_ids | Sequence[int] | Token IDs from previous generation. | required |
current_token_ids | Sequence[int] | All token IDs including current. | required |
delta_token_ids | Sequence[int] | The new token IDs in this delta. | required |
request | ChatCompletionRequest | The request object. | required |
Returns:
| Type | Description |
|---|---|
DeltaMessage | None | A DeltaMessage with tool_calls field, or None. |
Source code in vllm/parser/abstract_parser.py
is_reasoning_end abstractmethod ¶
Check if the reasoning content ends in the input_ids.
Used by structured engines like xgrammar to check if the reasoning content ends in the model output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids | list[int] | The token IDs of the model output. | required |
Returns:
| Type | Description |
|---|---|
bool | True if the reasoning content ends in the input_ids. |
Source code in vllm/parser/abstract_parser.py
is_reasoning_end_streaming ¶
Check if the reasoning content ends during a decode step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids | list[int] | The entire model output token IDs. | required |
delta_ids | list[int] | The last few computed tokens at the current decode step. | required |
Returns:
| Type | Description |
|---|---|
bool | True if the reasoning content ends in the delta_ids. |
Source code in vllm/parser/abstract_parser.py
ParserManager ¶
Central registry for Parser implementations.
Supports two registration modes
- Eager registration via
register_module - Lazy registration via
register_lazy_module
Source code in vllm/parser/parser_manager.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
_load_lazy_parser classmethod ¶
Import and register a lazily loaded parser.
Source code in vllm/parser/parser_manager.py
_register_module classmethod ¶
_register_module(
module: type[Parser],
module_name: str | list[str] | None = None,
force: bool = True,
) -> None
Register a Parser class immediately.
Source code in vllm/parser/parser_manager.py
get_parser classmethod ¶
get_parser(
tool_parser_name: str | None = None,
reasoning_parser_name: str | None = None,
enable_auto_tools: bool = False,
model_name: str | None = None,
) -> type[Parser] | None
Get a unified Parser that handles both reasoning and tool parsing.
This method checks if a unified Parser exists that can handle both reasoning extraction and tool call parsing. If no unified parser exists, it creates a DelegatingParser that wraps the individual reasoning and tool parsers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_parser_name | str | None | The name of the tool parser. | None |
reasoning_parser_name | str | None | The name of the reasoning parser. | None |
enable_auto_tools | bool | Whether auto tool choice is enabled. | False |
model_name | str | None | The model name for parser-specific warnings. | None |
Returns:
| Type | Description |
|---|---|
type[Parser] | None | A Parser class, or None if neither parser is specified. |
Source code in vllm/parser/parser_manager.py
get_parser_internal classmethod ¶
Retrieve a registered or lazily registered Parser class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | The registered name of the parser. | required |
Returns:
| Type | Description |
|---|---|
type[Parser] | The Parser class. |
Raises:
| Type | Description |
|---|---|
KeyError | If no parser is found under the given name. |
Source code in vllm/parser/parser_manager.py
get_reasoning_parser classmethod ¶
get_reasoning_parser(
reasoning_parser_name: str | None,
) -> type[ReasoningParser] | None
Get the reasoning parser based on the name.
Source code in vllm/parser/parser_manager.py
get_tool_parser classmethod ¶
get_tool_parser(
tool_parser_name: str | None = None,
enable_auto_tools: bool = False,
model_name: str | None = None,
) -> type[ToolParser] | None
Get the tool parser based on the name.
Source code in vllm/parser/parser_manager.py
import_parser classmethod ¶
import_parser(plugin_path: str) -> None
Import a user-defined parser from an arbitrary path.
Source code in vllm/parser/parser_manager.py
list_registered classmethod ¶
register_lazy_module classmethod ¶
Register a lazy module mapping for delayed import.
Example
ParserManager.register_lazy_module( name="minimax_m2", module_path="vllm.parser.minimax_m2_parser", class_name="MiniMaxM2Parser", )
Source code in vllm/parser/parser_manager.py
register_module classmethod ¶
register_module(
name: str | list[str] | None = None,
force: bool = True,
module: type[Parser] | None = None,
) -> type[Parser] | Callable[[type[Parser]], type[Parser]]
Register a Parser class.
Can be used as a decorator or called directly.
Usage
@ParserManager.register_module("my_parser") class MyParser(Parser): ...
Or
ParserManager.register_module(module=MyParser)
Source code in vllm/parser/parser_manager.py
_WrappedParser ¶
Bases: DelegatingParser
A DelegatingParser subclass that instantiates parsers from class attributes.
This class is used to dynamically create a parser that wraps individual ReasoningParser and ToolParser classes. The class attributes reasoning_parser_cls and tool_parser_cls should be set before instantiation.
Usage
_WrappedParser.reasoning_parser_cls = MyReasoningParser _WrappedParser.tool_parser_cls = MyToolParser parser = _WrappedParser(tokenizer)
Source code in vllm/parser/abstract_parser.py
reasoning_parser_cls class-attribute instance-attribute ¶
reasoning_parser_cls: type[ReasoningParser] | None = None
tool_parser_cls class-attribute instance-attribute ¶
tool_parser_cls: type[ToolParser] | None = None
__init__ ¶
__init__(tokenizer: TokenizerLike)