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]

fix: resolve ruff PLR0915 and mypy type checking lint errors (#22359)

Jaeyeon Kim(김재연)7a33d51edf09
diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py
index 5d1d6d412dd..1704861686e 100644
--- a/litellm/completion_extras/litellm_responses_transformation/transformation.py
+++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py
@@ -162,7 +162,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
"type": "message",
"role": role,
"content": self._convert_content_to_responses_format(
- content,
+ content, # type: ignore[arg-type]
role, # type: ignore
),
}
@@ -214,7 +214,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
{
"type": "message",
"role": role,
- "content": self._convert_content_to_responses_format(content, cast(str, role)),
+ "content": self._convert_content_to_responses_format(content, cast(str, role)), # type: ignore[arg-type]
}
)
diff --git a/litellm/litellm_core_utils/realtime_streaming.py b/litellm/litellm_core_utils/realtime_streaming.py
index 294f9c485c1..14a25e61d63 100644
--- a/litellm/litellm_core_utils/realtime_streaming.py
+++ b/litellm/litellm_core_utils/realtime_streaming.py
@@ -233,9 +233,9 @@ class RealTimeStreaming:
message, self.model, self.session_configuration_request
)
for msg in transformed:
- await self.backend_ws.send(msg) # type: ignore[union-attr]
+ await self.backend_ws.send(msg) # type: ignore[union-attr, attr-defined]
else:
- await self.backend_ws.send(message) # type: ignore[union-attr]
+ await self.backend_ws.send(message) # type: ignore[union-attr, attr-defined]
def _has_realtime_guardrails(self) -> bool:
"""Return True if any callback is registered for realtime guardrail event types."""
@@ -376,7 +376,7 @@ class RealTimeStreaming:
"[realtime guardrail] ending session after violation %d",
self._violation_count,
)
- await self.backend_ws.close() # type: ignore[union-attr]
+ await self.backend_ws.close() # type: ignore[union-attr, attr-defined]
verbose_logger.warning(
"[realtime guardrail] BLOCKED transcript (violation %d): %r",
diff --git a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py
index 40d8a6df05d..926719c4abf 100644
--- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py
+++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py
@@ -191,8 +191,8 @@ class AnthropicResponsesStreamWrapper:
cache_creation_tokens = getattr(usage, "input_tokens_details", None) # type: ignore[assignment]
cache_read_tokens = getattr(usage, "output_tokens_details", None) # type: ignore[assignment]
# Prefer direct cache fields if present
- cache_creation_tokens = getattr(usage, "cache_creation_input_tokens", 0) or 0
- cache_read_tokens = getattr(usage, "cache_read_input_tokens", 0) or 0
+ cache_creation_tokens = int(getattr(usage, "cache_creation_input_tokens", 0) or 0)
+ cache_read_tokens = int(getattr(usage, "cache_read_input_tokens", 0) or 0)
# Check if tool_use was in the output to override stop_reason
if response_obj is not None:
diff --git a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py
index 6fe28805c42..935babe4380 100644
--- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py
+++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py
@@ -310,7 +310,7 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
# output_format / output_config.format -> text format
# output_format: {"type": "json_schema", "schema": {...}}
# output_config: {"format": {"type": "json_schema", "schema": {...}}}
- output_format = anthropic_request.get("output_format")
+ output_format: Any = anthropic_request.get("output_format")
output_config = anthropic_request.get("output_config")
if not isinstance(output_format, dict) and isinstance(output_config, dict):
output_format = output_config.get("format") # type: ignore[assignment]
diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py
index c4ffc1a60f1..a2705ceb7da 100644
--- a/litellm/proxy/auth/user_api_key_auth.py
+++ b/litellm/proxy/auth/user_api_key_auth.py
@@ -1582,7 +1582,7 @@ async def _lookup_end_user_and_apply_budget(
_apply_budget_limits_to_end_user_params(
end_user_params=end_user_params,
budget_info=end_user_object.litellm_budget_table,
- end_user_id=valid_token.end_user_id,
+ end_user_id=valid_token.end_user_id or "",
)
valid_token = update_valid_token_with_end_user_params(
valid_token=valid_token, end_user_params=end_user_params
@@ -1600,7 +1600,7 @@ async def _lookup_end_user_and_apply_budget(
_apply_budget_limits_to_end_user_params(
end_user_params=end_user_params,
budget_info=default_budget,
- end_user_id=valid_token.end_user_id,
+ end_user_id=valid_token.end_user_id or "",
)
valid_token = update_valid_token_with_end_user_params(
valid_token=valid_token, end_user_params=end_user_params
diff --git a/litellm/proxy/management_endpoints/common_utils.py b/litellm/proxy/management_endpoints/common_utils.py
index 76a11c4dfd4..5a2af0b37c7 100644
--- a/litellm/proxy/management_endpoints/common_utils.py
+++ b/litellm/proxy/management_endpoints/common_utils.py
@@ -11,6 +11,8 @@ from litellm.proxy._types import (
LiteLLM_TeamTable,
LiteLLM_UserTable,
LitellmUserRoles,
+ NewProjectRequest,
+ UpdateProjectRequest,
UserAPIKeyAuth,
)
from litellm.proxy.utils import _premium_user_check
diff --git a/litellm/proxy/middleware/in_flight_requests_middleware.py b/litellm/proxy/middleware/in_flight_requests_middleware.py
index f7f75f4a551..ac3ab2c1552 100644
--- a/litellm/proxy/middleware/in_flight_requests_middleware.py
+++ b/litellm/proxy/middleware/in_flight_requests_middleware.py
@@ -6,7 +6,7 @@ Prometheus gauge `litellm_in_flight_requests`.
"""
import os
-from typing import Optional
+from typing import Any, Optional
from starlette.types import ASGIApp, Receive, Scope, Send
@@ -27,7 +27,7 @@ class InFlightRequestsMiddleware:
"""
_in_flight: int = 0
- _gauge: Optional[object] = None
+ _gauge: Optional[Any] = None
_gauge_init_attempted: bool = False
def __init__(self, app: ASGIApp) -> None:
@@ -55,14 +55,13 @@ class InFlightRequestsMiddleware:
return InFlightRequestsMiddleware._in_flight
@staticmethod
- def _get_gauge() -> Optional[object]:
+ def _get_gauge() -> Optional[Any]:
if InFlightRequestsMiddleware._gauge_init_attempted:
return InFlightRequestsMiddleware._gauge
InFlightRequestsMiddleware._gauge_init_attempted = True
try:
from prometheus_client import Gauge
- kwargs = {}
if "PROMETHEUS_MULTIPROC_DIR" in os.environ:
# livesum aggregates across all worker processes in the scrape response
kwargs["multiprocess_mode"] = "livesum"
diff --git a/litellm/utils.py b/litellm/utils.py
index 02fb346bba9..cf135c8e194 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -8399,7 +8399,9 @@ class ProviderConfigManager:
elif LlmProviders.CLARIFAI == provider:
return litellm.ClarifaiConfig()
elif LlmProviders.BEDROCK == provider:
- return litellm.llms.bedrock.common_utils.BedrockModelInfo()
+ from litellm.llms.bedrock.common_utils import BedrockModelInfo
+
+ return BedrockModelInfo()
elif LlmProviders.AZURE_AI == provider:
from litellm.llms.azure_ai.common_utils import AzureFoundryModelInfo