vllm_mlx.reasoning.gemma4_parser¶
Reasoning parser for Gemma 4 models.
View the complete module source at #L1-L386.
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.gemma4_parser
¶
Reasoning parser for Gemma 4 models.
Gemma 4 uses a channel-based protocol for reasoning:
<|channel>thought
...thinking content...
<channel|>
...response content...
Where
<|channel> = token 100 (channel switch marker)
The channel names "thought" and "response" appear as text after the special tokens and should be stripped from the output.
Some model variants may use <|channel>response instead of
When thinking is disabled or not triggered, output contains no tags.
Degenerate cycling
On long prompts with tools, Gemma 4 may oscillate between thought and
response channels many times, producing garbage reasoning before finally
emitting valid content/tool_calls. The parser handles this by splitting
at the LAST
vllm_mlx.reasoning.gemma4_parser._RESPONSE_MARKER
module-attribute
¶
vllm_mlx.reasoning.gemma4_parser._THOUGHT_MARKER
module-attribute
¶
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser
¶
Bases: BaseThinkingReasoningParser
Reasoning parser for Gemma 4 models.
Handles two transition formats:
1. <|channel>thought...
Channel names ("thought", "response") are stripped from output.
Example
Input: "<|channel>thought\nLet me think...
When no tags are present, the entire output is treated as content.
Degenerate cycling (long prompts + tools):
Uses rpartition to split at the LAST
Streaming buffering
Partial markers at a delta boundary (e.g. "<|channel>" without a following "response" yet) are buffered internally so they don't leak into reasoning/content. The buffer is either consumed when the marker completes in a later delta, or flushed as reasoning via finalize_stream() when the stream ends.
Source code in vllm_mlx/reasoning/gemma4_parser.py
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.start_token
property
¶
Return Gemma's marker for entering the thought channel.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.end_token
property
¶
Return Gemma's marker for entering the response channel.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._pending
instance-attribute
¶
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._content_seen
instance-attribute
¶
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.reset_state
¶
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._trailing_partial_marker_len
¶
Return length of trailing substring of text that is a proper prefix
of any transition marker (
Only counts PROPER prefixes — if the marker is already complete in
text, no buffering is needed. Returns 0 if no partial match.
We must never buffer legitimate content. For <|channel>, only buffer
when it appears AT THE END and is not followed by more text (i.e.,
response or thought hasn't arrived yet).
Source code in vllm_mlx/reasoning/gemma4_parser.py
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.finalize_stream
¶
finalize_stream() -> DeltaMessage | None
Flush any buffered partial marker at the end of stream.
If the stream ends while we have a partial marker buffered (e.g. model emitted "<|channel>" as its last token and got truncated by max_tokens), emit it as reasoning so the client doesn't lose the text. Content phase flushes as content.
Source code in vllm_mlx/reasoning/gemma4_parser.py
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.extract_reasoning
¶
Extract reasoning from complete output.
Uses rpartition (LAST
Source code in vllm_mlx/reasoning/gemma4_parser.py
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.extract_reasoning_streaming
¶
extract_reasoning_streaming(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage | None
Extract reasoning from streaming delta.
Handles:
- No tags: treat as content (Gemma 4 doesn't inject tags in prompt)
- <|channel>thought: enter reasoning mode, strip channel name
-
Partial markers at delta boundaries are buffered internally to prevent leaking them as reasoning/content.
Source code in vllm_mlx/reasoning/gemma4_parser.py
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._strip_channel_tokens_from_delta
staticmethod
¶
_strip_channel_tokens_from_delta(msg: DeltaMessage | None) -> DeltaMessage | None
Strip channel special tokens from content and reasoning in a delta.
Source code in vllm_mlx/reasoning/gemma4_parser.py
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._extract_from_safe_text
¶
_extract_from_safe_text(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage | None
Parse safe (non-buffered) text.
Uses count-based detection for channel tokens so that multiple
thought/response cycles (degenerate model behaviour) are handled
correctly — each NEW <|channel> re-enters reasoning, each NEW
Source code in vllm_mlx/reasoning/gemma4_parser.py
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 | |
vllm_mlx.reasoning.gemma4_parser._strip_channel_name
¶
Strip channel name and leading whitespace/newline from text start.
vllm_mlx.reasoning.gemma4_parser._strip_channel_tokens
¶
Remove all channel special tokens and bare channel names from text.
Handles degenerate model output with multiple thought/response cycles by stripping all protocol tokens, leaving only the actual text content.
Source code in vllm_mlx/reasoning/gemma4_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.gemma4_parser._strip_channel_name · function
Strip channel name and leading whitespace/newline from text start.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
prefix |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str - Direct return expressions:
text.lstrip('\n')
Exceptions and behavior
Function _strip_channel_name calls text.startswith, len, text.lstrip; returns text.lstrip('\n').
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser._strip_channel_tokens · function
Remove all channel special tokens and bare channel names from text.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str - Direct return expressions:
text.strip()
Exceptions and behavior
Function _strip_channel_tokens calls text.replace, text.split, line.strip, cleaned.append; returns text.strip().
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser · class
Reasoning parser for Gemma 4 models.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tokenizer |
not annotated |
no |
None |
Optional positional or keyword input; defaults to None. |
Returns
- Constructs:
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser
Exceptions and behavior
Class Gemma4ReasoningParser derives from BaseThinkingReasoningParser and declares 10 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.start_token · method
Return Gemma's marker for entering the thought channel.
Parameters
This callable has no explicit inputs.
Returns
- Type:
str - Direct return expressions:
'<|channel>'
Exceptions and behavior
Method Gemma4ReasoningParser.start_token returns '<|channel>'.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.end_token · method
Return Gemma's marker for entering the response channel.
Parameters
This callable has no explicit inputs.
Returns
- Type:
str - Direct return expressions:
'<channel|>'
Exceptions and behavior
Method Gemma4ReasoningParser.end_token returns '<channel|>'.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.__init__ · method
Method Gemma4ReasoningParser.__init__ updates self._pending, self._content_seen; 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 Gemma4ReasoningParser.__init__ updates self._pending, self._content_seen; calls super().__init__, super.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.reset_state · method
Reset base parsing state and buffered Gemma channel markers.
Parameters
This callable has no explicit inputs.
Returns
- Type:
not annotated
Exceptions and behavior
Method Gemma4ReasoningParser.reset_state updates self._pending, self._content_seen; calls super().reset_state, super.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._trailing_partial_marker_len · method
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._trailing_partial_marker_len(text: str) -> int
Return length of trailing substring of text that is a proper prefix of any transition marker (
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
int - Direct return expressions:
max_len
Exceptions and behavior
Method Gemma4ReasoningParser._trailing_partial_marker_len calls range, min, len, text.endswith; returns max_len.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.finalize_stream · method
Flush any buffered partial marker at the end of stream.
Parameters
This callable has no explicit inputs.
Returns
- Type:
DeltaMessage | None - Direct return expressions:
None;DeltaMessage(content=pending);DeltaMessage(reasoning=pending)
Exceptions and behavior
Method Gemma4ReasoningParser.finalize_stream updates self._pending; calls DeltaMessage; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.extract_reasoning · method
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.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 |
Required positional or keyword input. |
Returns
- Type:
tuple[str | None, str | None] - Direct return expressions:
(reasoning or None, content or None);(reasoning or None, None);(None, model_output)
Exceptions and behavior
Method Gemma4ReasoningParser.extract_reasoning calls text.partition, after_start.rpartition, _strip_channel_tokens, text.count; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.extract_reasoning_streaming · method
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser.extract_reasoning_streaming(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage | None
Extract reasoning from streaming delta.
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:
None;self._extract_from_safe_text(safe_previous, safe_current, safe_delta)
Exceptions and behavior
Method Gemma4ReasoningParser.extract_reasoning_streaming updates self._pending; calls self._trailing_partial_marker_len, len, self._extract_from_safe_text; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._strip_channel_tokens_from_delta · method
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._strip_channel_tokens_from_delta(msg: DeltaMessage | None) -> DeltaMessage | None
Strip channel special tokens from content and reasoning in a delta.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
msg |
DeltaMessage \| None |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
DeltaMessage | None - Direct return expressions:
None;msg;DeltaMessage(reasoning=r or None, content=c or None)
Exceptions and behavior
Method Gemma4ReasoningParser._strip_channel_tokens_from_delta calls c.replace('<channel|>', '').replace, c.replace, r.replace('<channel|>', '').replace, r.replace; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._extract_from_safe_text · method
vllm_mlx.reasoning.gemma4_parser.Gemma4ReasoningParser._extract_from_safe_text(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage | None
Parse safe (non-buffered) text.
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=delta_text);self._strip_channel_tokens_from_delta(DeltaMessage(content=after_marker));None;DeltaMessage(content=after);self._strip_channel_tokens_from_delta(DeltaMessage(content=stripped));self._strip_channel_tokens_from_delta(DeltaMessage(content=delta_text));DeltaMessage(reasoning=r) if r else None;DeltaMessage(reasoning=delta_text) if delta_text else None
Exceptions and behavior
Method Gemma4ReasoningParser._extract_from_safe_text updates self._phase, self._content_seen; calls DeltaMessage, current_text.find, len, after_marker.lstrip; has 8 explicit return paths.
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 |
|---|---|---|---|---|
_strip_channel_name |
function | _strip_channel_name(text: str, prefix: str) -> str |
Strip channel name and leading whitespace/newline from text start. | #L46-L50 |
_strip_channel_tokens |
function | _strip_channel_tokens(text: str) -> str |
Remove all channel special tokens and bare channel names from text. | #L53-L82 |
Gemma4ReasoningParser |
class | Gemma4ReasoningParser(tokenizer = None) |
Reasoning parser for Gemma 4 models. | #L85-L386 |
Gemma4ReasoningParser.start_token |
method | Gemma4ReasoningParser.start_token() -> str |
Return Gemma's marker for entering the thought channel. | #L115-L118 |
Gemma4ReasoningParser.end_token |
method | Gemma4ReasoningParser.end_token() -> str |
Return Gemma's marker for entering the response channel. | #L121-L124 |
Gemma4ReasoningParser.__init__ |
method | Gemma4ReasoningParser.__init__(tokenizer = None) -> not annotated |
Method Gemma4ReasoningParser.__init__ updates self._pending, self._content_seen; calls super().__init__, super. |
#L126-L133 |
Gemma4ReasoningParser.reset_state |
method | Gemma4ReasoningParser.reset_state() -> not annotated |
Reset base parsing state and buffered Gemma channel markers. | #L135-L140 |
Gemma4ReasoningParser._trailing_partial_marker_len |
method | Gemma4ReasoningParser._trailing_partial_marker_len(text: str) -> int |
Return length of trailing substring of text that is a proper prefix of any transition marker ( |
#L142-L166 |
Gemma4ReasoningParser.finalize_stream |
method | Gemma4ReasoningParser.finalize_stream() -> DeltaMessage \| None |
Flush any buffered partial marker at the end of stream. | #L168-L183 |
Gemma4ReasoningParser.extract_reasoning |
method | Gemma4ReasoningParser.extract_reasoning(model_output: str) -> tuple[str \| None, str \| None] |
Extract reasoning from complete output. | #L185-L233 |
Gemma4ReasoningParser.extract_reasoning_streaming |
method | Gemma4ReasoningParser.extract_reasoning_streaming(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage \| None |
Extract reasoning from streaming delta. | #L235-L272 |
Gemma4ReasoningParser._strip_channel_tokens_from_delta |
method | Gemma4ReasoningParser._strip_channel_tokens_from_delta(msg: DeltaMessage \| None) -> DeltaMessage \| None |
Strip channel special tokens from content and reasoning in a delta. | #L275-L291 |
Gemma4ReasoningParser._extract_from_safe_text |
method | Gemma4ReasoningParser._extract_from_safe_text(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage \| None |
Parse safe (non-buffered) text. | #L293-L386 |