{"record":{"id":"c5945dfaefd7a3d8","repo":"BerriAI/litellm","slug":"category-lazy-import-unknown-attribute-name-r","errorCode":null,"errorMessage":"{category} lazy import: unknown attribute {name!r}","messagePattern":"(.+?) lazy import: unknown attribute (.+?)","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"litellm/_lazy_imports.py","lineNumber":233,"sourceCode":"\n    Steps:\n    1. Check if the name exists in the import map (if not, raise error)\n    2. Check if we've already imported it (if yes, return cached value)\n    3. Look up where to find it (module_path and attr_name from the map)\n    4. Import the module (Python caches this automatically)\n    5. Get the attribute from the module\n    6. Cache it in _globals so we don't import again\n    7. Return it\n\n    Args:\n        name: The attribute name someone is trying to access (e.g., \"ModelResponse\")\n        import_map: Dictionary telling us where to find each attribute\n                   Format: {\"ModelResponse\": (\".utils\", \"ModelResponse\")}\n        category: Just for error messages (e.g., \"Utils\", \"Cost calculator\")\n    \"\"\"\n    # Step 1: Make sure this attribute exists in our map\n    if name not in import_map:\n        raise AttributeError(f\"{category} lazy import: unknown attribute {name!r}\")\n\n    # Step 2: Get the cache (where we store imported things)\n    _globals: Final = get_litellm_globals()\n\n    # Step 3: If we've already imported it, just return the cached version\n    if name in _globals:\n        return _globals[name]\n\n    # Step 4: Look up where to find this attribute\n    # The map tells us: (module_path, attribute_name)\n    # Example: (\".utils\", \"ModelResponse\") means \"look in .utils module, get ModelResponse\"\n    module_path, attr_name = import_map[name]\n\n    # Step 5: Import the module\n    # Python automatically caches modules in sys.modules, so calling this twice is fast\n    # If module_path starts with \".\", it's a relative import (needs package=\"litellm\")\n    # Otherwise it's an absolute import (like \"litellm.caching.caching\")\n    if module_path.startswith(\".\"):","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/_lazy_imports.py#L215-L251","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nfrom litellm import cost_calculator\ncost_calculator.completion_cost_my_model(...)  # unknown attribute via lazy map\n\n# after\nfrom litellm.cost_cache import completion_cost  # canonical import path\ncompletion_cost(response)","handlingStrategy":"type-guard","validationCode":"import litellm\n\n# validate names once at startup instead of failing at first lazy access\nrequired = [\"ModelResponse\", \"CompletionRequest\"]\nmissing = [n for n in required if not hasattr(litellm, n)]\nif missing:\n    raise RuntimeError(f\"litellm {litellm.__version__} lacks: {missing}\")","typeGuard":"import litellm\n\ndef litellm_export(name: str):\n    \"\"\"Narrowing helper: resolve a lazy export or raise a precise error.\"\"\"\n    if not hasattr(litellm, name):\n        raise AttributeError(\n            f\"{name!r} is not in this litellm version's lazy import maps; \"\n            \"check _lazy_imports.py for the exact keys\"\n        )\n    return getattr(litellm, name)","tryCatchPattern":"try:\n    obj = getattr(litellm, name)\nexcept AttributeError as e:\n    logger.warning(\"lazy import miss for %s on litellm %s: %s\", name, litellm.__version__, e)\n    raise","preventionTips":["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"],"tags":["attributeerror","lazy-import","import-map","api-surface","python"],"backgroundTag":"module-has-no-attribute","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}