Update Optional[x] -> x | None and Union[x, y] to x | y (#26633)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
Harry Mellor
2025-10-12 09:51:31 -07:00
committed by GitHub
parent 9bb38130cb
commit 8fcaaf6a16
944 changed files with 9490 additions and 10121 deletions
+8 -10
View File
@@ -1,13 +1,11 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from __future__ import annotations
import os
from abc import abstractmethod
from collections.abc import Sequence
from collections.abc import Callable, Sequence
from functools import cached_property
from typing import TYPE_CHECKING, Any, Callable, Union
from typing import TYPE_CHECKING, Any
from vllm.logger import init_logger
from vllm.utils import import_from_path, is_list_of
@@ -78,7 +76,7 @@ class ReasoningParser:
def extract_reasoning_content(
self,
model_output: str,
request: Union[ChatCompletionRequest, ResponsesRequest],
request: ChatCompletionRequest | ResponsesRequest,
) -> tuple[str | None, str | None]:
"""
Extract reasoning content from a complete model-generated string.
@@ -107,7 +105,7 @@ class ReasoningParser:
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> Union[DeltaMessage, None]:
) -> DeltaMessage | None:
"""
Instance method that should be implemented for extracting reasoning
from an incomplete response; for use when handling reasoning calls and
@@ -136,7 +134,7 @@ class ReasoningParserManager:
def _register_module(
cls,
module: type,
module_name: Union[str, list[str]] | None = None,
module_name: str | list[str] | None = None,
force: bool = True,
) -> None:
if not issubclass(module, ReasoningParser):
@@ -158,10 +156,10 @@ class ReasoningParserManager:
@classmethod
def register_module(
cls,
name: Union[str, list[str]] | None = None,
name: str | list[str] | None = None,
force: bool = True,
module: Union[type, None] = None,
) -> Union[type, Callable]:
module: type | None = None,
) -> type | Callable:
"""
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
+3 -4
View File
@@ -3,7 +3,6 @@
from abc import abstractmethod
from collections.abc import Sequence
from typing import Optional, Union
from vllm.entrypoints.openai.protocol import (
ChatCompletionRequest,
@@ -79,7 +78,7 @@ class BaseThinkingReasoningParser(ReasoningParser):
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> Union[DeltaMessage, None]:
) -> DeltaMessage | None:
"""
Extract reasoning content from a delta message.
Handles streaming output where previous + delta = current.
@@ -135,8 +134,8 @@ class BaseThinkingReasoningParser(ReasoningParser):
return DeltaMessage(content=delta_text)
def extract_reasoning_content(
self, model_output: str, request: Union[ChatCompletionRequest, ResponsesRequest]
) -> tuple[Optional[str], Optional[str]]:
self, model_output: str, request: ChatCompletionRequest | ResponsesRequest
) -> tuple[str | None, str | None]:
"""
Extract reasoning content from the model output.
@@ -2,7 +2,6 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from collections.abc import Sequence
from typing import Union
from vllm.entrypoints.openai.protocol import DeltaMessage
from vllm.reasoning.abs_reasoning_parsers import ReasoningParserManager
@@ -36,7 +35,7 @@ class DeepSeekR1ReasoningParser(BaseThinkingReasoningParser):
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> Union[DeltaMessage, None]:
) -> DeltaMessage | None:
ret = super().extract_reasoning_content_streaming(
previous_text,
current_text,
+2 -3
View File
@@ -2,7 +2,6 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from collections.abc import Sequence
from typing import Optional, Union
from transformers import PreTrainedTokenizerBase
@@ -80,7 +79,7 @@ class Glm4MoeModelReasoningParser(ReasoningParser):
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> Union[DeltaMessage, None]:
) -> DeltaMessage | None:
"""
Extract reasoning content from a delta message.
Handles streaming output where previous + delta = current.
@@ -137,7 +136,7 @@ class Glm4MoeModelReasoningParser(ReasoningParser):
def extract_reasoning_content(
self, model_output: str, request: ChatCompletionRequest
) -> tuple[Optional[str], Optional[str]]:
) -> tuple[str | None, str | None]:
"""
Extract reasoning content from the model output.
+2 -3
View File
@@ -2,7 +2,6 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from collections.abc import Sequence
from typing import Optional, Union
from transformers import PreTrainedTokenizerBase
@@ -53,7 +52,7 @@ class GptOssReasoningParser(ReasoningParser):
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> Union[DeltaMessage, None]:
) -> DeltaMessage | None:
prev_reasoning, prev_content, _ = parse_chat_output(list(previous_token_ids))
cur_reasoning, cur_content, _ = parse_chat_output(list(current_token_ids))
reasoning_delta = None
@@ -78,7 +77,7 @@ class GptOssReasoningParser(ReasoningParser):
self,
model_output: str,
request: ChatCompletionRequest,
) -> tuple[Optional[str], Optional[str]]:
) -> tuple[str | None, str | None]:
raise NotImplementedError(
"gpt-oss has a special branch for parsing reasoning in non-streaming mode. This method shouldn't be used." # noqa: E501
)
+3 -4
View File
@@ -2,7 +2,6 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from collections.abc import Sequence
from typing import Optional, Union
import regex as re
from transformers import PreTrainedTokenizerBase
@@ -53,7 +52,7 @@ class GraniteReasoningParser(ReasoningParser):
def extract_reasoning_content(
self, model_output: str, request: ChatCompletionRequest
) -> tuple[Optional[str], Optional[str]]:
) -> tuple[str | None, str | None]:
"""Extract the reasoning content & content sections, respectively.
If the sequence doesn't match what we expect, i.e., the model generates
something else, all content is considered non-reasoning content.
@@ -82,7 +81,7 @@ class GraniteReasoningParser(ReasoningParser):
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> Union[DeltaMessage, None]:
) -> DeltaMessage | None:
"""Extract the reasoning content / content emitted by granite models;
If the sequence doesn't match what we expect, i.e., the model generates
something else, all content is considered non-reasoning content.
@@ -322,7 +321,7 @@ class GraniteReasoningParser(ReasoningParser):
def _get_content_sections(
self, current_text: str
) -> tuple[Optional[str], Optional[int], Optional[str]]:
) -> tuple[str | None, int | None, str | None]:
"""Parse the text to extract the reasoning content / content
if we have them.
@@ -2,7 +2,6 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from collections.abc import Sequence
from typing import Optional, Union
import regex as re
from transformers import PreTrainedTokenizerBase
@@ -90,7 +89,7 @@ class HunyuanA13BReasoningParser(ReasoningParser):
def extract_reasoning_content(
self, model_output: str, request: ChatCompletionRequest
) -> tuple[Optional[str], Optional[str]]:
) -> tuple[str | None, str | None]:
"""Extract the reasoning content & content sections, respectively.
If the sequence doesn't match what we expect, i.e., the model generates
something else, all content is considered non-reasoning content.
@@ -150,7 +149,7 @@ class HunyuanA13BReasoningParser(ReasoningParser):
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> Union[DeltaMessage, None]:
) -> DeltaMessage | None:
"""Extract content using token ID sequence state machine"""
# Define sequences
think_start_sequence = self.think_start_ids
+8 -8
View File
@@ -4,7 +4,7 @@
import dataclasses as dt
import enum
from collections.abc import Sequence
from typing import TYPE_CHECKING, Optional, Union
from typing import TYPE_CHECKING
import regex as re
@@ -36,7 +36,7 @@ class Indices:
return self.end - self.start
def string_overlap(a: str, b: str) -> tuple[Optional[Indices], Optional[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
of string b.
@@ -90,7 +90,7 @@ class Olmo3ReasoningBuffer:
# is when we switch to content state.
state: Olmo3ReasoningState = Olmo3ReasoningState.REASONING
def process_buffer(self) -> Optional[DeltaMessage]:
def process_buffer(self) -> DeltaMessage | None:
start_think_idx = self.buffer.find(self.think_start)
if start_think_idx >= 0:
@@ -142,12 +142,12 @@ class Olmo3ReasoningBuffer:
# is the length of the text buffer
return len(self.buffer)
def add_text(self, delta_text: str) -> Optional[DeltaMessage]:
def add_text(self, delta_text: str) -> DeltaMessage | None:
# we start by adding the delta text to the buffer
self.buffer += delta_text
# setting this to empty before starting
delta_message: Optional[DeltaMessage] = None
delta_message: DeltaMessage | None = None
# we start by computing the overlap between the delta_text
# and start/end of think tokens.
@@ -254,8 +254,8 @@ class Olmo3ReasoningParser(ReasoningParser):
def extract_reasoning_content(
self,
model_output: str,
request: Union[ChatCompletionRequest, ResponsesRequest],
) -> tuple[Optional[str], Optional[str]]:
request: ChatCompletionRequest | ResponsesRequest,
) -> tuple[str | None, str | None]:
"""Extract the reasoning content & content sections, respectively.
If the sequence doesn't match what we expect, i.e., the model generates
something else, all content is considered non-reasoning content.
@@ -287,7 +287,7 @@ class Olmo3ReasoningParser(ReasoningParser):
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> Union[DeltaMessage, None]:
) -> DeltaMessage | None:
"""Extract content using token ID sequence state machine"""
delta_message = self.buffer.add_text(delta_text)
+2 -3
View File
@@ -1,7 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from typing import Optional, Union
from vllm.entrypoints.openai.protocol import ChatCompletionRequest, ResponsesRequest
from vllm.reasoning.abs_reasoning_parsers import ReasoningParserManager
@@ -31,8 +30,8 @@ class Qwen3ReasoningParser(BaseThinkingReasoningParser):
return "</think>"
def extract_reasoning_content(
self, model_output: str, request: Union[ChatCompletionRequest, ResponsesRequest]
) -> tuple[Optional[str], Optional[str]]:
self, model_output: str, request: ChatCompletionRequest | ResponsesRequest
) -> tuple[str | None, str | None]:
"""
Extract reasoning content from the model output.
+2 -3
View File
@@ -2,7 +2,6 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from collections.abc import Sequence
from typing import Optional, Union
import regex as re
from transformers import PreTrainedTokenizerBase
@@ -50,7 +49,7 @@ class Step3ReasoningParser(ReasoningParser):
previous_token_ids: Sequence[int],
current_token_ids: Sequence[int],
delta_token_ids: Sequence[int],
) -> Union[DeltaMessage, None]:
) -> DeltaMessage | None:
"""
Extract reasoning content from a delta message.
Handles streaming output where previous + delta = current.
@@ -81,7 +80,7 @@ class Step3ReasoningParser(ReasoningParser):
def extract_reasoning_content(
self, model_output: str, request: ChatCompletionRequest
) -> tuple[Optional[str], Optional[str]]:
) -> tuple[str | None, str | None]:
# Check if the model output contains the </think> token
if self.think_end_token not in model_output:
# If no </think> token, everything is reasoning content