diff --git a/litellm/__init__.py b/litellm/__init__.py index 739cef04e2d..8e8815b1257 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -1531,26 +1531,37 @@ def _lazy_import_cost_calculator(name: str) -> Any: # This significantly reduces memory usage when importing litellm def _lazy_import_litellm_logging(name: str) -> Any: """Lazy import for litellm_logging module.""" - from litellm.litellm_core_utils.litellm_logging import ( - Logging as _Logging, - modify_integration as _modify_integration, - ) - - # Map names to imported objects - _logging_objects = { - "Logging": _Logging, - "modify_integration": _modify_integration, - } - - # Cache the imported object in the module namespace - obj = _logging_objects[name] - globals()[name] = obj - - return obj + try: + from litellm.litellm_core_utils.litellm_logging import ( + Logging as _Logging, + modify_integration as _modify_integration, + ) + + # Map names to imported objects + _logging_objects = { + "Logging": _Logging, + "modify_integration": _modify_integration, + } + + # Cache the imported object in the module namespace + obj = _logging_objects[name] + globals()[name] = obj + + return obj + except Exception as e: + # If lazy import fails, raise a more informative error + raise AttributeError( + f"module {__name__!r} has no attribute {name!r}. " + f"Lazy import failed: {e}" + ) from e def __getattr__(name: str) -> Any: - """Lazy import for cost_calculator and litellm_logging functions.""" + """Lazy import for cost_calculator and litellm_logging functions. + + This allows these heavy modules to be loaded only when accessed, + reducing initial import time and memory usage. + """ if name in ("completion_cost", "response_cost_calculator", "cost_per_token"): return _lazy_import_cost_calculator(name) diff --git a/litellm/main.py b/litellm/main.py index b3ce7d7c73b..88bf0b72a0b 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -1154,7 +1154,7 @@ def completion( # type: ignore # noqa: PLR0915 api_base = base_url if num_retries is not None: max_retries = num_retries - logging: Logging = cast(Logging, litellm_logging_obj) + logging: LiteLLMLoggingObj = cast(LiteLLMLoggingObj, litellm_logging_obj) fallbacks = fallbacks or litellm.model_fallbacks if fallbacks is not None: return completion_with_fallbacks(**args) @@ -4094,7 +4094,7 @@ def embedding( # noqa: PLR0915 litellm_params_dict = get_litellm_params(**kwargs) - logging: Logging = litellm_logging_obj # type: ignore + logging: LiteLLMLoggingObj = litellm_logging_obj # type: ignore logging.update_environment_variables( model=model, user=user, @@ -5808,7 +5808,7 @@ def speech( # noqa: PLR0915 kwargs=kwargs, ) - logging_obj: Logging = cast(Logging, kwargs.get("litellm_logging_obj")) + logging_obj: LiteLLMLoggingObj = cast(LiteLLMLoggingObj, kwargs.get("litellm_logging_obj")) logging_obj.update_environment_variables( model=model, user=user,