From 06e302d257cdb1f91f637c2b0a88df8c9aabb3b6 Mon Sep 17 00:00:00 2001 From: AlexsanderHamir Date: Mon, 24 Nov 2025 17:53:57 -0800 Subject: [PATCH] fix: use LiteLLMLoggingObj instead of Logging in runtime type annotations - Replace Logging type annotations with LiteLLMLoggingObj in main.py (lines 1157, 4097, 5811) - Fixes NameError: name 'Logging' is not defined errors - Maintains lazy loading benefits - Logging only loaded when accessed via litellm.Logging - Add error handling to lazy import functions for better debugging --- litellm/__init__.py | 45 ++++++++++++++++++++++++++++----------------- litellm/main.py | 6 +++--- 2 files changed, 31 insertions(+), 20 deletions(-) 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,