Skip to content

vllm.reasoning.abs_reasoning_parsers

Classes:

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.

Methods:

Attributes:

implicit_reasoning_end_token_ids property

Reasoning-end tokens that are also content, e.g. a tool-call start.

reasoning_end_str property

Set reasoning_end_str to the strings that delimit the reasoning block (e.g. ""</seed:think>"" and "</think>").

reasoning_end_token_ids property

Every token id that alone ends reasoning.

Empty if reasoning can also end in a way that is not a single token.

reasoning_start_str property

Set reasoning_start_str to the strings that delimit the reasoning block (e.g. ""<seed:think>"" and "<think>").

adjust_initial_state_from_prompt(prompt_token_ids)

Hook called once at the start of streaming with the prompt tokens.

Gives parsers a chance to adjust their initial parsing state based on the prompt — for example, when the chat template leaves the prompt inside an open reasoning channel and the engine's default initial state would otherwise misclassify the first generated tokens.

Default is a no-op; override in subclasses as needed.

adjust_request(request)

Adjust request parameters; override in subclasses as needed.

count_reasoning_tokens(token_ids)

Count the number of reasoning tokens in a sequence.

Text-based reasoning models typically wrap their chain-of-thought between special start/end tokens (e.g., <think> ... </think>). Implementations that support reasoning token counting should override this method. The default implementation returns 0 so existing parsers remain unchanged unless they explicitly opt in.

Parameters:

  • token_ids

    (Sequence[int]) –

    Sequence of generated token ids (excluding prompt).

Returns:

  • int ( int ) –

    Number of tokens that belong to reasoning content.

extract_content_ids(input_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.

extract_reasoning(model_output, request) abstractmethod

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.

Parameters:

Returns:

  • tuple[str | None, str | None] –

    A tuple containing the reasoning content and the content.

extract_reasoning_streaming(previous_text, current_text, delta_text, previous_token_ids, current_token_ids, delta_token_ids) abstractmethod

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)

find_reasoning_end(token_ids)

Find the first reasoning end in token_ids.

Returns its offset and whether its token is content, or ReasoningEnd(len(token_ids), False) if there is none. Returns None if the parser cannot locate reasoning ends by token id.

has_engine_confirmed_reasoning_end()

Whether the engine has confirmed the reasoning end transition.

Engine-based parsers may defer terminal processing when the detokenizer holds back text. This method returns the engine's processed state, not a raw token-ID check.

Only called for parsers with engine_based_streaming = True.

is_reasoning_end(input_ids) 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.

Parameters:

  • input_ids

    (Sequence[int]) –

    The input_ids of the model output.

Returns:

  • bool –

    True if the reasoning content ends in the input_ids.

is_reasoning_end_streaming(input_ids, delta_ids)

Check if the reasoning content ends in the input_ids on a decode step.

It is used in structured engines like xgrammar to check if the reasoning content ends in the model output during a decode step. input_ids the entire model output and delta_ids are the last few computed tokens of the model output (like during a decode step).

Parameters:

  • input_ids

    (Sequence[int]) –

    The entire model output.

  • delta_ids

    (Iterable[int]) –

    The last few computed tokens of the model output at the current decode step.

Returns:

  • bool –

    True if the reasoning content ends in the delta_ids on a

  • bool –

    decode step.

prepare_structured_tag(original_tag, tool_server)

Instance method that is implemented for preparing the structured tag

set_prompt_token_ids(prompt_token_ids)

Provide the rendered prompt before the output is parsed.

Parsers that can tell whether the prompt already closes reasoning (e.g. continue_final_message) use it to classify the output. Default is a no-op; override in subclasses as needed.

ReasoningParserManager

Central registry for ReasoningParser implementations.

Supports two registration modes
  • Eager registration via register_module
  • Lazy registration via register_lazy_module

Each reasoning parser must inherit from ReasoningParser.

Methods:

_load_lazy_parser(name) classmethod

Import and register a lazily loaded reasoning parser.

_register_module(module, module_name=None, force=True) classmethod

Register a ReasoningParser class immediately.

get_reasoning_parser(name) classmethod

Retrieve a registered or lazily registered ReasoningParser class.

If the parser is lazily registered, it will be imported and cached on first access.

Raises:

  • KeyError –

    if no parser is found under the given name.

import_reasoning_parser(plugin_path) classmethod

Import a user-defined reasoning parser.

list_registered() classmethod

Return names of all eagerly and lazily registered reasoning parsers.

register_lazy_module(name, module_path, class_name) classmethod

Register a lazy module mapping for delayed import.

Example

ReasoningParserManager.register_lazy_module( name="qwen3", module_path="vllm.reasoning.qwen3_engine_reasoning_parser", class_name="Qwen3ParserReasoningAdapter", )

register_module(name=None, force=True, module=None) classmethod

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