Skip to content

vllm_mlx.reasoning.base

Base classes for reasoning content extraction.

View the complete module source at #L1-L126.

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.base

Base classes for reasoning content extraction.

This module provides the abstract base class for reasoning parsers that extract thinking/reasoning content from model outputs (e.g., ... tags).

vllm_mlx.reasoning.base.DeltaMessage dataclass

DeltaMessage(role: str | None = None, content: str | None = None, reasoning: str | None = None)

Delta message for streaming reasoning output.

Contains either reasoning content, regular content, or both when transitioning from reasoning to content phase.

Note: reasoning and content should typically not both be non-None except during the transition chunk.

vllm_mlx.reasoning.base.DeltaMessage.role class-attribute instance-attribute

role: str | None = None

vllm_mlx.reasoning.base.DeltaMessage.content class-attribute instance-attribute

content: str | None = None

vllm_mlx.reasoning.base.DeltaMessage.reasoning class-attribute instance-attribute

reasoning: str | None = None

vllm_mlx.reasoning.base.DeltaMessage.reasoning_content property

reasoning_content: str | None

Deprecated: use reasoning instead. Maintained for backward compatibility.

vllm_mlx.reasoning.base.ReasoningParser

ReasoningParser(tokenizer: Any | None = None)

Bases: ABC

Abstract base class for reasoning content extraction.

Reasoning parsers extract thinking/reasoning content from model outputs, separating it from the final response content. This is useful for models like DeepSeek-R1, Qwen3, etc. that use special tokens to denote reasoning.

Example

Input: "Let me solve this step by step...The answer is 42." Output: reasoning="Let me solve this step by step...", content="The answer is 42."

Initialize parser with optional tokenizer.

Parameters:

  • tokenizer (Any | None, default: None ) –

    Optional tokenizer for token-based parsing. For vllm-mlx, text-based parsing is sufficient, so this is optional.

Source code in vllm_mlx/reasoning/base.py
def __init__(self, tokenizer: Any | None = None):
    """
    Initialize parser with optional tokenizer.

    Args:
        tokenizer: Optional tokenizer for token-based parsing. For vllm-mlx,
                  text-based parsing is sufficient, so this is optional.
    """
    self.tokenizer = tokenizer

vllm_mlx.reasoning.base.ReasoningParser.tokenizer instance-attribute

tokenizer = tokenizer

vllm_mlx.reasoning.base.ReasoningParser.extract_reasoning abstractmethod

extract_reasoning(model_output: str) -> tuple[str | None, str | None]

Extract reasoning content from complete model output.

Parameters:

  • model_output (str) –

    Complete text output from the model.

Returns:

  • str | None

    Tuple of (reasoning_content, final_content).

  • str | None

    Either may be None if not present.

Source code in vllm_mlx/reasoning/base.py
@abstractmethod
def extract_reasoning(
    self,
    model_output: str,
) -> tuple[str | None, str | None]:
    """
    Extract reasoning content from complete model output.

    Args:
        model_output: Complete text output from the model.

    Returns:
        Tuple of (reasoning_content, final_content).
        Either may be None if not present.
    """
    pass

vllm_mlx.reasoning.base.ReasoningParser.extract_reasoning_streaming abstractmethod

