BerriAI/litellm · error · AttributeError

HTTP handlers lazy import: unknown attribute

Error message

HTTP handlers lazy import: unknown attribute {name!r}

What it means

The HTTP-handler lazy loader in litellm/_lazy_imports.py recognizes only 'module_level_aclient' (async httpx client via get_async_httpx_client) and 'module_level_client' (sync HTTPHandler, both honoring request_timeout). Any other name routed to this handler raises AttributeError('HTTP handlers lazy import: unknown attribute ...'), typically from a typo when reaching for litellm's shared HTTP clients.

Solutions

  1. Use exactly litellm.module_level_client (sync) or litellm.module_level_aclient (async)
  2. For test patching, target the concrete handler: litellm.llms.custom_httpx.http_handler.HTTPHandler / get_async_httpx_client
  3. Set timeouts via litellm.request_timeout rather than reconstructing clients
  4. Guard optional access with hasattr

Example fix

# before
import litellm
litellm.module_level_aclients  # typo -> HTTP handlers lazy import: unknown attribute

# after
import litellm
litellm.module_level_aclient   # async httpx client
litellm.module_level_client    # sync HTTPHandler
Defensive patterns

Strategy: type-guard

Validate before calling

import litellm

VALID = {"module_level_client", "module_level_aclient"}
name = "module_level_aclient"
assert name in VALID, f"{name!r} invalid; valid handler attrs: {sorted(VALID)}"

Type guard

import litellm
from typing import Optional

def get_module_client(async_: bool = False) -> Optional[object]:
    """Safely fetch litellm's shared HTTP client, or None if unavailable."""
    return getattr(litellm, "module_level_aclient" if async_ else "module_level_client", None)

Try / catch

try:
    client = litellm.module_level_aclient
except AttributeError:
    from litellm.llms.custom_httpx.http_handler import get_async_httpx_client
    client = get_async_httpx_client(llm_provider="my_app")  # construct directly

Prevention

When it happens

Trigger: litellm.module_level_clients, litellm.aclient, or other guesses at the shared-client attributes; monkeypatching litellm's HTTP clients in tests with the wrong name; hasattr probes for client instrumentation.

Common situations: Test suites patching litellm's transport for mocking; debugging request timeouts by inspecting the module-level client; names changed across versions.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/589cf3d37f627b7a. Report an issue: GitHub.

Appendix: source

Thrown at litellm/_lazy_imports.py:447

            params=params,
        )

        # Cache it so we don't create it again
        _globals["module_level_aclient"] = async_client
        return async_client

    if name == "module_level_client":
        # Create a sync HTTP client
        from litellm.llms.custom_httpx.http_handler import HTTPHandler

        timeout = _globals.get("request_timeout")
        sync_client: Final = HTTPHandler(timeout=timeout)

        # Cache it
        _globals["module_level_client"] = sync_client
        return sync_client

    raise AttributeError(f"HTTP handlers lazy import: unknown attribute {name!r}")

View on GitHub (pinned to 77b7c6c40c)