BerriAI/litellm · error · AttributeError
lazy import: unknown attribute
Error message
{category} lazy import: unknown attribute {name!r} What it means
litellm/_lazy_imports.py implements lazy attribute resolution via import maps per category (e.g. utils, cost calculator, LLM provider logic). _generic_lazy_import raises AttributeError with '<category> lazy import: unknown attribute ...' when the requested name is not a key in that category's import_map — i.e. the symbol genuinely isn't part of that lazy-exported surface (typo, removed, or belongs elsewhere).
Solutions
- Verify the exact attribute name in litellm's docs or source (grep the relevant _*_IMPORT_MAP in litellm/_lazy_imports.py)
- Upgrade litellm if the name is a recently added export
- Import directly from the defining submodule instead of the lazy top-level path
- Pin the litellm version your code was validated against
Example fix
# before from litellm import cost_calculator cost_calculator.completion_cost_my_model(...) # unknown attribute via lazy map # after from litellm.cost_cache import completion_cost # canonical import path completion_cost(response)
Defensive patterns
Strategy: type-guard
Validate before calling
import litellm
# validate names once at startup instead of failing at first lazy access
required = ["ModelResponse", "CompletionRequest"]
missing = [n for n in required if not hasattr(litellm, n)]
if missing:
raise RuntimeError(f"litellm {litellm.__version__} lacks: {missing}") Type guard
import litellm
def litellm_export(name: str):
"""Narrowing helper: resolve a lazy export or raise a precise error."""
if not hasattr(litellm, name):
raise AttributeError(
f"{name!r} is not in this litellm version's lazy import maps; "
"check _lazy_imports.py for the exact keys"
)
return getattr(litellm, name) Try / catch
try:
obj = getattr(litellm, name)
except AttributeError as e:
logger.warning("lazy import miss for %s on litellm %s: %s", name, litellm.__version__, e)
raise Prevention
- Cross-check example code version against your installed litellm version
- Grep the *_IMPORT_MAP tables in your installed litellm/_lazy_imports.py to confirm a name is exported
- Import provider/cost internals from their defining submodules for stability
When it happens
Trigger: Accessing litellm.<name> that routes to a lazy handler (provider logic, cost calculator groups) but is missing from the map, e.g. a mistyped provider class or cost function name; using a name added in a newer litellm while running an older one; code reaching for internal helpers that were never exported.
Common situations: Version skew between docs/examples and the installed litellm; accessing provider-internal classes after a refactor moved them; typos caught late because lazy imports defer the failure to first access.
Related errors
- module has no attribute
- Utils module lazy import: unknown 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/c5945dfaefd7a3d8.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/_lazy_imports.py:233
Steps:
1. Check if the name exists in the import map (if not, raise error)
2. Check if we've already imported it (if yes, return cached value)
3. Look up where to find it (module_path and attr_name from the map)
4. Import the module (Python caches this automatically)
5. Get the attribute from the module
6. Cache it in _globals so we don't import again
7. Return it
Args:
name: The attribute name someone is trying to access (e.g., "ModelResponse")
import_map: Dictionary telling us where to find each attribute
Format: {"ModelResponse": (".utils", "ModelResponse")}
category: Just for error messages (e.g., "Utils", "Cost calculator")
"""
# Step 1: Make sure this attribute exists in our map
if name not in import_map:
raise AttributeError(f"{category} lazy import: unknown attribute {name!r}")
# Step 2: Get the cache (where we store imported things)
_globals: Final = get_litellm_globals()
# Step 3: If we've already imported it, just return the cached version
if name in _globals:
return _globals[name]
# Step 4: Look up where to find this attribute
# The map tells us: (module_path, attribute_name)
# Example: (".utils", "ModelResponse") means "look in .utils module, get ModelResponse"
module_path, attr_name = import_map[name]
# Step 5: Import the module
# Python automatically caches modules in sys.modules, so calling this twice is fast
# If module_path starts with ".", it's a relative import (needs package="litellm")
# Otherwise it's an absolute import (like "litellm.caching.caching")
if module_path.startswith("."):View on GitHub (pinned to 77b7c6c40c)