Skip to content

Commit f8b80bc

Browse files
Lazy load provider_list and priority_reservation_settings
- Make provider_list and priority_reservation_settings lazy-loaded via __getattr__ - Lazy load types.proxy.management_endpoints.ui_sso imports (DefaultTeamSSOParams, LiteLLM_UpperboundKeyGenerateParams) - Keep LlmProviders and PriorityReservationSettings as direct imports (needed by other modules) - Remove non-essential comments - Significantly reduces import-time memory usage
1 parent efcc634 commit f8b80bc

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

litellm/__init__.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,13 @@
7878
KeyManagementSystem,
7979
KeyManagementSettings,
8080
)
81-
from litellm.types.proxy.management_endpoints.ui_sso import (
82-
DefaultTeamSSOParams,
83-
LiteLLM_UpperboundKeyGenerateParams,
84-
)
85-
# Types utils imports are lazy-loaded to reduce import-time memory cost
86-
# They are created on first access via __getattr__
87-
# However, some are needed at module level for initialization, so import them directly
8881
from litellm.types.utils import LlmProviders, PriorityReservationSettings
89-
# Import only for type checking; runtime access is via __getattr__
9082
if TYPE_CHECKING:
9183
from litellm.integrations.custom_logger import CustomLogger
9284
from litellm.types.llms.bedrock import COHERE_EMBEDDING_INPUT_TYPES
9385
from litellm.types.guardrails import GuardrailItem
94-
from litellm.types.utils import CredentialItem, BudgetConfig, PriorityReservationDict, StandardKeyGenerationConfig
86+
from litellm.types.utils import CredentialItem, BudgetConfig, PriorityReservationDict, StandardKeyGenerationConfig, LlmProviders, PriorityReservationSettings
87+
from litellm.types.proxy.management_endpoints.ui_sso import DefaultTeamSSOParams, LiteLLM_UpperboundKeyGenerateParams
9588
import httpx
9689
import dotenv
9790
from litellm.llms.custom_httpx.async_client_cleanup import register_async_client_cleanup
@@ -382,10 +375,10 @@ def __getattr__(self, name: str) -> Any:
382375
aws_sqs_callback_params: Optional[Dict] = None
383376
generic_logger_headers: Optional[Dict] = None
384377
default_key_generate_params: Optional[Dict] = None
385-
upperbound_key_generate_params: Optional[LiteLLM_UpperboundKeyGenerateParams] = None
378+
upperbound_key_generate_params: Optional["LiteLLM_UpperboundKeyGenerateParams"] = None
386379
key_generation_settings: Optional["StandardKeyGenerationConfig"] = None
387380
default_internal_user_params: Optional[Dict] = None
388-
default_team_params: Optional[Union[DefaultTeamSSOParams, Dict]] = None
381+
default_team_params: Optional[Union["DefaultTeamSSOParams", Dict]] = None
389382
default_team_settings: Optional[List] = None
390383
max_user_budget: Optional[float] = None
391384
default_max_internal_user_budget: Optional[float] = None
@@ -413,10 +406,6 @@ def __getattr__(self, name: str) -> Any:
413406
public_model_groups_links: Dict[str, str] = {}
414407
#### REQUEST PRIORITIZATION #######
415408
priority_reservation: Optional[Dict[str, Union[float, "PriorityReservationDict"]]] = None
416-
# PriorityReservationSettings is imported at top level since it's needed for initialization
417-
priority_reservation_settings: "PriorityReservationSettings" = (
418-
PriorityReservationSettings()
419-
)
420409

421410

422411
######## Networking Settings ########
@@ -927,8 +916,6 @@ def add_known_models():
927916

928917
model_list_set = set(model_list)
929918

930-
# LlmProviders is imported at top level since it's needed for initialization
931-
provider_list: List[Union["LlmProviders", str]] = list(LlmProviders)
932919

933920

934921
models_by_provider: dict = {
@@ -1761,8 +1748,6 @@ def _lazy_import_caching(name: str) -> Any:
17611748
raise AttributeError(f"Caching lazy import: unknown attribute {name!r}")
17621749

17631750

1764-
# Lazy import for types.utils to avoid loading the module at import time
1765-
# This significantly reduces memory usage when importing litellm
17661751
def _lazy_import_types_utils(name: str) -> Any:
17671752
"""Lazy import for types.utils module - imports only the requested item by name."""
17681753
if name == "ImageObject":
@@ -1818,6 +1803,21 @@ def _lazy_import_types_utils(name: str) -> Any:
18181803
raise AttributeError(f"Types utils lazy import: unknown attribute {name!r}")
18191804

18201805

1806+
def _lazy_import_ui_sso(name: str) -> Any:
1807+
"""Lazy import for types.proxy.management_endpoints.ui_sso module - imports only the requested item by name."""
1808+
if name == "DefaultTeamSSOParams":
1809+
from litellm.types.proxy.management_endpoints.ui_sso import DefaultTeamSSOParams as _DefaultTeamSSOParams
1810+
globals()["DefaultTeamSSOParams"] = _DefaultTeamSSOParams
1811+
return _DefaultTeamSSOParams
1812+
1813+
if name == "LiteLLM_UpperboundKeyGenerateParams":
1814+
from litellm.types.proxy.management_endpoints.ui_sso import LiteLLM_UpperboundKeyGenerateParams as _LiteLLM_UpperboundKeyGenerateParams
1815+
globals()["LiteLLM_UpperboundKeyGenerateParams"] = _LiteLLM_UpperboundKeyGenerateParams
1816+
return _LiteLLM_UpperboundKeyGenerateParams
1817+
1818+
raise AttributeError(f"UI SSO lazy import: unknown attribute {name!r}")
1819+
1820+
18211821
def __getattr__(name: str) -> Any:
18221822
"""Lazy import for cost_calculator, litellm_logging, and utils functions."""
18231823
if name in {"completion_cost", "response_cost_calculator", "cost_per_token"}:
@@ -1867,6 +1867,19 @@ def __getattr__(name: str) -> Any:
18671867
if name in _types_utils_names:
18681868
return _lazy_import_types_utils(name)
18691869

1870+
if name in {"DefaultTeamSSOParams", "LiteLLM_UpperboundKeyGenerateParams"}:
1871+
return _lazy_import_ui_sso(name)
1872+
1873+
if name == "provider_list":
1874+
provider_list_val = list(LlmProviders)
1875+
globals()["provider_list"] = provider_list_val
1876+
return provider_list_val
1877+
1878+
if name == "priority_reservation_settings":
1879+
prs_val = PriorityReservationSettings()
1880+
globals()["priority_reservation_settings"] = prs_val
1881+
return prs_val
1882+
18701883
# Lazy-load CustomLogger to avoid circular imports
18711884
if name == "CustomLogger":
18721885
from litellm.integrations.custom_logger import CustomLogger as _CustomLogger

0 commit comments

Comments
 (0)