extract_reasoning_streaming(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage | None

Extract reasoning from streaming delta.

Uses the "previous + delta = current" model where: - previous_text: All text accumulated before this delta - current_text: All text including this delta (previous + delta) - delta_text: Just the new text in this chunk

Parameters:

  • previous_text (str) –

    Accumulated text before this delta.

  • current_text (str) –

    Accumulated text including this delta.

  • delta_text (str) –

    The new text in this streaming chunk.

Returns:

  • DeltaMessage | None

    DeltaMessage with reasoning and/or content populated,

  • DeltaMessage | None

    or None if this delta should be skipped (e.g., special tokens).

Source code in vllm_mlx/reasoning/base.py
@abstractmethod
def extract_reasoning_streaming(
    self,
    previous_text: str,
    current_text: str,
    delta_text: str,
) -> DeltaMessage | None:
    """
    Extract reasoning from streaming delta.

    Uses the "previous + delta = current" model where:
    - previous_text: All text accumulated before this delta
    - current_text: All text including this delta (previous + delta)
    - delta_text: Just the new text in this chunk

    Args:
        previous_text: Accumulated text before this delta.
        current_text: Accumulated text including this delta.
        delta_text: The new text in this streaming chunk.

    Returns:
        DeltaMessage with reasoning and/or content populated,
        or None if this delta should be skipped (e.g., special tokens).
    """
    pass

vllm_mlx.reasoning.base.ReasoningParser.reset_state

reset_state()

Reset any internal state for a new request.

Called before starting to process a new streaming request. Override in subclasses if stateful parsing is needed. This is intentionally a default no-op implementation.

Source code in vllm_mlx/reasoning/base.py
def reset_state(self):  # noqa: B027
    """
    Reset any internal state for a new request.

    Called before starting to process a new streaming request.
    Override in subclasses if stateful parsing is needed.
    This is intentionally a default no-op implementation.
    """
    pass

vllm_mlx.reasoning.base.ReasoningParser.finalize_stream

finalize_stream() -> DeltaMessage | None

Finalize streaming state at end of stream.

Called after the last delta is processed but before the stream closes. Parsers that buffer partial markers internally should flush any remaining text here.

Default implementation is a no-op (returns None).

Returns:

  • DeltaMessage | None

    DeltaMessage with any pending reasoning/content to emit,

  • DeltaMessage | None

    or None if nothing to flush.

Source code in vllm_mlx/reasoning/base.py
def finalize_stream(self) -> DeltaMessage | None:  # noqa: B027
    """
    Finalize streaming state at end of stream.

    Called after the last delta is processed but before the stream
    closes. Parsers that buffer partial markers internally should
    flush any remaining text here.

    Default implementation is a no-op (returns None).

    Returns:
        DeltaMessage with any pending reasoning/content to emit,
        or None if nothing to flush.
    """
    return None

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.base.DeltaMessage · class
vllm_mlx.reasoning.base.DeltaMessage(role: str | None = None, content: str | None = None, reasoning: str | None = None)

Delta message for streaming reasoning output.

Parameters

Name Type Required Default Description
role str \| None no None Optional constructor field; defaults to None.
content str \| None no None Optional constructor field; defaults to None.
reasoning str \| None no None Optional constructor field; defaults to None.

Returns

  • Constructs: vllm_mlx.reasoning.base.DeltaMessage

Exceptions and behavior

Class DeltaMessage declares 1 direct member(s). No direct raise statement appears in this definition.

View source #L15-L33.

vllm_mlx.reasoning.base.DeltaMessage.reasoning_content · method
vllm_mlx.reasoning.base.DeltaMessage.reasoning_content() -> str | None

Deprecated: use reasoning instead.

Parameters

This callable has no explicit inputs.

Returns

  • Type: str | None
  • Direct return expressions: self.reasoning

Exceptions and behavior

Method DeltaMessage.reasoning_content returns self.reasoning. No direct raise statement appears in this definition.

View source #L31-L33.

vllm_mlx.reasoning.base.ReasoningParser · class
vllm_mlx.reasoning.base.ReasoningParser(tokenizer: Any | None = None)

Abstract base class for reasoning content extraction.

Parameters

Name Type Required Default Description
tokenizer Any \| None no None Optional tokenizer for token-based parsing. For vllm-mlx, text-based parsing is sufficient, so this is optional.

Returns

  • Constructs: vllm_mlx.reasoning.base.ReasoningParser

Exceptions and behavior

Class ReasoningParser derives from ABC and declares 5 direct member(s). No direct raise statement appears in this definition.

View source #L36-L126.

vllm_mlx.reasoning.base.ReasoningParser.__init__ · method
vllm_mlx.reasoning.base.ReasoningParser.__init__(tokenizer: Any | None = None) -> not annotated

Initialize parser with optional tokenizer.

Parameters

Name Type Required Default Description
tokenizer Any \| None no None Optional tokenizer for token-based parsing. For vllm-mlx, text-based parsing is sufficient, so this is optional.

Returns

  • Type: not annotated

Exceptions and behavior

Method ReasoningParser.__init__ updates self.tokenizer. No direct raise statement appears in this definition.

View source #L49-L57.

vllm_mlx.reasoning.base.ReasoningParser.extract_reasoning · method
vllm_mlx.reasoning.base.ReasoningParser.extract_reasoning(model_output: str) -> tuple[str | None, str | None]

Extract reasoning content from complete model output.

Parameters

Name Type Required Default Description
model_output str yes none Complete text output from the model.

Returns

  • Type: tuple[str | None, str | None]

Exceptions and behavior

Method ReasoningParser.extract_reasoning contains no state mutation, call, raise, return, await, or yield. No direct raise statement appears in this definition.

View source #L60-L74.

vllm_mlx.reasoning.base.ReasoningParser.extract_reasoning_streaming · method
vllm_mlx.reasoning.base.ReasoningParser.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 Accumulated text before this delta.
current_text str yes none Accumulated text including this delta.
delta_text str yes none The new text in this streaming chunk.

Returns

  • Type: DeltaMessage | None

Exceptions and behavior

Method ReasoningParser.extract_reasoning_streaming contains no state mutation, call, raise, return, await, or yield. No direct raise statement appears in this definition.

View source #L77-L100.

vllm_mlx.reasoning.base.ReasoningParser.reset_state · method
vllm_mlx.reasoning.base.ReasoningParser.reset_state() -> not annotated

Reset any internal state for a new request.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated

Exceptions and behavior

Method ReasoningParser.reset_state contains no state mutation, call, raise, return, await, or yield. No direct raise statement appears in this definition.

View source #L102-L110.

vllm_mlx.reasoning.base.ReasoningParser.finalize_stream · method
vllm_mlx.reasoning.base.ReasoningParser.finalize_stream() -> DeltaMessage | None

Finalize streaming state at end of stream.

Parameters

This callable has no explicit inputs.

Returns

  • Type: DeltaMessage | None
  • Direct return expressions: None

Exceptions and behavior

Method ReasoningParser.finalize_stream returns None. No direct raise statement appears in this definition.

View source #L112-L126.

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
DeltaMessage class DeltaMessage(role: str \| None = None, content: str \| None = None, reasoning: str \| None = None) Delta message for streaming reasoning output. #L15-L33
DeltaMessage.reasoning_content method DeltaMessage.reasoning_content() -> str \| None Deprecated: use reasoning instead. #L31-L33
ReasoningParser class ReasoningParser(tokenizer: Any \| None = None) Abstract base class for reasoning content extraction. #L36-L126
ReasoningParser.__init__ method ReasoningParser.__init__(tokenizer: Any \| None = None) -> not annotated Initialize parser with optional tokenizer. #L49-L57
ReasoningParser.extract_reasoning method ReasoningParser.extract_reasoning(model_output: str) -> tuple[str \| None, str \| None] Extract reasoning content from complete model output. #L60-L74
ReasoningParser.extract_reasoning_streaming method ReasoningParser.extract_reasoning_streaming(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage \| None Extract reasoning from streaming delta. #L77-L100
ReasoningParser.reset_state method ReasoningParser.reset_state() -> not annotated Reset any internal state for a new request. #L102-L110
ReasoningParser.finalize_stream method ReasoningParser.finalize_stream() -> DeltaMessage \| None Finalize streaming state at end of stream. #L112-L126