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
This commit is contained in:
AlexsanderHamir 2025-11-24 17:53:57 -08:00
parent 8df9ff39f7
commit 06e302d257
2 changed files with 31 additions and 20 deletions

View file

@ -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)

View file

@ -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,