BerriAI/litellm · error · Exception

tenacity import failed please run `pip install tenacity`. Er

Error message

tenacity import failed please run `pip install tenacity`. Error{e}

What it means

Exception raised by completion_with_retries when the tenacity package cannot be imported at call time. tenacity is an optional dependency that powers the *_with_retries helpers (this one: sync completion with 3 retries by default), so without it the retry wrapper cannot be constructed.

Source

Thrown at litellm/main.py:5793

    except Exception as e:
        ## Map to OpenAI Exception
        raise exception_type(
            model=model,
            custom_llm_provider=custom_llm_provider,
            original_exception=e,
            completion_kwargs=args,
            extra_kwargs=kwargs,
        )


def completion_with_retries(*args, **kwargs):
    """
    Executes a litellm.completion() with 3 retries
    """
    try:
        import tenacity
    except Exception as e:
        raise Exception(f"tenacity import failed please run `pip install tenacity`. Error{e}")

    num_retries: Final = kwargs.pop("num_retries", 3)
    # reset retries in .completion()
    kwargs["max_retries"] = 0
    kwargs["num_retries"] = 0
    retry_strategy: Final[Literal["exponential_backoff_retry", "constant_retry"]] = kwargs.pop(
        "retry_strategy", "constant_retry"
    )
    original_function: Final = kwargs.pop("original_function", completion)
    if retry_strategy == "exponential_backoff_retry":
        retryer = tenacity.Retrying(
            wait=tenacity.wait_exponential(multiplier=1, max=10),
            stop=tenacity.stop_after_attempt(num_retries),
            reraise=True,
        )
    else:
        retryer = tenacity.Retrying(stop=tenacity.stop_after_attempt(num_retries), reraise=True)
    return retryer(original_function, *args, **kwargs)

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. pip install tenacity in the same environment as litellm
  2. Or skip the helper: pass num_retries=N to litellm.completion(), which uses litellm's built-in retry loop and needs no extra package
  3. Add tenacity to your requirements/lockfile so rebuilds keep it
  4. Verify with python -c 'import tenacity' in the exact env that runs the code

Example fix

# before
resp = litellm.completion_with_retries(model='gpt-4o', messages=m)  # Exception: tenacity import failed

# after (option 1): pip install tenacity
# after (option 2): use built-in retries, no extra dep
resp = litellm.completion(model='gpt-4o', messages=m, num_retries=3)
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util
if importlib.util.find_spec('tenacity') is None:
    raise SystemExit('tenacity missing: pip install tenacity, or use completion(num_retries=3)')

Type guard

def tenacity_available() -> bool:
    import importlib.util
    return importlib.util.find_spec('tenacity') is not None

Try / catch

try:
    resp = litellm.completion_with_retries(model='gpt-4o', messages=m)
except Exception as e:
    if 'tenacity import failed' in str(e):
        resp = litellm.completion(model='gpt-4o', messages=m, num_retries=3)  # built-in retries
    else:
        raise

Prevention

When it happens

Trigger: Calling litellm.completion_with_retries(...) in an environment where tenacity was never installed -- plain 'pip install litellm' does not pull it in, and slim Docker/CI images usually lack it.

Common situations: New project or CI image built from a minimal requirements list; using a library that internally calls completion_with_retries; virtualenv recreated from an incomplete lockfile.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


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