Add pydocstyle to the ruff rules (#52136)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Signed-off-by: Harry Mellor <hej.mellor@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Harry Mellor
2026-09-16 16:44:29 +00:00
committed by GitHub
co-authored by Claude Opus 5
parent 804e5377aa
commit 8c1557a79c
2002 changed files with 15024 additions and 14269 deletions
+33 -31
View File
@@ -23,8 +23,7 @@ logger = init_logger(__name__)
class ReasoningParser:
"""
Abstract reasoning parser class that should not be used directly.
"""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.
@@ -71,26 +70,27 @@ class ReasoningParser:
@abstractmethod
def is_reasoning_end(self, input_ids: Sequence[int]) -> bool:
"""
Check if the reasoning content ends in the input_ids.
"""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:
Parameters
----------
input_ids: list[int]
The input_ids of the model output.
Returns:
Returns
-------
bool
True if the reasoning content ends in the input_ids.
"""
def is_reasoning_end_streaming(
self, input_ids: Sequence[int], delta_ids: Iterable[int]
) -> bool:
"""
Check if the reasoning content ends in the input_ids on a
"""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
@@ -98,29 +98,36 @@ class ReasoningParser:
`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:
Parameters
----------
input_ids: list[int]
The entire model output.
delta_ids: list[int]
The last few computed tokens of the model output at the current decode step.
Returns:
Returns
-------
bool
True if the reasoning content ends in the `delta_ids` on a
decode step.
"""
return self.is_reasoning_end(input_ids)
@abstractmethod
def extract_content_ids(self, input_ids: list[int]) -> list[int]:
"""
Extract content token ids from the input_ids.
Parameters:
"""Extract content token ids from the input_ids.
Parameters
----------
input_ids: list[int]
The input_ids of the model output.
Returns:
Returns
-------
list[int]
The extracted content from the input_ids.
"""
def count_reasoning_tokens(self, token_ids: Sequence[int]) -> int:
@@ -137,8 +144,8 @@ class ReasoningParser:
Returns:
int: Number of tokens that belong to reasoning content.
"""
"""
# By default, assume the parser cannot detect reasoning spans.
return 0
@@ -148,18 +155,18 @@ class ReasoningParser:
model_output: str,
request: "ChatCompletionRequest | ResponsesRequest",
) -> tuple[str | None, str | None]:
"""
Extract reasoning content from a complete model-generated string.
"""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:
Args:
model_output: The model-generated string to extract reasoning content from.
request: The request object that was used to generate the model_output.
Returns:
A tuple containing the reasoning content and the content.
"""
@abstractmethod
@@ -172,8 +179,7 @@ class ReasoningParser:
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> "DeltaMessage | None":
"""
Instance method that should be implemented for extracting reasoning
"""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
@@ -203,15 +209,12 @@ class ReasoningParser:
original_tag: str | None,
tool_server: ToolServer | None,
) -> str | None:
"""
Instance method that is implemented for preparing the structured tag
"""
"""Instance method that is implemented for preparing the structured tag"""
return original_tag
class ReasoningParserManager:
"""
Central registry for ReasoningParser implementations.
"""Central registry for ReasoningParser implementations.
Supports two registration modes:
- Eager registration via `register_module`
@@ -225,14 +228,14 @@ class ReasoningParserManager:
@classmethod
def get_reasoning_parser(cls, name: str) -> type[ReasoningParser]:
"""
Retrieve a registered or lazily registered ReasoningParser class.
"""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.
"""
if name in cls.reasoning_parsers:
return cls.reasoning_parsers[name]
@@ -303,8 +306,7 @@ class ReasoningParserManager:
@classmethod
def register_lazy_module(cls, name: str, module_path: str, class_name: str) -> None:
"""
Register a lazy module mapping for delayed import.
"""Register a lazy module mapping for delayed import.
Example:
ReasoningParserManager.register_lazy_module(
@@ -312,6 +314,7 @@ class ReasoningParserManager:
module_path="vllm.reasoning.qwen3_engine_reasoning_parser",
class_name="Qwen3ParserReasoningAdapter",
)
"""
cls.lazy_parsers[name] = (module_path, class_name)
@@ -324,8 +327,7 @@ class ReasoningParserManager:
) -> (
type[ReasoningParser] | Callable[[type[ReasoningParser]], type[ReasoningParser]]
):
"""
Register module with the given name or name list. it can be used as a
"""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).
"""
+4 -9
View File
@@ -16,8 +16,7 @@ if TYPE_CHECKING:
class BaseThinkingReasoningParser(ReasoningParser):
"""
Base class for reasoning parsers that use thinking tokens.
"""Base class for reasoning parsers that use thinking tokens.
This class provides common functionality for parsers that use start and end
tokens to delimit reasoning content (
@@ -87,9 +86,7 @@ class BaseThinkingReasoningParser(ReasoningParser):
return end_token_id in delta_ids
def extract_content_ids(self, input_ids: list[int]) -> list[int]:
"""
Extract the content after the end tokens
"""
"""Extract the content after the end tokens."""
if self.end_token_id not in islice(input_ids, 0, max(0, len(input_ids) - 1)):
return []
else:
@@ -104,8 +101,7 @@ class BaseThinkingReasoningParser(ReasoningParser):
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> DeltaMessage | None:
"""
Extract reasoning content from a delta message.
"""Extract reasoning content from a delta message.
Handles streaming output where previous + delta = current.
Uses token IDs for faster processing.
"""
@@ -157,8 +153,7 @@ class BaseThinkingReasoningParser(ReasoningParser):
def extract_reasoning(
self, model_output: str, request: "ChatCompletionRequest | ResponsesRequest"
) -> tuple[str | None, str | None]:
"""
Extract reasoning content from the model output.
"""Extract reasoning content from the model output.
This is the base implementation that works for most models.
Subclasses can override this method for specific behavior.
@@ -8,8 +8,7 @@ from vllm.reasoning.basic_parsers import BaseThinkingReasoningParser
class DeepSeekR1ReasoningParser(BaseThinkingReasoningParser):
"""
Reasoning parser for DeepSeek R1 model.
"""Reasoning parser for DeepSeek R1 model.
The DeepSeek R1 model uses <think>...</think> tokens to denote reasoning
text. This parser extracts the reasoning content from the model output.
@@ -18,8 +18,7 @@ if TYPE_CHECKING:
class DeepSeekV3ReasoningParser(ReasoningParser):
"""
V3 parser that delegates to either DeepSeekR1ReasoningParser or
"""V3 parser that delegates to either DeepSeekR1ReasoningParser or
IdentityReasoningParser based on `thinking` and `separate_reasoning`.
"""
@@ -81,9 +80,7 @@ class DeepSeekV3ReasoningParser(ReasoningParser):
class DeepSeekV3ReasoningWithThinkingParser(DeepSeekV3ReasoningParser):
"""
DeepSeekV3ReasoningParser that defaults to thinking mode.
"""
"""DeepSeekV3ReasoningParser that defaults to thinking mode."""
def __init__(self, tokenizer: PreTrainedTokenizerBase, *args, **kwargs):
chat_kwargs = kwargs.get("chat_template_kwargs", {}) or {}
+3 -6
View File
@@ -15,8 +15,7 @@ if TYPE_CHECKING:
class Ernie45ReasoningParser(BaseThinkingReasoningParser):
"""
Reasoning parser for Ernie45 thinking model.
"""Reasoning parser for Ernie45 thinking model.
The Ernie45 thinking model output format is
abc\n</think>\n\n<response>\ndef\n</response>\n
or abc\n</think>\ndef
@@ -60,8 +59,7 @@ class Ernie45ReasoningParser(BaseThinkingReasoningParser):
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> DeltaMessage | None:
"""
Extract reasoning content from a delta message.
"""Extract reasoning content from a delta message.
Handles streaming output where previous + delta = current.
Uses token IDs for faster processing.
The Ernie45 thinking model output format is
@@ -138,8 +136,7 @@ class Ernie45ReasoningParser(BaseThinkingReasoningParser):
def extract_reasoning(
self, model_output: str, request: "ChatCompletionRequest | ResponsesRequest"
) -> tuple[str | None, str | None]:
"""
Extract reasoning content from the model output.
"""Extract reasoning content from the model output.
The Ernie45 thinking model output format is
abc\n</think>\n\n\n<response>\ndef\n</response>\n
or abc\n</think>\ndef
+1
View File
@@ -75,6 +75,7 @@ def parse_thinking_output(text: str) -> dict[str, str | None]:
>>> result = parse_thinking_output(output_text)
>>> print(result["thinking"]) # chain-of-thought reasoning or None
>>> print(result["answer"]) # final answer
"""
if _THINKING_END_TAG in text:
parts = text.split(_THINKING_END_TAG, 1)
+1 -2
View File
@@ -14,8 +14,7 @@ if TYPE_CHECKING:
class GptOssReasoningParser(ReasoningParser):
"""
Reasoning parser for GptOss model.
"""Reasoning parser for GptOss model.
The GptOss model uses harmony to extract reasoning content and this parser
is only used for detecting the end of the reasoning content.
+9 -2
View File
@@ -16,8 +16,7 @@ if TYPE_CHECKING:
class GraniteReasoningParser(ReasoningParser):
"""
Reasoning parser for IBM Granite.
"""Reasoning parser for IBM Granite.
IBM granite models currently use "Here is my thought process:"
and "Here is my response:" to separate its thinking / response outputs.
@@ -65,6 +64,7 @@ class GraniteReasoningParser(ReasoningParser):
Returns:
tuple[Optional[str], Optional[str]]: Tuple pair containing the
reasoning content and non-reasoning content.
"""
re_match = self.reasoning_regex.findall(model_output)
if not re_match:
@@ -108,6 +108,7 @@ class GraniteReasoningParser(ReasoningParser):
Returns:
Union[DeltaMessage, None]
DeltaMessage with either reasoning content or content, or None.
"""
reasoning, resp_seq_len, content = self._get_content_sections(current_text)
# Either we haven't finished the start of the reasoning sequence,
@@ -142,6 +143,7 @@ class GraniteReasoningParser(ReasoningParser):
Returns:
bool: True if any of the possible reasoning start seqs match.
"""
return any(
think_start.startswith(text) for think_start in self.valid_think_starts
@@ -155,6 +157,7 @@ class GraniteReasoningParser(ReasoningParser):
Returns:
bool: True if any of the possible response start seqs match.
"""
return any(
response_start.startswith(text)
@@ -175,6 +178,7 @@ class GraniteReasoningParser(ReasoningParser):
Returns:
DeltaMessage: Message containing the parsed content.
"""
prev_longest_length = len(current_text) - len(delta_text)
is_substr = self._is_reasoning_start_substr(current_text)
@@ -213,6 +217,7 @@ class GraniteReasoningParser(ReasoningParser):
Returns:
DeltaMessage: Message containing the parsed content.
"""
# If we have no reasoning content or explicitly end with the start of
# response sequence, we are in transition to the response; need to be
@@ -293,6 +298,7 @@ class GraniteReasoningParser(ReasoningParser):
Returns:
DeltaMessage: Message containing the parsed content.
"""
# Always have content; take length to the end
delta_content = delta_text[-len(response_content) :]
@@ -329,6 +335,7 @@ class GraniteReasoningParser(ReasoningParser):
tuple[Optional[str], Optional[int], Optional[str]]: Tuple of len 3
containing the reasoning content, the length of the response seq
(if there is one) and the non-reasoning content.
"""
current_chunk_start = 0
start_reasoning = None
@@ -16,8 +16,7 @@ if TYPE_CHECKING:
class HunyuanA13BReasoningParser(ReasoningParser):
"""
Reasoning parser for Hunyuan A13B Model
"""Reasoning parser for Hunyuan A13B Model.
HunyuanReasoningParser
@@ -102,8 +101,8 @@ class HunyuanA13BReasoningParser(ReasoningParser):
Returns:
tuple[Optional[str], Optional[str]]: Tuple pair containing the
reasoning content and non-reasoning content.
"""
"""
re_match = self.full_match_reasoning_regex.findall(model_output)
if re_match:
reasoning, response_content = re_match[0]
@@ -139,7 +138,7 @@ class HunyuanA13BReasoningParser(ReasoningParser):
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> DeltaMessage | None:
"""Extract content using token ID sequence state machine"""
"""Extract content using token ID sequence state machine."""
# Define sequences
think_start_sequence = self.think_start_ids
response_start_sequence = self.response_start_ids
+1 -2
View File
@@ -18,8 +18,7 @@ logger = init_logger(__name__)
class HYV3ReasoningParser(BaseThinkingReasoningParser):
"""
HYV3 parser that delegates to either HYV3ReasoningParser or
"""HYV3 parser that delegates to either HYV3ReasoningParser or
IdentityReasoningParser based on `reasoning_effort`.
The HYV3 model uses <think>...</think> tokens to denote reasoning text.
+1 -1
View File
@@ -46,8 +46,8 @@ def detect_token_suffix(tokenizer: TokenizerLike) -> str:
RuntimeError: The tokenizer declares the structural tokens through
``model_specific_special_tokens``, which transformers 5 no longer
round-trips.
"""
"""
import transformers
if int(transformers.__version__.split(".")[0]) >= 5:
+1 -2
View File
@@ -15,8 +15,7 @@ if TYPE_CHECKING:
class IdentityReasoningParser(ReasoningParser):
"""
Identity reasoning parser.
"""Identity reasoning parser.
This parser does not attempt to parse or strip out reasoning tokens.
It treats the entire model output as content and ignores reasoning.
@@ -15,8 +15,7 @@ if TYPE_CHECKING:
class MiniMaxM2ReasoningParser(MinimaxM2ParserReasoningAdapter): # type: ignore[valid-type, misc]
"""
Reasoning parser for MiniMax M2 model.
"""Reasoning parser for MiniMax M2 model.
MiniMax M2 models don't generate <think> start token, only </think> end
token. All content before </think> is reasoning, content after is the
@@ -25,9 +24,7 @@ class MiniMaxM2ReasoningParser(MinimaxM2ParserReasoningAdapter): # type: ignore
class MiniMaxM2AppendThinkReasoningParser(ReasoningParser):
"""
Reasoning parser for MiniMax M2 model.
"""
"""Reasoning parser for MiniMax M2 model."""
def __init__(self, tokenizer: TokenizerLike, *args, **kwargs):
super().__init__(tokenizer, *args, **kwargs)
+5 -8
View File
@@ -32,8 +32,7 @@ class Indices:
def string_overlap(a: str, b: str) -> tuple[Indices | None, Indices | None]:
"""
Find the longest overlap where the end of string a matches the start
"""Find the longest overlap where the end of string a matches the start
of string b.
Args:
@@ -43,8 +42,8 @@ def string_overlap(a: str, b: str) -> tuple[Indices | None, Indices | None]:
Returns:
Tuple of IndicesTuples representing the overlapping portions in each
string, or a tuple of None if no overlap exists
"""
"""
# swap so a is always the shorter string
a, b, swap = (a, b, False) if len(a) < len(b) else (b, a, True)
@@ -188,8 +187,7 @@ class Olmo3ReasoningBuffer:
class Olmo3ReasoningParser(ReasoningParser):
"""
Reasoning parser for Olmo 3 model
"""Reasoning parser for Olmo 3 model.
Olmo3ReasoningParser
@@ -285,8 +283,8 @@ class Olmo3ReasoningParser(ReasoningParser):
Returns:
tuple[Optional[str], Optional[str]]: Tuple pair containing the
reasoning content and non-reasoning content.
"""
"""
re_match = self.reasoning_regex.match(model_output)
if re_match:
reasoning = re_match.group("reasoning") or None
@@ -305,8 +303,7 @@ class Olmo3ReasoningParser(ReasoningParser):
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> DeltaMessage | None:
"""Extract content using token ID sequence state machine"""
"""Extract content using token ID sequence state machine."""
delta_message = self.buffer.add_text(delta_text)
if delta_message is None and self.buffer.think_end in self.buffer.buffer:
# this is a bit hacky, but, because of how the buffer is
@@ -1,7 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""
Laguna reasoning parser.
"""Laguna reasoning parser.
``DeepSeekV3ReasoningParser.is_reasoning_end`` walks the entire
token sequence backwards and returns ``True`` on the first ``</think>`` it
+2 -4
View File
@@ -17,8 +17,7 @@ if TYPE_CHECKING:
class Step3ReasoningParser(ReasoningParser):
"""
Reasoning parser for Step3 model.
"""Reasoning parser for Step3 model.
The Step3 model uses </think> token to denote the end of reasoning
text. This parser extracts all content before </think> as reasoning content.
@@ -62,8 +61,7 @@ class Step3ReasoningParser(ReasoningParser):
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> DeltaMessage | None:
"""
Extract reasoning content from a delta message.
"""Extract reasoning content from a delta message.
Handles streaming output where previous + delta = current.
Uses token IDs for faster processing.
For text "abc</think>xyz":
+1 -2
View File
@@ -14,8 +14,7 @@ if TYPE_CHECKING:
class Step3p5ReasoningParser(BaseThinkingReasoningParser):
"""
Reasoning parser for Step3p5 model.
"""Reasoning parser for Step3p5 model.
Step3p5 uses the <think>...</think> format, but it tends to emit an extra
newline immediately before and/or after the </think> token. This parser