vllm.parser.abstract_parser ¶
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
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | |
_parse_tool_calls ¶
_parse_tool_calls(
request: ResponsesRequest,
content: str | None,
enable_auto_tools: bool,
) -> tuple[list[FunctionCall], str | None]
TODO(qandrew): merge _parse_tool_calls_from_content for ChatCompletions into this function Parse tool calls from content based on request tool_choice settings.
Returns:
| Type | Description |
|---|---|
list[FunctionCall] | A tuple of (function_calls, remaining_content) if tool calls |
str | None | were parsed |
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_response_outputs ¶
extract_response_outputs(
model_output: str,
request: ResponsesRequest,
enable_auto_tools: bool = False,
tool_call_id_type: str = "random",
logprobs: list[Logprob] | None = None,
) -> list[ResponseOutputItem]
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
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 | |
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_response_outputs abstractmethod ¶
extract_response_outputs(
model_output: str,
request: ResponsesRequest,
enable_auto_tools: bool = False,
tool_call_id_type: str = "random",
logprobs: list[Logprob] | None = None,
) -> list[ResponseOutputItem]
Extract reasoning, content, and tool calls from a complete model-generated string and return as ResponseOutputItem objects.
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 | ResponsesRequest | The request object used to generate the output. | required |
enable_auto_tools | bool | Whether to enable automatic tool call parsing. | False |
tool_call_id_type | str | Type of tool call ID generation ("random", etc). | 'random' |
logprobs | list[Logprob] | None | Pre-computed logprobs for the output text, if any. | None |
Returns:
| Type | Description |
|---|---|
list[ResponseOutputItem] | A list of ResponseOutputItem objects. |
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
_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)