Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

refactor(types): replace Any with proven types in 5 files (#43304)

devin-ai-integration[bot]f4308bc124ee
diff --git a/litellm/assistants/main.py b/litellm/assistants/main.py
index 1ce40e94320..c14c4aec093 100644
--- a/litellm/assistants/main.py
+++ b/litellm/assistants/main.py
@@ -3,7 +3,7 @@
import asyncio
import contextvars
import os
-from collections.abc import Coroutine, Iterable
+from collections.abc import Coroutine, Iterable, Mapping, Sequence
from functools import partial
from typing import Any, Final, Literal
@@ -233,8 +233,8 @@ def create_assistants(
name: str | None = None,
description: str | None = None,
instructions: str | None = None,
- tools: list[dict[str, Any]] | None = None,
- tool_resources: dict[str, Any] | None = None,
+ tools: Sequence[Mapping[str, object]] | None = None,
+ tool_resources: Mapping[str, object] | None = None,
metadata: dict[str, str] | None = None,
temperature: float | None = None,
top_p: float | None = None,
@@ -244,7 +244,7 @@ def create_assistants(
api_base: str | None = None,
api_version: str | None = None,
**kwargs,
-) -> Assistant | Coroutine[Any, Any, Assistant]:
+) -> Assistant | Coroutine[None, None, Assistant]:
async_create_assistants: Final[bool | None] = kwargs.pop("async_create_assistants", None)
if async_create_assistants is not None and not isinstance(async_create_assistants, bool):
raise ValueError("Invalid value passed in for async_create_assistants. Only bool or None allowed")
@@ -283,7 +283,7 @@ def create_assistants(
# only send params that are not None
create_assistant_data = {k: v for k, v in create_assistant_data.items() if v is not None}
- response: Coroutine[Any, Any, Assistant] | Assistant | None = None
+ response: Coroutine[None, None, Assistant] | Assistant | None = None
if custom_llm_provider == "openai":
api_base = (
optional_params.api_base # for deepinfra/perplexity/anyscale/groq we check in get_llm_provider and pass in the api base from there
@@ -415,7 +415,7 @@ def delete_assistant(
api_base: str | None = None,
api_version: str | None = None,
**kwargs,
-) -> AssistantDeleted | Coroutine[Any, Any, AssistantDeleted]:
+) -> AssistantDeleted | Coroutine[None, None, AssistantDeleted]:
optional_params: Final = GenericLiteLLMParams(api_key=api_key, api_base=api_base, api_version=api_version, **kwargs)
litellm_params_dict: Final = get_litellm_params(**kwargs)
@@ -440,7 +440,7 @@ def delete_assistant(
elif timeout is None:
timeout = 600.0
- response: AssistantDeleted | Coroutine[Any, Any, AssistantDeleted] | None = None
+ response: AssistantDeleted | Coroutine[None, None, AssistantDeleted] | None = None
if custom_llm_provider == "openai":
api_base = (
optional_params.api_base
diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py
index 152fd54e55d..5b4187846ff 100644
--- a/litellm/litellm_core_utils/litellm_logging.py
+++ b/litellm/litellm_core_utils/litellm_logging.py
@@ -640,8 +640,8 @@ class Logging(LiteLLMLoggingBaseClass):
self._own_session_id: str = session_id_var.get()
self.function_id = function_id
- self.streaming_chunks: list[Any] = [] # for generating complete stream response
- self.sync_streaming_chunks: list[Any] = [] # for generating complete stream response
+ self.streaming_chunks: list[object] = [] # for generating complete stream response
+ self.sync_streaming_chunks: list[object] = [] # for generating complete stream response
self.log_raw_request_response = log_raw_request_response
self.raw_request_only = raw_request_only
@@ -693,7 +693,7 @@ class Logging(LiteLLMLoggingBaseClass):
self.response_timing_metrics: Mapping[str, float] = {} # mutable-ok: kept deep-copyable
# Passthrough endpoint guardrails config for field targeting
- self.passthrough_guardrails_config: dict[str, Any] | None = None
+ self.passthrough_guardrails_config: dict[str, object] | None = None
self.model_call_details: dict[str, Any] = {
"litellm_trace_id": self.litellm_trace_id,
@@ -4479,7 +4479,7 @@ def set_callbacks(callback_list, function_id=None):
def _init_custom_logger_compatible_class(
logging_integration: _custom_logger_compatible_callbacks_literal,
internal_usage_cache: DualCache | None,
- llm_router: Any | None, # expect litellm.Router, but typing errors due to circular import
+ llm_router: object, # expect litellm.Router, but typing errors due to circular import
custom_logger_init_args: dict | None = {},
) -> CustomLogger | None:
"""
@@ -6439,7 +6439,7 @@ def _autorouter_savings_for_payload(
def get_standard_logging_object_payload(
kwargs: dict | None,
- init_response_obj: Any | BaseModel | dict,
+ init_response_obj: object,
start_time: dt_object,
end_time: dt_object,
logging_obj: Logging,
diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py
index 8aa38ff3341..ce9f7a2ea54 100644
--- a/litellm/llms/custom_httpx/llm_http_handler.py
+++ b/litellm/llms/custom_httpx/llm_http_handler.py
@@ -6375,7 +6375,7 @@ class BaseLLMHTTPHandler:
custom_llm_provider: str | None = None,
first_message: str | None = None,
request_defaults: ResponsesWebSocketRequestDefaults | None = None,
- **kwargs: Any,
+ **kwargs: object,
) -> Exception | None:
"""
Handles Responses API WebSocket mode.
@@ -10378,13 +10378,14 @@ class BaseLLMHTTPHandler:
encoded_vector_store_id: Final = encode_url_path_segment(vector_store_id, field_name="vector_store_id")
url: Final = f"{api_base}/{encoded_vector_store_id}"
- request_body: Final[dict[str, Any]] = dict(vector_store_update_optional_params)
+ request_body: Final[dict[str, object]] = dict(vector_store_update_optional_params)
+ metadata: Final = vector_store_update_optional_params.get("metadata")
# Clean metadata to only include string values (OpenAI requirement)
- if "metadata" in request_body and request_body["metadata"] is not None:
+ if metadata is not None:
from litellm.utils import add_openai_metadata
- request_body["metadata"] = add_openai_metadata(request_body["metadata"])
+ request_body["metadata"] = add_openai_metadata(metadata)
if extra_body:
request_body.update(extra_body)
@@ -10456,13 +10457,14 @@ class BaseLLMHTTPHandler:
encoded_vector_store_id: Final = encode_url_path_segment(vector_store_id, field_name="vector_store_id")
url: Final = f"{api_base}/{encoded_vector_store_id}"
- request_body: Final[dict[str, Any]] = dict(vector_store_update_optional_params)
+ request_body: Final[dict[str, object]] = dict(vector_store_update_optional_params)
+ metadata: Final = vector_store_update_optional_params.get("metadata")
# Clean metadata to only include string values (OpenAI requirement)
- if "metadata" in request_body and request_body["metadata"] is not None:
+ if metadata is not None:
from litellm.utils import add_openai_metadata
- request_body["metadata"] = add_openai_metadata(request_body["metadata"])
+ request_body["metadata"] = add_openai_metadata(metadata)
if extra_body:
request_body.update(extra_body)
diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.py
index 64b0c6c1967..15610da9aec 100644
--- a/litellm/proxy/common_request_processing.py
+++ b/litellm/proxy/common_request_processing.py
@@ -739,7 +739,7 @@ async def _parse_event_data_for_error(event_line: str | bytes) -> int | None:
if not json_str or json_str == "[DONE]": # handle empty data or [DONE] message
return None
try:
- data: Final = orjson.loads(json_str)
+ data: Final[object] = orjson.loads(json_str)
if isinstance(data, dict) and "error" in data and isinstance(data["error"], dict):
error_code_raw: Final = data["error"].get("code")
error_code: int | None = None
@@ -792,7 +792,7 @@ def _extract_error_from_sse_chunk(event_line: str | bytes) -> dict:
return default_error
try:
- data: Final = orjson.loads(json_str)
+ data: Final[object] = orjson.loads(json_str)
if isinstance(data, dict) and "error" in data:
error_obj: Final = data["error"]
if isinstance(error_obj, dict):
@@ -4131,7 +4131,9 @@ class ProxyBaseLLMRequestProcessing:
if stripped_ln.startswith("data:"):
json_part = stripped_ln.split("data:", 1)[1].strip()
if json_part and json_part != "[DONE]":
- obj = json.loads(json_part)
+ obj: object = json.loads(json_part)
+ if not isinstance(obj, dict):
+ return None
maybe_modified = ProxyBaseLLMRequestProcessing._inject_cost_into_usage_dict(
obj, model_name, litellm_logging_obj
)
diff --git a/litellm/utils.py b/litellm/utils.py
index 7ce412e818c..d45b29c0f16 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -1469,7 +1469,7 @@ async def async_pre_call_deployment_hook(kwargs: dict[str, Any], call_type: str)
async def async_post_call_success_deployment_hook(
request_data: dict, response: object, call_type: CallTypes | None
-) -> Any | None:
+) -> object:
"""
Allow modifying / reviewing the response just after it's received from the deployment.
"""