Skip to content

vllm_mlx.reasoning.deepseek_r1_parser

Reasoning parser for DeepSeek-R1 models.

View the complete module source at #L1-L114.

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

Reasoning parser for DeepSeek-R1 models.

DeepSeek-R1 uses ... tags for reasoning content. The model may sometimes start outputting reasoning without the explicit tag, so this parser is more lenient than Qwen3.

vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser

DeepSeekR1ReasoningParser(tokenizer=None)

Bases: BaseThinkingReasoningParser

Reasoning parser for DeepSeek-R1 model.

DeepSeek-R1 uses <think>...</think> tokens to denote reasoning text.
This parser is more lenient than Qwen3:
- The <think> tag may not be explicitly generated (model assumes it)
- If only </think> is found, everything before it is reasoning

Example:
    Input: "<think>Step 1: analyze...

Step 2: solve...The answer is 42." Output: reasoning="Step 1: analyze... Step 2: solve...", content="The answer is 42."

    Input: "reasoning content</think>final answer"  # No opening tag
    Output: reasoning="reasoning content", content="final answer"
Source code in vllm_mlx/reasoning/think_parser.py
def __init__(self, tokenizer=None):
    super().__init__(tokenizer)
    # Streaming state — reset per request via reset_state()
    self._phase: str = "pre_think"  # "pre_think" | "thinking" | "content"
    self._content_started = False
    self._content_buffer = ""
    # Tool call promotion state.
    self._in_tool_call = False
    self._tool_call_buffer = ""

vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser.start_token property

start_token: str

Return the marker that opens an explicit DeepSeek reasoning span.

vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser.end_token property

end_token: str

Return the marker that closes a DeepSeek reasoning span.

vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser.extract_reasoning

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

Extract reasoning from DeepSeek-R1 output.

More lenient than Qwen3 - handles cases where start tag is implicit.

Parameters:

  • model_output (str) –

    Complete model output text.

Returns:

  • tuple[str | None, str | None]

    (reasoning, content) tuple.

Source code in vllm_mlx/reasoning/deepseek_r1_parser.py
def extract_reasoning(
    self,
    model_output: str,
) -> tuple[str | None, str | None]:
    """
    Extract reasoning from DeepSeek-R1 output.

    More lenient than Qwen3 - handles cases where start tag is implicit.

    Args:
        model_output: Complete model output text.

    Returns:
        (reasoning, content) tuple.
    """
    # If we have end token but no start token, treat beginning as reasoning
    if self.end_token in model_output and self.start_token not in model_output:
        return self._extract_complete_reasoning(model_output)

    # If neither token, return as pure content
    if self.end_token not in model_output and self.start_token not in model_output:
        return None, model_output

    # Use base class for standard case
    return super().extract_reasoning(model_output)

vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser.extract_reasoning_streaming

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

Extract reasoning from streaming delta.

Handles DeepSeek-R1's pattern where may be implicit.

Parameters:

  • previous_text (str) –

    Text accumulated before this delta.

  • current_text (str) –

    Text including this delta.

  • delta_text (str) –

    Just the new text.

