Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions litellm/assistants/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

def get_optional_params_add_message(
role: str | None,
content: str | List[MessageContentTextObject | MessageContentImageFileObject | MessageContentImageURLObject] | None,
attachments: List[Attachment] | None,
content: str | list[MessageContentTextObject | MessageContentImageFileObject | MessageContentImageURLObject] | None,
attachments: list[Attachment] | None,
metadata: dict | None,
custom_llm_provider: str,
**kwargs,
Expand Down
4 changes: 2 additions & 2 deletions litellm/fine_tuning/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def acreate_fine_tuning_job(
hyperparameters: dict | None = {},
suffix: str | None = None,
validation_file: str | None = None,
integrations: List[str] | None = None,
integrations: list[str] | None = None,
seed: int | None = None,
custom_llm_provider: Literal["openai", "azure", "vertex_ai"] = "openai",
extra_headers: dict[str, str] | None = None,
Expand Down Expand Up @@ -157,7 +157,7 @@ def create_fine_tuning_job(
hyperparameters: dict | None = {},
suffix: str | None = None,
validation_file: str | None = None,
integrations: List[str] | None = None,
integrations: list[str] | None = None,
seed: int | None = None,
custom_llm_provider: Literal["openai", "azure", "vertex_ai"] = "openai",
extra_headers: dict[str, str] | None = None,
Expand Down
40 changes: 19 additions & 21 deletions litellm/llms/bedrock/chat/invoke_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class AmazonCohereChatConfig:
Reference - https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-cohere-command-r-plus.html
"""

documents: List[Document] | None = None
documents: list[Document] | None = None
search_queries_only: bool | None = None
preamble: str | None = None
max_tokens: int | None = None
Expand All @@ -69,12 +69,12 @@ class AmazonCohereChatConfig:
presence_penalty: float | None = None
seed: int | None = None
return_prompt: bool | None = None
stop_sequences: List[str] | None = None
stop_sequences: list[str] | None = None
raw_prompting: bool | None = None

def __init__(
self,
documents: List[Document] | None = None,
documents: list[Document] | None = None,
search_queries_only: bool | None = None,
preamble: str | None = None,
max_tokens: int | None = None,
Expand Down Expand Up @@ -112,7 +112,7 @@ def get_config(cls):
and v is not None
}

def get_supported_openai_params(self) -> List[str]:
def get_supported_openai_params(self) -> list[str]:
return [
"max_tokens",
"max_completion_tokens",
Expand Down Expand Up @@ -325,7 +325,7 @@ def __init__(self, model: str, json_mode: bool | None = False) -> None:

self.model = model
self.parser = EventStreamJSONParser()
self.content_blocks: List[ContentBlockDeltaEvent] = []
self.content_blocks: list[ContentBlockDeltaEvent] = []
self.tool_calls_index: int | None = None
self.response_id: str | None = None
self.json_mode = json_mode
Expand Down Expand Up @@ -362,13 +362,13 @@ def extract_reasoning_content_str(

def translate_thinking_blocks(
self, thinking_block: BedrockConverseReasoningContentBlockDelta
) -> List[Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock]] | None:
) -> list[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock] | None:
"""
Translate the thinking blocks to a string
"""

thinking_blocks_list: Final[List[Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock]]] = []
_thinking_block: Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock] | None = None
thinking_blocks_list: Final[list[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock]] = []
_thinking_block: ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock | None = None

if "text" in thinking_block:
_thinking_block = ChatCompletionThinkingBlock(type="thinking")
Expand Down Expand Up @@ -402,12 +402,12 @@ def _handle_converse_start_event(
) -> tuple[
ChatCompletionToolCallChunk | None,
dict,
List[Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock]] | None,
list[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock] | None,
]:
"""Handle 'start' event in converse chunk parsing."""
tool_use: ChatCompletionToolCallChunk | None = None
provider_specific_fields: dict = {}
thinking_blocks: List[Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock]] | None = None
thinking_blocks: list[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock] | None = None

self.content_blocks = [] # reset
if start_obj is not None:
Expand Down Expand Up @@ -450,14 +450,14 @@ def _handle_converse_delta_event(
ChatCompletionToolCallChunk | None,
dict,
str | None,
List[Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock]] | None,
list[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock] | None,
]:
"""Handle 'delta' event in converse chunk parsing."""
text = ""
tool_use: ChatCompletionToolCallChunk | None = None
provider_specific_fields: dict = {}
reasoning_content: str | None = None
thinking_blocks: List[Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock]] | None = None
thinking_blocks: list[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock] | None = None

self.content_blocks.append(delta_obj)
if "text" in delta_obj:
Expand Down Expand Up @@ -535,7 +535,7 @@ def converse_chunk_parser(self, chunk_data: dict) -> ModelResponseStream:
usage: Usage | None = None
provider_specific_fields: dict = {}
reasoning_content: str | None = None
thinking_blocks: List[Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock]] | None = None
thinking_blocks: list[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock] | None = None

content_block_index: Final = int(chunk_data.get("contentBlockIndex", 0))
if "start" in chunk_data:
Expand Down Expand Up @@ -590,7 +590,7 @@ def converse_chunk_parser(self, chunk_data: dict) -> ModelResponseStream:
except Exception as e:
raise Exception(f"Received streaming error - {e}")

def _chunk_parser(self, chunk_data: dict) -> Union[GChunk, ModelResponseStream, dict]:
def _chunk_parser(self, chunk_data: dict) -> GChunk | ModelResponseStream | dict:
text = ""
is_finished = False
finish_reason = ""
Expand Down Expand Up @@ -645,7 +645,7 @@ def _chunk_parser(self, chunk_data: dict) -> Union[GChunk, ModelResponseStream,
tool_use=None,
)

def iter_bytes(self, iterator: Iterator[bytes]) -> Iterator[Union[GChunk, ModelResponseStream, dict]]:
def iter_bytes(self, iterator: Iterator[bytes]) -> Iterator[GChunk | ModelResponseStream | dict]:
"""Given an iterator that yields lines, iterate over it & yield every event encountered"""
from botocore.eventstream import EventStreamBuffer

Expand All @@ -659,9 +659,7 @@ def iter_bytes(self, iterator: Iterator[bytes]) -> Iterator[Union[GChunk, ModelR
_data = json.loads(message)
yield self._chunk_parser(chunk_data=_data)

async def aiter_bytes(
self, iterator: AsyncIterator[bytes]
) -> AsyncIterator[Union[GChunk, ModelResponseStream, dict]]:
async def aiter_bytes(self, iterator: AsyncIterator[bytes]) -> AsyncIterator[GChunk | ModelResponseStream | dict]:
"""Given an async iterator that yields lines, iterate over it & yield every event encountered"""
from botocore.eventstream import EventStreamBuffer

Expand Down Expand Up @@ -741,7 +739,7 @@ def __init__(
sync_stream=sync_stream,
)

def _chunk_parser(self, chunk_data: dict) -> Union[GChunk, ModelResponseStream, dict]:
def _chunk_parser(self, chunk_data: dict) -> GChunk | ModelResponseStream | dict:
return self.deepseek_model_response_iterator.chunk_parser(chunk=chunk_data)


Expand All @@ -756,7 +754,7 @@ def __iter__(self):
return self

def _handle_json_mode_chunk(
self, text: str, tool_calls: List[ChatCompletionToolCallChunk] | None
self, text: str, tool_calls: list[ChatCompletionToolCallChunk] | None
) -> tuple[str, ChatCompletionToolCallChunk | None]:
"""
If JSON mode is enabled, convert the tool call to a message.
Expand Down Expand Up @@ -789,7 +787,7 @@ def _chunk_parser(self, chunk_data: ModelResponse) -> GChunk:
text = chunk_data.choices[0].message.content or "" # type: ignore
tool_use = None
_model_response_tool_call: Final = cast(
List[ChatCompletionMessageToolCall] | None,
list[ChatCompletionMessageToolCall] | None,
cast(Choices, chunk_data.choices[0]).message.tool_calls,
)
if self.json_mode is True:
Expand Down
30 changes: 15 additions & 15 deletions litellm/llms/openai/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def _get_openai_client(
client: OpenAI | AsyncOpenAI | None = None,
shared_session: Optional["ClientSession"] = None,
) -> OpenAI | AsyncOpenAI | None:
client_initialization_params: Final[Dict] = locals()
client_initialization_params: Final[dict] = locals()
if client is None:
if not isinstance(max_retries, int):
raise OpenAIError(
Expand Down Expand Up @@ -402,7 +402,7 @@ async def make_openai_chat_completion_request(
data: dict,
timeout: float | httpx.Timeout,
logging_obj: LiteLLMLoggingObj,
) -> Tuple[dict, BaseModel]:
) -> tuple[dict, BaseModel]:
"""
Helper to:
- call chat.completions.create.with_raw_response when litellm.return_response_headers is True
Expand Down Expand Up @@ -439,7 +439,7 @@ def make_sync_openai_chat_completion_request(
data: dict,
timeout: float | httpx.Timeout,
logging_obj: LiteLLMLoggingObj,
) -> Tuple[dict, BaseModel]:
) -> tuple[dict, BaseModel]:
"""
Helper to:
- call chat.completions.create.with_raw_response when litellm.return_response_headers is True
Expand Down Expand Up @@ -474,11 +474,11 @@ async def _call_agentic_completion_hooks_openai(
self,
response: Any,
model: str,
messages: list[Dict],
optional_params: Dict,
messages: list[dict],
optional_params: dict,
logging_obj: LiteLLMLoggingObj,
stream: bool,
litellm_params: Dict,
litellm_params: dict,
) -> Any | None:
"""
Call agentic completion hooks for all custom loggers (OpenAI Chat Completions API).
Expand Down Expand Up @@ -1288,7 +1288,7 @@ def embedding( # type: ignore
)

## embedding CALL
headers: Dict | None = None
headers: dict | None = None
headers, sync_embedding_response = self.make_sync_openai_embedding_request(
openai_client=openai_client,
data=data,
Expand Down Expand Up @@ -2852,7 +2852,7 @@ async def arun_thread(
assistant_id: str,
additional_instructions: str | None,
instructions: str | None,
metadata: Dict | None,
metadata: dict | None,
model: str | None,
stream: bool | None,
tools: Iterable[AssistantToolParam] | None,
Expand Down Expand Up @@ -2891,12 +2891,12 @@ def async_run_thread_stream(
assistant_id: str,
additional_instructions: str | None,
instructions: str | None,
metadata: Dict | None,
metadata: dict | None,
model: str | None,
tools: Iterable[AssistantToolParam] | None,
event_handler: AssistantEventHandler | None,
) -> AsyncAssistantStreamManager[AsyncAssistantEventHandler]:
data: Final[Dict[str, Any]] = {
data: Final[dict[str, Any]] = {
"thread_id": thread_id,
"assistant_id": assistant_id,
"additional_instructions": additional_instructions,
Expand All @@ -2916,12 +2916,12 @@ def run_thread_stream(
assistant_id: str,
additional_instructions: str | None,
instructions: str | None,
metadata: Dict | None,
metadata: dict | None,
model: str | None,
tools: Iterable[AssistantToolParam] | None,
event_handler: AssistantEventHandler | None,
) -> AssistantStreamManager[AssistantEventHandler]:
data: Final[Dict[str, Any]] = {
data: Final[dict[str, Any]] = {
"thread_id": thread_id,
"assistant_id": assistant_id,
"additional_instructions": additional_instructions,
Expand All @@ -2943,7 +2943,7 @@ def run_thread(
assistant_id: str,
additional_instructions: str | None,
instructions: str | None,
metadata: Dict | None,
metadata: dict | None,
model: str | None,
stream: bool | None,
tools: Iterable[AssistantToolParam] | None,
Expand All @@ -2965,7 +2965,7 @@ def run_thread(
assistant_id: str,
additional_instructions: str | None,
instructions: str | None,
metadata: Dict | None,
metadata: dict | None,
model: str | None,
stream: bool | None,
tools: Iterable[AssistantToolParam] | None,
Expand All @@ -2988,7 +2988,7 @@ def run_thread(
assistant_id: str,
additional_instructions: str | None,
instructions: str | None,
metadata: Dict | None,
metadata: dict | None,
model: str | None,
stream: bool | None,
tools: Iterable[AssistantToolParam] | None,
Expand Down
18 changes: 9 additions & 9 deletions litellm/llms/openai/responses/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ def remove_cache_control_flag_from_input_and_tools(
self,
model: str, # allows overrides to selectively run this
input: str | ResponseInputParam,
tools: List[ALL_RESPONSES_API_TOOL_PARAMS] | None = None,
) -> Tuple[
tools: list[ALL_RESPONSES_API_TOOL_PARAMS] | None = None,
) -> tuple[
str | ResponseInputParam,
List[ALL_RESPONSES_API_TOOL_PARAMS] | None,
list[ALL_RESPONSES_API_TOOL_PARAMS] | None,
]:
"""Sibling of `remove_cache_control_flag_from_messages_and_tools` on
the chat path. Strips Anthropic-only `cache_control` markers from
Expand Down Expand Up @@ -447,7 +447,7 @@ def transform_delete_response_api_request(
api_base: str,
litellm_params: GenericLiteLLMParams,
headers: dict,
) -> Tuple[str, dict]:
) -> tuple[str, dict]:
"""
Transform the delete response API request into a URL and data

Expand Down Expand Up @@ -482,7 +482,7 @@ def transform_get_response_api_request(
api_base: str,
litellm_params: GenericLiteLLMParams,
headers: dict,
) -> Tuple[str, dict]:
) -> tuple[str, dict]:
"""
Transform the get response API request into a URL and data

Expand Down Expand Up @@ -525,10 +525,10 @@ def transform_list_input_items_request(
headers: dict,
after: str | None = None,
before: str | None = None,
include: List[str] | None = None,
include: list[str] | None = None,
limit: int = 20,
order: Literal["asc", "desc"] = "desc",
) -> Tuple[str, dict]:
) -> tuple[str, dict]:
encoded_response_id: Final = encode_url_path_segment(response_id, field_name="response_id")
url: Final = f"{api_base}/{encoded_response_id}/input_items"
params: Final[dict[str, Any]] = {}
Expand Down Expand Up @@ -563,7 +563,7 @@ def transform_cancel_response_api_request(
api_base: str,
litellm_params: GenericLiteLLMParams,
headers: dict,
) -> Tuple[str, dict]:
) -> tuple[str, dict]:
"""
Transform the cancel response API request into a URL and data

Expand Down Expand Up @@ -607,7 +607,7 @@ def transform_compact_response_api_request(
api_base: str,
litellm_params: GenericLiteLLMParams,
headers: dict,
) -> Tuple[str, dict]:
) -> tuple[str, dict]:
"""
Transform the compact response API request into a URL and data

