BerriAI/litellm · error · AttributeError
Utils module lazy import: unknown attribute
Error message
Utils module lazy import: unknown attribute {name!r} What it means
The utils-module lazy handler in litellm/_lazy_imports.py resolves only names listed in _UTILS_MODULE_IMPORT_MAP (cached into utils globals). Accessing litellm.utils-surface names not present in that map raises AttributeError('Utils module lazy import: unknown attribute ...'). It means the requested util symbol is not one of the lazily exported utils attributes — misspelled, removed in a refactor, or living in another module.
Solutions
- Confirm the name exists: check _UTILS_MODULE_IMPORT_MAP in litellm/_lazy_imports.py for your version
- Import from the actual module (from litellm.utils import ...) which bypasses the map restriction for real exports
- Upgrade/downgrade litellm to the version matching your code
- Replace removed helpers with their documented successors
Example fix
# before import litellm litellm.utils.get_model_cost_map # Utils module lazy import: unknown attribute # after from litellm.utils import get_model_cost_map # import from the defining module # or use the documented replacement helper
Defensive patterns
Strategy: type-guard
Validate before calling
import litellm
utils_names = ["get_litellm_params", "token_counter"]
missing = [n for n in utils_names if not hasattr(litellm, n)]
if missing:
raise RuntimeError(f"utils lazy map lacks {missing}; import from litellm.utils directly") Type guard
from typing import Any
import litellm
def utils_attr(name: str) -> Any:
"""Resolve a utils-surface name or raise a clear AttributeError."""
if not hasattr(litellm, name):
raise AttributeError(
f"{name!r} not in _UTILS_MODULE_IMPORT_MAP; use `from litellm.utils import {name}`"
)
return getattr(litellm, name) Try / catch
try:
helper = getattr(litellm, name)
except AttributeError:
from litellm import utils as _utils
helper = getattr(_utils, name, None) or _utils # fall back to module-level lookup Prevention
- Prefer `from litellm.utils import X` for utils helpers — it bypasses the lazy map entirely
- Lock your litellm version in requirements to keep the utils export surface stable
- When copying snippets from issues, check they match your installed major version
When it happens
Trigger: `litellm.get_max_tokens` style accesses routed through the utils lazy path where the name is not in the map; using utils helpers renamed between versions (e.g. older function names after a cleanup); calling internal helpers that were never public.
Common situations: Code written against an older/newer litellm where the utils map changed; porting snippets from GitHub issues that touch litellm internals; autocomplete-driven typos.
Related errors
- lazy import: unknown attribute
- module has no attribute
- HTTP handlers lazy import: unknown attribute
- LLM client cache lazy import: unknown attribute
- {e}
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/cfc3ba650e1156db.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/_lazy_imports.py:339
"""Handler for litellm_logging module (Logging, modify_integration)"""
return _generic_lazy_import(name, _LITELLM_LOGGING_IMPORT_MAP, "Litellm logging")
def _lazy_import_llm_provider_logic(name: str) -> Any:
"""Handler for LLM provider logic functions (get_llm_provider, etc.)"""
return _generic_lazy_import(name, _LLM_PROVIDER_LOGIC_IMPORT_MAP, "LLM provider logic")
def _lazy_import_utils_module(name: str) -> Any:
"""
Handler for utils module lazy imports.
This uses a custom implementation because utils module needs to use
_get_utils_globals() instead of get_litellm_globals() for caching.
"""
# Check if this attribute exists in our map
if name not in _UTILS_MODULE_IMPORT_MAP:
raise AttributeError(f"Utils module lazy import: unknown attribute {name!r}")
# Get the cache (where we store imported things) - use utils globals
_globals: Final = _get_utils_globals()
# If we've already imported it, just return the cached version
if name in _globals:
return _globals[name]
# Look up where to find this attribute
module_path, attr_name = _UTILS_MODULE_IMPORT_MAP[name]
# Import the module
if module_path.startswith("."):
module = importlib.import_module(module_path, package="litellm")
else:
module = importlib.import_module(module_path)
# Get the actual attribute from the moduleView on GitHub (pinned to 77b7c6c40c)