Returns:

  • DeltaMessage | None

    DeltaMessage with reasoning/content, or None to skip.

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

    Handles DeepSeek-R1's pattern where <think> may be implicit.

    Args:
        previous_text: Text accumulated before this delta.
        current_text: Text including this delta.
        delta_text: Just the new text.

    Returns:
        DeltaMessage with reasoning/content, or None to skip.
    """
    # First try base class logic
    result = super().extract_reasoning_streaming(
        previous_text, current_text, delta_text
    )

    # Handle DeepSeek-R1 special case: no start token seen but end token appears
    if result is not None:
        start_in_prev = self.start_token in previous_text
        start_in_delta = self.start_token in delta_text
        end_in_delta = self.end_token in delta_text

        # If end token in delta but we never saw start token
        if not start_in_prev and not start_in_delta and end_in_delta:
            # Everything before end token is reasoning
            idx = delta_text.find(self.end_token)
            reasoning_part = delta_text[:idx]
            content_part = delta_text[idx + len(self.end_token) :]
            return DeltaMessage(
                reasoning=reasoning_part if reasoning_part else None,
                content=content_part if content_part else None,
            )

        # Note: DeepSeek-R1 may omit <think> but still be in reasoning mode.
        # However, we can't reliably detect implicit reasoning without context,
        # so we default to treating unmarked content as regular content.

    return result

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.deepseek_r1_parser.DeepSeekR1ReasoningParser · class
vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser()

Reasoning parser for DeepSeek-R1 model.

Parameters

This callable has no explicit inputs.

Returns

  • Constructs: vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser

Exceptions and behavior

Class DeepSeekR1ReasoningParser derives from BaseThinkingReasoningParser and declares 4 direct member(s). No direct raise statement appears in this definition.

View source #L14-L114.

vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser.start_token · method
vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser.start_token() -> str

Return the marker that opens an explicit DeepSeek reasoning span.

Parameters

This callable has no explicit inputs.

Returns

  • Type: str
  • Direct return expressions: '<think>'

Exceptions and behavior

Method DeepSeekR1ReasoningParser.start_token returns '<think>'. No direct raise statement appears in this definition.

View source #L32-L35.

vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser.end_token · method
vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser.end_token() -> str

Return the marker that closes a DeepSeek reasoning span.

Parameters

This callable has no explicit inputs.

Returns

  • Type: str
  • Direct return expressions: '</think>'

Exceptions and behavior

Method DeepSeekR1ReasoningParser.end_token returns '</think>'. No direct raise statement appears in this definition.

View source #L38-L41.

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

Extract reasoning from DeepSeek-R1 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._extract_complete_reasoning(model_output); (None, model_output); super().extract_reasoning(model_output)

Exceptions and behavior

Method DeepSeekR1ReasoningParser.extract_reasoning calls self._extract_complete_reasoning, super().extract_reasoning, super; has 3 explicit return paths. No direct raise statement appears in this definition.

View source #L43-L67.

vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser.extract_reasoning_streaming · method
vllm_mlx.reasoning.deepseek_r1_parser.DeepSeekR1ReasoningParser.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 Text accumulated before this delta.
current_text str yes none Text including this delta.
delta_text str yes none Just the new text.

Returns

  • Type: DeltaMessage | None
  • Direct return expressions: DeltaMessage(reasoning=reasoning_part if reasoning_part else None, content=content_part if content_part else None); result

Exceptions and behavior

Method DeepSeekR1ReasoningParser.extract_reasoning_streaming calls super().extract_reasoning_streaming, super, delta_text.find, len; has 2 explicit return paths. No direct raise statement appears in this definition.

View source #L69-L114.

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
DeepSeekR1ReasoningParser class DeepSeekR1ReasoningParser() Reasoning parser for DeepSeek-R1 model. #L14-L114
DeepSeekR1ReasoningParser.start_token method DeepSeekR1ReasoningParser.start_token() -> str Return the marker that opens an explicit DeepSeek reasoning span. #L32-L35
DeepSeekR1ReasoningParser.end_token method DeepSeekR1ReasoningParser.end_token() -> str Return the marker that closes a DeepSeek reasoning span. #L38-L41
DeepSeekR1ReasoningParser.extract_reasoning method DeepSeekR1ReasoningParser.extract_reasoning(model_output: str) -> tuple[str \| None, str \| None] Extract reasoning from DeepSeek-R1 output. #L43-L67
DeepSeekR1ReasoningParser.extract_reasoning_streaming method DeepSeekR1ReasoningParser.extract_reasoning_streaming(previous_text: str, current_text: str, delta_text: str) -> DeltaMessage \| None Extract reasoning from streaming delta. #L69-L114