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:
co-authored by
Claude Opus 5
parent
804e5377aa
commit
8c1557a79c
@@ -50,6 +50,7 @@ class OpenAIServingChatBatch(OpenAIServingChat):
|
||||
Returns:
|
||||
A tuple of (all_conversations, engine_prompts) on success — one
|
||||
entry per conversation — or an ErrorResponse on failure.
|
||||
|
||||
"""
|
||||
error_check_ret = await self._check_model(request)
|
||||
if error_check_ret is not None:
|
||||
|
||||
@@ -220,8 +220,7 @@ class OpenAIServingChat(GenerateBaseServing):
|
||||
self,
|
||||
request: ChatCompletionRequest,
|
||||
) -> tuple[list[ConversationMessage], list[EngineInput]] | ErrorResponse:
|
||||
"""
|
||||
Validate the model and preprocess a chat completion request.
|
||||
"""Validate the model and preprocess a chat completion request.
|
||||
|
||||
Delegates preprocessing logic to OnlineRenderer, adding the
|
||||
engine-aware checks (LoRA model validation, engine health).
|
||||
@@ -229,6 +228,7 @@ class OpenAIServingChat(GenerateBaseServing):
|
||||
Returns:
|
||||
A tuple of (conversation, engine_inputs) on success,
|
||||
or an ErrorResponse on failure.
|
||||
|
||||
"""
|
||||
error_check_ret = await self._check_model(request)
|
||||
if error_check_ret is not None:
|
||||
@@ -244,8 +244,7 @@ class OpenAIServingChat(GenerateBaseServing):
|
||||
request: ChatCompletionRequest,
|
||||
raw_request: Request | None = None,
|
||||
) -> AsyncGenerator[str, None] | ChatCompletionResponse | ErrorResponse:
|
||||
"""
|
||||
Chat Completion API similar to OpenAI's API.
|
||||
"""Chat Completion API similar to OpenAI's API.
|
||||
|
||||
See https://platform.openai.com/docs/api-reference/chat/create
|
||||
for the API specification. This API mimics the OpenAI
|
||||
|
||||
@@ -89,14 +89,14 @@ class OpenAIServingCompletion(GenerateBaseServing):
|
||||
self,
|
||||
request: CompletionRequest,
|
||||
) -> list[EngineInput] | ErrorResponse:
|
||||
"""
|
||||
Validate the model and preprocess a completion request.
|
||||
"""Validate the model and preprocess a completion request.
|
||||
|
||||
Delegates preprocessing logic to OnlineRenderer, adding the
|
||||
engine-aware checks (LoRA model validation, engine health).
|
||||
|
||||
Returns:
|
||||
A list of engine_inputs on success, or an ErrorResponse on failure.
|
||||
|
||||
"""
|
||||
error_check_ret = await self._check_model(request)
|
||||
if error_check_ret is not None:
|
||||
|
||||
@@ -123,7 +123,8 @@ class OpenAIServingModels:
|
||||
|
||||
async def init_static_loras(self):
|
||||
"""Loads all static LoRA modules.
|
||||
Raises if any fail to load"""
|
||||
Raises if any fail to load
|
||||
"""
|
||||
if self.static_lora_modules is None:
|
||||
return
|
||||
for lora in self.static_lora_modules:
|
||||
@@ -148,7 +149,8 @@ class OpenAIServingModels:
|
||||
|
||||
async def show_available_models(self) -> ModelList:
|
||||
"""Show available models. This includes the base model and all
|
||||
adapters."""
|
||||
adapters.
|
||||
"""
|
||||
model_list = await self.registry.show_available_models()
|
||||
lora_cards = [
|
||||
ModelCard(
|
||||
@@ -289,6 +291,7 @@ class OpenAIServingModels:
|
||||
LoRARequest if found and loaded successfully.
|
||||
ErrorResponse (404) if no resolver finds the adapter.
|
||||
ErrorResponse (400) if adapter(s) are found but none load.
|
||||
|
||||
"""
|
||||
async with self.lora_resolver_lock[lora_name]:
|
||||
# First check if this LoRA is already loaded
|
||||
|
||||
@@ -88,8 +88,7 @@ MCP_BUILTIN_TOOLS: set[str] = set(BUILTIN_TOOL_TO_MCP_SERVER_LABEL.values())
|
||||
|
||||
|
||||
def has_custom_tools(tool_types: set[str]) -> bool:
|
||||
"""
|
||||
Checks if the given tool types are custom tools
|
||||
"""Checks if the given tool types are custom tools
|
||||
(i.e. any tool other than MCP builtin tools)
|
||||
"""
|
||||
return not tool_types.issubset(MCP_BUILTIN_TOOLS)
|
||||
@@ -209,8 +208,7 @@ def get_system_or_developer_message(role: str, instructions: str) -> Message:
|
||||
|
||||
|
||||
def parse_chat_inputs_to_harmony_messages(chat_msgs: list) -> list[Message]:
|
||||
"""
|
||||
Parse a list of messages from request.messages in the Chat Completion API to
|
||||
"""Parse a list of messages from request.messages in the Chat Completion API to
|
||||
Harmony messages.
|
||||
"""
|
||||
msgs: list[Message] = []
|
||||
@@ -230,9 +228,8 @@ def parse_chat_inputs_to_harmony_messages(chat_msgs: list) -> list[Message]:
|
||||
|
||||
|
||||
def auto_drop_analysis_messages(msgs: list[Message]) -> list[Message]:
|
||||
"""
|
||||
Harmony models expect the analysis messages (representing raw chain of thought) to
|
||||
be dropped after an assistant message to the final channel is produced from the
|
||||
"""Harmony models expect the analysis messages (representing raw chain of thought)
|
||||
to be dropped after an assistant message to the final channel is produced from the
|
||||
reasoning of those messages.
|
||||
|
||||
The openai-harmony library does this if the very last assistant message is to the
|
||||
@@ -262,8 +259,7 @@ def auto_drop_analysis_messages(msgs: list[Message]) -> list[Message]:
|
||||
|
||||
|
||||
def flatten_input_text_content(content: Any) -> str | None:
|
||||
"""
|
||||
Extract text parts from a Chat Completion or Responses API content field and
|
||||
"""Extract text parts from a Chat Completion or Responses API content field and
|
||||
flatten them into a single string. Returns None if no text content is found.
|
||||
"""
|
||||
if content is None or isinstance(content, str):
|
||||
@@ -286,8 +282,7 @@ def flatten_input_text_content(content: Any) -> str | None:
|
||||
def extract_instructions_from_messages(
|
||||
messages: Sequence[Any],
|
||||
) -> tuple[str | None, list[Any]]:
|
||||
"""
|
||||
Peel a leading system/developer Chat Completion or Responses message and
|
||||
"""Peel a leading system/developer Chat Completion or Responses message and
|
||||
flatten its instruction text.
|
||||
"""
|
||||
remaining_messages = list(messages)
|
||||
@@ -327,9 +322,7 @@ def build_harmony_preamble(
|
||||
container_description: str | None = None,
|
||||
with_custom_tools: bool = False,
|
||||
) -> list[Message]:
|
||||
"""
|
||||
Build the standard Harmony system/developer prefix for a request.
|
||||
"""
|
||||
"""Build the standard Harmony system/developer prefix for a request."""
|
||||
developer_instructions = system_instructions = None
|
||||
if envs.VLLM_GPT_OSS_HARMONY_SYSTEM_INSTRUCTIONS:
|
||||
system_instructions = instructions
|
||||
@@ -359,8 +352,7 @@ def build_harmony_preamble(
|
||||
def parse_chat_input_to_harmony_message(
|
||||
chat_msg, tool_id_names: dict[str, str] | None = None
|
||||
) -> list[Message]:
|
||||
"""
|
||||
Parse a message from request.messages in the Chat Completion API to
|
||||
"""Parse a message from request.messages in the Chat Completion API to
|
||||
Harmony messages.
|
||||
"""
|
||||
tool_id_names = tool_id_names or {}
|
||||
|
||||
@@ -34,7 +34,7 @@ def responses(request: Request) -> OpenAIServingResponses | None:
|
||||
async def _convert_stream_to_sse_events(
|
||||
generator: AsyncGenerator[StreamingResponsesResponse, None],
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""Convert the generator to a stream of events in SSE format"""
|
||||
"""Convert the generator to a stream of events in SSE format."""
|
||||
async for event in generator:
|
||||
event_type = getattr(event, "type", "unknown")
|
||||
# https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format
|
||||
|
||||
@@ -141,9 +141,7 @@ class ConversationContext(ABC):
|
||||
def _create_json_parse_error_messages(
|
||||
last_msg: Message, e: json.JSONDecodeError
|
||||
) -> list[Message]:
|
||||
"""
|
||||
Creates an error message when json parse failed.
|
||||
"""
|
||||
"""Creates an error message when json parse failed."""
|
||||
error_msg = (
|
||||
f"Error parsing tool arguments as JSON: {str(e)}. "
|
||||
"Please ensure the tool call arguments are valid JSON and try again."
|
||||
@@ -161,7 +159,7 @@ def _create_json_parse_error_messages(
|
||||
|
||||
|
||||
class SimpleContext(ConversationContext):
|
||||
"""This is a context that cannot handle MCP tool calls"""
|
||||
"""This is a context that cannot handle MCP tool calls."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -410,7 +408,8 @@ class ParsableContext(ConversationContext):
|
||||
|
||||
def need_builtin_tool_call(self) -> bool:
|
||||
"""Return true if the last message is a builtin tool call
|
||||
that the request has enabled."""
|
||||
that the request has enabled.
|
||||
"""
|
||||
last_message = self.response_messages[-1]
|
||||
if last_message.type != "function_call":
|
||||
return False
|
||||
@@ -474,8 +473,7 @@ class ParsableContext(ConversationContext):
|
||||
async def call_container_tool(
|
||||
self, tool_session: Union["ClientSession", Tool], last_msg: Message
|
||||
) -> list[Message]:
|
||||
"""
|
||||
Call container tool. Expect this to be run in a stateful docker
|
||||
"""Call container tool. Expect this to be run in a stateful docker
|
||||
with command line terminal.
|
||||
The official container tool would at least
|
||||
expect the following format:
|
||||
@@ -583,7 +581,7 @@ class ParsableContext(ConversationContext):
|
||||
exit_stack.push_async_exit(self.cleanup_session)
|
||||
|
||||
async def cleanup_session(self, *args, **kwargs) -> None:
|
||||
"""Can be used as coro to used in __aexit__"""
|
||||
"""Can be used as coro to used in __aexit__."""
|
||||
|
||||
async def cleanup_tool_session(tool_session):
|
||||
if not isinstance(tool_session, Tool):
|
||||
@@ -693,6 +691,7 @@ class HarmonyContext(ConversationContext):
|
||||
|
||||
Args:
|
||||
output: The RequestOutput containing prompt token information
|
||||
|
||||
"""
|
||||
if output.prompt_token_ids is not None:
|
||||
this_turn_input_tokens = len(output.prompt_token_ids)
|
||||
@@ -757,6 +756,7 @@ class HarmonyContext(ConversationContext):
|
||||
|
||||
Returns:
|
||||
int: Number of output tokens processed in this call
|
||||
|
||||
"""
|
||||
updated_output_token_count = 0
|
||||
if output.outputs:
|
||||
@@ -884,8 +884,7 @@ class HarmonyContext(ConversationContext):
|
||||
async def call_container_tool(
|
||||
self, tool_session: Union["ClientSession", Tool], last_msg: Message
|
||||
) -> list[Message]:
|
||||
"""
|
||||
Call container tool. Expect this to be run in a stateful docker
|
||||
"""Call container tool. Expect this to be run in a stateful docker
|
||||
with command line terminal.
|
||||
The official container tool would at least
|
||||
expect the following format:
|
||||
@@ -925,7 +924,7 @@ class HarmonyContext(ConversationContext):
|
||||
]
|
||||
|
||||
async def cleanup_session(self, *args, **kwargs) -> None:
|
||||
"""Can be used as coro to used in __aexit__"""
|
||||
"""Can be used as coro to used in __aexit__."""
|
||||
|
||||
async def cleanup_tool_session(tool_session):
|
||||
if not isinstance(tool_session, Tool):
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""
|
||||
Harmony ↔ Responses API conversion utilities.
|
||||
"""Harmony ↔ Responses API conversion utilities.
|
||||
|
||||
Handles two directions:
|
||||
1. Response Input → Harmony Messages (input parsing)
|
||||
@@ -53,7 +52,8 @@ logger = init_logger(__name__)
|
||||
|
||||
def _parse_harmony_format_message(chat_msg: dict) -> Message:
|
||||
"""Reconstruct a Message from Harmony-format dict,
|
||||
preserving channel, recipient, and content_type."""
|
||||
preserving channel, recipient, and content_type.
|
||||
"""
|
||||
author_dict = chat_msg["author"]
|
||||
role = author_dict.get("role")
|
||||
name = author_dict.get("name")
|
||||
|
||||
@@ -100,9 +100,7 @@ class ResponseUsage(OpenAIBaseModel):
|
||||
|
||||
|
||||
def serialize_message(msg):
|
||||
"""
|
||||
Serializes a single message
|
||||
"""
|
||||
"""Serializes a single message."""
|
||||
if isinstance(msg, dict):
|
||||
return msg
|
||||
elif hasattr(msg, "to_dict"):
|
||||
@@ -113,15 +111,14 @@ def serialize_message(msg):
|
||||
|
||||
|
||||
def serialize_messages(msgs):
|
||||
"""
|
||||
Serializes multiple messages
|
||||
"""
|
||||
"""Serializes multiple messages."""
|
||||
return [serialize_message(msg) for msg in msgs] if msgs else None
|
||||
|
||||
|
||||
class ResponseRawMessageAndToken(OpenAIBaseModel):
|
||||
"""Class to show the raw message.
|
||||
If message / tokens diverge, tokens is the source of truth"""
|
||||
If message / tokens diverge, tokens is the source of truth
|
||||
"""
|
||||
|
||||
message: str
|
||||
tokens: list[int]
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""
|
||||
Streaming SSE event builders for the Responses API.
|
||||
"""Streaming SSE event builders for the Responses API.
|
||||
|
||||
Pure functions that translate streaming state + delta data into
|
||||
OpenAI Response API SSE events. Used by the streaming event
|
||||
@@ -125,8 +124,7 @@ def is_mcp_tool_by_namespace(
|
||||
recipient: str | None,
|
||||
allowed_function_tool_names: frozenset[str] | None = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Determine if a tool call is an MCP tool based on recipient prefix.
|
||||
"""Determine if a tool call is an MCP tool based on recipient prefix.
|
||||
|
||||
Inverse of :func:`is_function_recipient` — everything that is not
|
||||
a function call is an MCP tool.
|
||||
@@ -1145,8 +1143,7 @@ def split_delta(delta: DeltaMessage) -> list[DeltaMessage]:
|
||||
|
||||
|
||||
class SimpleStreamingEventProcessor:
|
||||
"""
|
||||
State-machine processor for the simple (non-Harmony) streaming path.
|
||||
"""State-machine processor for the simple (non-Harmony) streaming path.
|
||||
|
||||
Core flow:
|
||||
1. Resolve the target state from the delta_message
|
||||
@@ -1188,8 +1185,7 @@ class SimpleStreamingEventProcessor:
|
||||
def resolve_target_state(
|
||||
self, delta_message: DeltaMessage
|
||||
) -> tuple[_StateType, Any]:
|
||||
"""
|
||||
Decide which state the next delta belongs to.
|
||||
"""Decide which state the next delta belongs to.
|
||||
|
||||
Priority: TOOL_CALL > REASONING > CONTENT, fallback to NONE.
|
||||
For TOOL_CALL the first tool_call object is also returned so
|
||||
@@ -1207,8 +1203,7 @@ class SimpleStreamingEventProcessor:
|
||||
return _StateType.NONE, None
|
||||
|
||||
def needs_transition(self, target_state: _StateType, tool_call: Any) -> bool:
|
||||
"""
|
||||
Return True when we must close the current state and open a new one.
|
||||
"""Return True when we must close the current state and open a new one.
|
||||
|
||||
Two cases trigger a transition:
|
||||
1. The target state differs from the current state
|
||||
|
||||
@@ -115,8 +115,7 @@ def build_response_output_items(
|
||||
def should_continue_final_message(
|
||||
request_input: str | list[ResponseInputOutputItem],
|
||||
) -> bool:
|
||||
"""
|
||||
Determine if the last input message is a partial assistant message
|
||||
"""Determine if the last input message is a partial assistant message
|
||||
that should be continued rather than starting a new generation.
|
||||
|
||||
This enables partial message completion similar to Anthropic's Messages API,
|
||||
@@ -132,6 +131,7 @@ def should_continue_final_message(
|
||||
|
||||
Returns:
|
||||
True if the final message should be continued, False otherwise
|
||||
|
||||
"""
|
||||
if isinstance(request_input, str):
|
||||
# Simple string input is always a user message
|
||||
@@ -226,8 +226,7 @@ def _construct_message_from_response_item(
|
||||
item: ResponseInputOutputItem,
|
||||
prev_msg: ChatCompletionMessageParam | None = None,
|
||||
) -> ChatCompletionMessageParam | None:
|
||||
"""
|
||||
Returns a new message or None. If `None`, `prev_msg` might be updated.
|
||||
"""Returns a new message or None. If `None`, `prev_msg` might be updated.
|
||||
If `prev_msg` is `None`, a new message is always returned.
|
||||
"""
|
||||
prev_assistant_msg = (
|
||||
@@ -360,9 +359,7 @@ def extract_function_tool_names(tools: list[Tool]) -> frozenset[str]:
|
||||
|
||||
|
||||
def extract_tool_types(tools: list[Tool]) -> set[str]:
|
||||
"""
|
||||
Extracts the tool types from the given tools.
|
||||
"""
|
||||
"""Extracts the tool types from the given tools."""
|
||||
tool_types: set[str] = set()
|
||||
for tool in tools:
|
||||
if tool.type == "mcp":
|
||||
@@ -379,8 +376,7 @@ def extract_tool_types(tools: list[Tool]) -> set[str]:
|
||||
def convert_tool_responses_to_completions_format(
|
||||
tool: dict,
|
||||
) -> ChatCompletionToolsParam:
|
||||
"""
|
||||
Convert a flat Responses tool schema:
|
||||
"""Convert a flat Responses tool schema:
|
||||
{"type": "function", "name": "...", "description": "...", "parameters": {...}}
|
||||
into a Chat Completions tool param for chat-template rendering.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user