vllm_mlx.reasoning.think_parser¶
Base parser for models using
View the complete module source at #L1-L462.
API details¶
Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.
vllm_mlx.reasoning.think_parser
¶
Base parser for models using
This module provides BaseThinkingReasoningParser, a concrete implementation for extracting reasoning content from models that use thinking tags.
Supports three scenarios:
1. Both tags in output:
Performance: The streaming parser uses a simple state machine to track the
current phase (pre-think / thinking / content). Tag completion is detected
against the accumulated text for correctness when <think> / </think> are
split across delta boundaries, but phase tracking still avoids the old
whole-output rescanning behavior.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser
¶
Bases: ReasoningParser
Base parser for models using
This parser handles the common pattern where reasoning content is wrapped in special tags. Subclasses define the specific start and end tokens.
Supports "implicit reasoning mode" where
The streaming parser uses a state machine with three phases:
pre_think -> thinking -> content
Transitions are tracked by parser state. Accumulated text is consulted only to detect when a start/end tag has completed across delta boundaries.
Source code in vllm_mlx/reasoning/think_parser.py
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.start_token
abstractmethod
property
¶
The token/tag that starts reasoning content (e.g., '
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.end_token
abstractmethod
property
¶
The token/tag that ends reasoning content (e.g., '').
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._TOOL_CALL_START
class-attribute
instance-attribute
¶
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._TOOL_CALL_END
class-attribute
instance-attribute
¶
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._TOOL_CALL_CLOSED_RE
class-attribute
instance-attribute
¶
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._TOOL_CALL_UNCLOSED_RE
class-attribute
instance-attribute
¶
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._phase
instance-attribute
¶
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._content_started
instance-attribute
¶
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._content_buffer
instance-attribute
¶
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._in_tool_call
instance-attribute
¶
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._tool_call_buffer
instance-attribute
¶
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.reset_state
¶
Reset state machine for a new streaming request.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.extract_reasoning
¶
Extract reasoning from complete output.
Handles three cases:
1. Both tags present:
Parameters:
-
model_output(str) –Complete model output text.
Returns:
-
tuple[str | None, str | None]–(reasoning, content) tuple. Either may be None.
Source code in vllm_mlx/reasoning/think_parser.py
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.extract_reasoning_streaming
¶
extract_reasoning_streaming(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage | None
Extract reasoning from a streaming delta using state-machine tracking.
Instead of rescanning the full accumulated text on every token, this method tracks the current phase (pre_think / thinking / content) and only consults accumulated text to detect completed start/end tags that were split across delta boundaries.
Handles three scenarios:
1. Explicit
Parameters:
-
previous_text(str) –Text accumulated before this delta.
-
current_text(str) –Text including this delta.
-
delta_text(str) –Just the new text in this chunk.
Returns:
-
DeltaMessage | None–DeltaMessage with reasoning and/or content, or None to skip.
Source code in vllm_mlx/reasoning/think_parser.py
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 | |
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._extract_complete_reasoning
¶
Split complete output into leading reasoning spans and final content.
Source code in vllm_mlx/reasoning/think_parser.py
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._transition_to_content
¶
_transition_to_content(reasoning: str | None, content: str | None) -> DeltaMessage | None
Return a delta while suppressing leading post-transition think blocks.
Source code in vllm_mlx/reasoning/think_parser.py
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._content_delta
¶
_content_delta(delta_text: str) -> DeltaMessage | None
Emit content after consuming repeated leading think blocks.
Source code in vllm_mlx/reasoning/think_parser.py
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._thinking_tool_call
¶
_thinking_tool_call(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage | None
Handle streaming while inside a
Source code in vllm_mlx/reasoning/think_parser.py
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.finalize_stream
¶
finalize_stream() -> DeltaMessage | None
Flush any buffered tool call text at end of stream.
Source code in vllm_mlx/reasoning/think_parser.py
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._promote_tool_calls
classmethod
¶
Source code in vllm_mlx/reasoning/think_parser.py
Complete contract reference¶
Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser · class
Base parser for models using
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tokenizer |
not annotated |
no |
None |
Optional positional or keyword input; defaults to None. |
Returns
- Constructs:
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser
Exceptions and behavior
Class BaseThinkingReasoningParser derives from ReasoningParser and declares 12 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.start_token · method
The token/tag that starts reasoning content (e.g., '
Parameters
This callable has no explicit inputs.
Returns
- Type:
str
Exceptions and behavior
Method BaseThinkingReasoningParser.start_token contains no state mutation, call, raise, return, await, or yield.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.end_token · method
The token/tag that ends reasoning content (e.g., '').
Parameters
This callable has no explicit inputs.
Returns
- Type:
str
Exceptions and behavior
Method BaseThinkingReasoningParser.end_token contains no state mutation, call, raise, return, await, or yield.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.__init__ · method
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.__init__(tokenizer = None) -> not annotated
Method BaseThinkingReasoningParser.__init__ updates self._phase, self._content_started, self._content_buffer, self._in_tool_call; calls super().__init__, super.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tokenizer |
not annotated |
no |
None |
Optional positional or keyword input; defaults to None. |
Returns
- Type:
not annotated
Exceptions and behavior
Method BaseThinkingReasoningParser.__init__ updates self._phase, self._content_started, self._content_buffer, self._in_tool_call; calls super().__init__, super.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.reset_state · method
Reset state machine for a new streaming request.
Parameters
This callable has no explicit inputs.
Returns
- Type:
not annotated
Exceptions and behavior
Method BaseThinkingReasoningParser.reset_state updates self._phase, self._content_started, self._content_buffer, self._in_tool_call.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.extract_reasoning · method
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.extract_reasoning(model_output: str) -> tuple[str | None, str | None]
Extract reasoning from complete output.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
model_output |
str |
yes |
none |
Complete model output text. |
Returns
- Type:
tuple[str | None, str | None] - Direct return expressions:
self._promote_tool_calls(reasoning, content);self._promote_tool_calls(reasoning, None);(None, model_output)
Exceptions and behavior
Method BaseThinkingReasoningParser.extract_reasoning calls self._extract_complete_reasoning, self._promote_tool_calls, text.partition, reasoning.strip; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.extract_reasoning_streaming · method
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.extract_reasoning_streaming(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage | None
Extract reasoning from a streaming delta using state-machine tracking.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
previous_text |
str |
yes |
none |
Text accumulated before this delta. |
current_text |
str |
yes |
none |
Text including this delta. |
delta_text |
str |
yes |
none |
Just the new text in this chunk. |
Returns
- Type:
DeltaMessage | None - Direct return expressions:
None;self._transition_to_content(reasoning, content);DeltaMessage(reasoning=before) if before else None;DeltaMessage(reasoning=after) if after else None;DeltaMessage(reasoning=delta_text);self._thinking_tool_call(previous_text, current_text, delta_text);DeltaMessage(reasoning=reasoning) if reasoning else None;self._content_delta(delta_text)
Exceptions and behavior
Method BaseThinkingReasoningParser.extract_reasoning_streaming updates self._phase, self._in_tool_call, self._tool_call_buffer; calls delta_text.find, len, after.find, self._transition_to_content; has 8 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._extract_complete_reasoning · method
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._extract_complete_reasoning(text: str) -> tuple[str | None, str | None]
Split complete output into leading reasoning spans and final content.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[str | None, str | None] - Direct return expressions:
(reasoning, content)
Exceptions and behavior
Method BaseThinkingReasoningParser._extract_complete_reasoning calls remainder.lstrip, stripped.startswith, len, after_start.partition; returns (reasoning, content).
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._transition_to_content · method
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._transition_to_content(reasoning: str | None, content: str | None) -> DeltaMessage | None
Return a delta while suppressing leading post-transition think blocks.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
reasoning |
str \| None |
yes |
none |
Required positional or keyword input. |
content |
str \| None |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
DeltaMessage | None - Direct return expressions:
None;DeltaMessage(reasoning=reasoning_text or None, content=final_content or None)
Exceptions and behavior
Method BaseThinkingReasoningParser._transition_to_content calls self._promote_tool_calls, self._content_delta, DeltaMessage; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._content_delta · method
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._content_delta(delta_text: str) -> DeltaMessage | None
Emit content after consuming repeated leading think blocks.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
delta_text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
DeltaMessage | None - Direct return expressions:
None;DeltaMessage(content=delta_text) if delta_text else None;DeltaMessage(reasoning=''.join(reasoning_parts) or None, content=buffer);DeltaMessage(reasoning=''.join(reasoning_parts))
Exceptions and behavior
Method BaseThinkingReasoningParser._content_delta updates self._content_buffer, self._content_started; calls DeltaMessage, self._content_buffer.lstrip, buffer.startswith, buffer[len(self.end_token):].lstrip; has 4 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._thinking_tool_call · method
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._thinking_tool_call(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage | None
Handle streaming while inside a
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
previous_text |
str |
yes |
none |
Required positional or keyword input. |
current_text |
str |
yes |
none |
Required positional or keyword input. |
delta_text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
DeltaMessage | None - Direct return expressions:
DeltaMessage(content=final_content or None, reasoning=r_text or None);DeltaMessage(content=promoted, reasoning=reasoning);DeltaMessage(content=final_content or None);None
Exceptions and behavior
Method BaseThinkingReasoningParser._thinking_tool_call updates self._tool_call_buffer, self._in_tool_call, self._phase; calls self._tool_call_buffer.find, len, logger.warning, remainder.find; has 4 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.finalize_stream · method
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser.finalize_stream() -> DeltaMessage | None
Flush any buffered tool call text at end of stream.
Parameters
This callable has no explicit inputs.
Returns
- Type:
DeltaMessage | None - Direct return expressions:
DeltaMessage(content=promoted);None
Exceptions and behavior
Method BaseThinkingReasoningParser.finalize_stream updates self._tool_call_buffer, self._in_tool_call; calls logger.warning, DeltaMessage; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._promote_tool_calls · method
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._promote_tool_calls(reasoning: str | None, content: str | None) -> tuple[str | None, str | None]
Method BaseThinkingReasoningParser._promote_tool_calls calls cls._TOOL_CALL_CLOSED_RE.sub, cls._TOOL_CALL_UNCLOSED_RE.search, unclosed_match.group, unclosed_match.start; has 2 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
reasoning |
str \| None |
yes |
none |
Required positional or keyword input. |
content |
str \| None |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[str | None, str | None] - Direct return expressions:
(reasoning, content);(cleaned, result_content)
Exceptions and behavior
Method BaseThinkingReasoningParser._promote_tool_calls calls cls._TOOL_CALL_CLOSED_RE.sub, cls._TOOL_CALL_UNCLOSED_RE.search, unclosed_match.group, unclosed_match.start; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._promote_tool_calls._collect_closed · nested function
vllm_mlx.reasoning.think_parser.BaseThinkingReasoningParser._promote_tool_calls._collect_closed(match) -> not annotated
Nested Function BaseThinkingReasoningParser._promote_tool_calls._collect_closed calls closed.append, match.group; returns ''.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
match |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated - Direct return expressions:
''
Exceptions and behavior
Nested Function BaseThinkingReasoningParser._promote_tool_calls._collect_closed calls closed.append, match.group; returns ''.
No direct raise statement appears in this definition.
Complete symbol map¶
This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.
| Symbol | Kind | Signature and inputs | What it does | Source |
|---|---|---|---|---|
BaseThinkingReasoningParser |
class | BaseThinkingReasoningParser(tokenizer = None) |
Base parser for models using |
#L29-L462 |
BaseThinkingReasoningParser.start_token |
method | BaseThinkingReasoningParser.start_token() -> str |
The token/tag that starts reasoning content (e.g., ' |
#L50-L51 |
BaseThinkingReasoningParser.end_token |
method | BaseThinkingReasoningParser.end_token() -> str |
The token/tag that ends reasoning content (e.g., ''). | #L55-L56 |
BaseThinkingReasoningParser.__init__ |
method | BaseThinkingReasoningParser.__init__(tokenizer = None) -> not annotated |
Method BaseThinkingReasoningParser.__init__ updates self._phase, self._content_started, self._content_buffer, self._in_tool_call; calls super().__init__, super. |
#L63-L71 |
BaseThinkingReasoningParser.reset_state |
method | BaseThinkingReasoningParser.reset_state() -> not annotated |
Reset state machine for a new streaming request. | #L73-L79 |
BaseThinkingReasoningParser.extract_reasoning |
method | BaseThinkingReasoningParser.extract_reasoning(model_output: str) -> tuple[str \| None, str \| None] |
Extract reasoning from complete output. | #L81-L110 |
BaseThinkingReasoningParser.extract_reasoning_streaming |
method | BaseThinkingReasoningParser.extract_reasoning_streaming(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage \| None |
Extract reasoning from a streaming delta using state-machine tracking. | #L112-L224 |
BaseThinkingReasoningParser._extract_complete_reasoning |
method | BaseThinkingReasoningParser._extract_complete_reasoning(text: str) -> tuple[str \| None, str \| None] |
Split complete output into leading reasoning spans and final content. | #L226-L260 |
BaseThinkingReasoningParser._transition_to_content |
method | BaseThinkingReasoningParser._transition_to_content(reasoning: str \| None, content: str \| None) -> DeltaMessage \| None |
Return a delta while suppressing leading post-transition think blocks. | #L262-L276 |
BaseThinkingReasoningParser._content_delta |
method | BaseThinkingReasoningParser._content_delta(delta_text: str) -> DeltaMessage \| None |
Emit content after consuming repeated leading think blocks. | #L278-L325 |
BaseThinkingReasoningParser._thinking_tool_call |
method | BaseThinkingReasoningParser._thinking_tool_call(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage \| None |
Handle streaming while inside a |
#L327-L396 |
BaseThinkingReasoningParser.finalize_stream |
method | BaseThinkingReasoningParser.finalize_stream() -> DeltaMessage \| None |
Flush any buffered tool call text at end of stream. | #L398-L406 |
BaseThinkingReasoningParser._promote_tool_calls |
method | BaseThinkingReasoningParser._promote_tool_calls(reasoning: str \| None, content: str \| None) -> tuple[str \| None, str \| None] |
Method BaseThinkingReasoningParser._promote_tool_calls calls cls._TOOL_CALL_CLOSED_RE.sub, cls._TOOL_CALL_UNCLOSED_RE.search, unclosed_match.group, unclosed_match.start; has 2 explicit return paths. |
#L409-L462 |
BaseThinkingReasoningParser._promote_tool_calls._collect_closed |
nested function | BaseThinkingReasoningParser._promote_tool_calls._collect_closed(match) -> not annotated |
Nested Function BaseThinkingReasoningParser._promote_tool_calls._collect_closed calls closed.append, match.group; returns ''. |
#L419-L421 |