Expand Down
9 changes: 5 additions & 4 deletions litellm/llms/volcengine/responses/transformation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Callable, Mapping, Sequence
from types import UnionType
from typing import TYPE_CHECKING, Any, Final, Literal, Protocol, Union, get_args, get_origin

import httpx
Expand Down Expand Up @@ -475,14 +476,12 @@ def _default_for_annotation(annotation: object) -> object:
return 0
if annotation is list or origin is list:
return []
if origin is Union:
if origin is Union or origin is UnionType:
# Prefer empty list when any option is a list
if any((arg is list or VolcEngineResponsesAPIConfig._annotation_origin(arg) is list) for arg in args):
return []
if type(None) in args:
return None
if origin is Union and type(None) in args:
return None

# Fallback to None when no safer guess exists
return None
Expand Down Expand Up @@ -514,7 +513,9 @@ def _pick_model_class(annotation: object, value: object) -> object | None:
Choose the best-matching Pydantic model class for a nested dict.
"""
origin: Final = VolcEngineResponsesAPIConfig._annotation_origin(annotation)
union_args: Final = VolcEngineResponsesAPIConfig._annotation_args(annotation) if origin is Union else ()
union_args: Final = (
VolcEngineResponsesAPIConfig._annotation_args(annotation) if origin is Union or origin is UnionType else ()
)
candidates = tuple(candidate for candidate in (annotation, *union_args) if hasattr(candidate, "model_fields"))

if not candidates:
Expand Down
Loading
Loading