vllm.reasoning
Modules:
Name | Description |
---|---|
abs_reasoning_parsers |
|
deepseek_r1_reasoning_parser |
|
granite_reasoning_parser |
|
qwen3_reasoning_parser |
|
__all__
module-attribute
¶
__all__ = [
"ReasoningParser",
"ReasoningParserManager",
"DeepSeekR1ReasoningParser",
"GraniteReasoningParser",
"Qwen3ReasoningParser",
]
DeepSeekR1ReasoningParser
¶
Bases: ReasoningParser
Reasoning parser for DeepSeek R1 model.
The DeepSeek R1 model uses
Source code in vllm/reasoning/deepseek_r1_reasoning_parser.py
17 18 19 20 21 22 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 |
|
__init__
¶
Source code in vllm/reasoning/deepseek_r1_reasoning_parser.py
extract_content_ids
¶
Extract the content after the end tokens
Source code in vllm/reasoning/deepseek_r1_reasoning_parser.py
extract_reasoning_content
¶
extract_reasoning_content(
model_output: str, request: ChatCompletionRequest
) -> tuple[Optional[str], Optional[str]]
Extract reasoning content from the model output.
For text
Returns:
Type | Description |
---|---|
tuple[Optional[str], Optional[str]]
|
tuple[Optional[str], Optional[str]]: reasoning content and content |
Source code in vllm/reasoning/deepseek_r1_reasoning_parser.py
extract_reasoning_content_streaming
¶
extract_reasoning_content_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],
) -> Union[DeltaMessage, None]
Extract reasoning content from a delta message.
Handles streaming output where previous + delta = current.
Uses token IDs for faster processing.
For text
Source code in vllm/reasoning/deepseek_r1_reasoning_parser.py
GraniteReasoningParser
¶
Bases: ReasoningParser
Reasoning parser for IBM Granite.
IBM granite models currently use "Here is my thought process:" and "Here is my response:" to separate its thinking / response outputs.
Source code in vllm/reasoning/granite_reasoning_parser.py
18 19 20 21 22 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 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 |
|
longest_think_start
instance-attribute
¶
reasoning_regex
instance-attribute
¶
valid_response_starts
instance-attribute
¶
valid_think_starts
instance-attribute
¶
__init__
¶
Source code in vllm/reasoning/granite_reasoning_parser.py
_get_content_sections
¶
Parse the text to extract the reasoning content / content if we have them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_text
|
str
|
The full previous + delta text. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
tuple[Optional[str], Optional[int], Optional[str]]: Tuple of len 3 |
Optional[int]
|
containing the reasoning content, the length of the response seq |
Optional[str]
|
(if there is one) and the non-reasoning content. |
Source code in vllm/reasoning/granite_reasoning_parser.py
_get_delta_message_with_both_bounds
¶
_get_delta_message_with_both_bounds(
delta_text: str,
reasoning_content: str,
response_content: str,
current_text: str,
response_seq_len: int,
) -> DeltaMessage
Parse the delta message when the current text has both reasoning content and normal (response) content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
delta_text
|
str
|
Text to consider and parse content from. |
required |
reasoning_content
|
str
|
reasoning content from current_text. |
required |
response_content
|
str
|
response content from current_text. |
required |
current_text
|
str
|
The full previous + delta text. |
required |
response_seq_len(str)
|
Len of the complete response sequence used. |
required |
Returns:
Name | Type | Description |
---|---|---|
DeltaMessage |
DeltaMessage
|
Message containing the parsed content. |
Source code in vllm/reasoning/granite_reasoning_parser.py
_get_delta_message_with_no_reasoning_bounds
¶
_get_delta_message_with_no_reasoning_bounds(
current_text: str, delta_text: str
) -> DeltaMessage
Parse the delta message when the current text has not yet completed its start of reasoning sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_text
|
str
|
The full previous + delta text. |
required |
delta_text
|
str
|
Text to consider and parse content from. |
required |
Returns:
Name | Type | Description |
---|---|---|
DeltaMessage |
DeltaMessage
|
Message containing the parsed content. |
Source code in vllm/reasoning/granite_reasoning_parser.py
_get_delta_message_with_no_response_bounds
¶
_get_delta_message_with_no_response_bounds(
current_text: str,
reasoning_content: str,
delta_text: str,
) -> DeltaMessage
Parse the delta message when the current text has both reasoning content with no (response) content. NOTE that we may have overlapping tokens with the start of reasoning / start of response sequences on either side of the delta text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_text
|
str
|
The full previous + delta text. |
required |
reasoning_content
|
str
|
reasoning content from current_text. |
required |
delta_text
|
str
|
Text to consider and parse content from. |
required |
Returns:
Name | Type | Description |
---|---|---|
DeltaMessage |
DeltaMessage
|
Message containing the parsed content. |
Source code in vllm/reasoning/granite_reasoning_parser.py
_is_reasoning_start_substr
¶
Check if a text matches one of the possible start reasoning seqs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
Text to check for leading substr. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if any of the possible reasoning start seqs match. |
Source code in vllm/reasoning/granite_reasoning_parser.py
_is_response_start_substr
¶
Check if a text matches one of the possible start response seqs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
Text to check for leading substr. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if any of the possible response start seqs match. |
Source code in vllm/reasoning/granite_reasoning_parser.py
extract_reasoning_content
¶
extract_reasoning_content(
model_output: str, request: ChatCompletionRequest
) -> tuple[Optional[str], Optional[str]]
Extract the reasoning content & content sections, respectively. If the sequence doesn't match what we expect, i.e., the model generates something else, all content is considered non-reasoning content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_output
|
str
|
Output of the model to be parsed. |
required |
request
|
ChatCompletionRequest
|
Request being processed. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
tuple[Optional[str], Optional[str]]: Tuple pair containing the |
Optional[str]
|
reasoning content and non-reasoning content. |
Source code in vllm/reasoning/granite_reasoning_parser.py
extract_reasoning_content_streaming
¶
extract_reasoning_content_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],
) -> Union[DeltaMessage, None]
Extract the reasoning content / content emitted by granite models; If the sequence doesn't match what we expect, i.e., the model generates something else, all content is considered non-reasoning content.
NOTE: Granite models do not use a special token to start their reasoning and response sections; instead they have token sequences, e.g.,
Here is my thought process: Foo Here is my response: Bar
This increases the complexity of correctly handling streams, since we need to watch for specific sequences and correctly parse them without dropping content that is potentially overlapping & spanning multiple delta messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
previous_text
|
str
|
Previous text outside of this delta message. |
required |
current_text
|
str
|
Previous text + delta text. |
required |
delta_text
|
str
|
Text to consider and parse content from. |
required |
previous_token_ids
|
Sequence[int]
|
Token IDs of previous_text. |
required |
current_token_ids
|
Sequence[int]
|
Token IDs of current_text. |
required |
delta_token_ids
|
Sequence[int]
|
Token IDs of delta_text. |
required |
Returns:
Type | Description |
---|---|
Union[DeltaMessage, None]
|
Union[DeltaMessage, None] DeltaMessage with either reasoning content or content, or None. |
Source code in vllm/reasoning/granite_reasoning_parser.py
Qwen3ReasoningParser
¶
Bases: ReasoningParser
Reasoning parser for the Qwen3 model.
The Qwen3 model uses
Source code in vllm/reasoning/qwen3_reasoning_parser.py
17 18 19 20 21 22 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 |
|
__init__
¶
Source code in vllm/reasoning/qwen3_reasoning_parser.py
extract_content_ids
¶
Extract the content after the end tokens
Source code in vllm/reasoning/qwen3_reasoning_parser.py
extract_reasoning_content
¶
extract_reasoning_content(
model_output: str, request: ChatCompletionRequest
) -> tuple[Optional[str], Optional[str]]
Extract reasoning content from the model output.
For text
Returns:
Type | Description |
---|---|
tuple[Optional[str], Optional[str]]
|
tuple[Optional[str], Optional[str]]: reasoning content and content |
Source code in vllm/reasoning/qwen3_reasoning_parser.py
extract_reasoning_content_streaming
¶
extract_reasoning_content_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],
) -> Union[DeltaMessage, None]
Extract reasoning content from a delta message.
Handles streaming output where previous + delta = current.
Uses token IDs for faster processing.
For text
Source code in vllm/reasoning/qwen3_reasoning_parser.py
ReasoningParser
¶
Abstract reasoning parser class that should not be used directly. Provided and methods should be used in derived classes.
It is used to extract reasoning content from the model output.
Source code in vllm/reasoning/abs_reasoning_parsers.py
__init__
¶
__init__(tokenizer: AnyTokenizer)
extract_content_ids
abstractmethod
¶
Extract content token ids from the input_ids. Parameters: input_ids: list[int] The input_ids of the model output. Returns: list[int] The extracted content from the input_ids.
Source code in vllm/reasoning/abs_reasoning_parsers.py
extract_reasoning_content
abstractmethod
¶
extract_reasoning_content(
model_output: str, request: ChatCompletionRequest
) -> tuple[Optional[str], Optional[str]]
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.
model_output: str The model-generated string to extract reasoning content from.
ChatCompletionRequest
The request object that was used to generate the model_output.
tuple[Optional[str], Optional[str]] A tuple containing the reasoning content and the content.
Source code in vllm/reasoning/abs_reasoning_parsers.py
extract_reasoning_content_streaming
abstractmethod
¶
extract_reasoning_content_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],
) -> Union[DeltaMessage, None]
Instance method that should be implemented for extracting reasoning from an incomplete response; for use when handling reasoning calls and streaming. Has to be an instance method because it requires state - the current tokens/diffs, but also the information about what has previously been parsed and extracted (see constructor)
Source code in vllm/reasoning/abs_reasoning_parsers.py
is_reasoning_end
abstractmethod
¶
Check if the reasoning content ends in the input_ids.
It is used in structured engines like xgrammar
to check if the
reasoning content ends in the model output.
input_ids: list[int] The input_ids of the model output.
bool True if the reasoning content ends in the input_ids.
Source code in vllm/reasoning/abs_reasoning_parsers.py
ReasoningParserManager
¶
Source code in vllm/reasoning/abs_reasoning_parsers.py
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 |
|
_register_module
classmethod
¶
_register_module(
module: type,
module_name: Optional[Union[str, list[str]]] = None,
force: bool = True,
) -> None
Source code in vllm/reasoning/abs_reasoning_parsers.py
get_reasoning_parser
classmethod
¶
get_reasoning_parser(
name: str | None,
) -> type[ReasoningParser]
Get reasoning parser by name which is registered by register_module
.
Raise a KeyError exception if the name is not registered.
Source code in vllm/reasoning/abs_reasoning_parsers.py
import_reasoning_parser
classmethod
¶
import_reasoning_parser(plugin_path: str) -> None
Import a user-defined reasoning parser by the path of the reasoning parser define file.
Source code in vllm/reasoning/abs_reasoning_parsers.py
register_module
classmethod
¶
register_module(
name: Optional[Union[str, list[str]]] = None,
force: bool = True,
module: Union[type, None] = None,
) -> Union[type, Callable]
Register module with the given name or name list. it can be used as a decoder(with module as None) or normal function(with module as not None).