{"record":{"id":"1204cd2b4b5222e0","repo":"BerriAI/litellm","slug":"tenacity-import-failed-please-run-pip-install-ten","errorCode":null,"errorMessage":"tenacity import failed please run `pip install tenacity`. Error{e}","messagePattern":"tenacity import failed please run `pip install tenacity`\\. Error(.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/main.py","lineNumber":5793,"sourceCode":"    except Exception as e:\n        ## Map to OpenAI Exception\n        raise exception_type(\n            model=model,\n            custom_llm_provider=custom_llm_provider,\n            original_exception=e,\n            completion_kwargs=args,\n            extra_kwargs=kwargs,\n        )\n\n\ndef completion_with_retries(*args, **kwargs):\n    \"\"\"\n    Executes a litellm.completion() with 3 retries\n    \"\"\"\n    try:\n        import tenacity\n    except Exception as e:\n        raise Exception(f\"tenacity import failed please run `pip install tenacity`. Error{e}\")\n\n    num_retries: Final = kwargs.pop(\"num_retries\", 3)\n    # reset retries in .completion()\n    kwargs[\"max_retries\"] = 0\n    kwargs[\"num_retries\"] = 0\n    retry_strategy: Final[Literal[\"exponential_backoff_retry\", \"constant_retry\"]] = kwargs.pop(\n        \"retry_strategy\", \"constant_retry\"\n    )\n    original_function: Final = kwargs.pop(\"original_function\", completion)\n    if retry_strategy == \"exponential_backoff_retry\":\n        retryer = tenacity.Retrying(\n            wait=tenacity.wait_exponential(multiplier=1, max=10),\n            stop=tenacity.stop_after_attempt(num_retries),\n            reraise=True,\n        )\n    else:\n        retryer = tenacity.Retrying(stop=tenacity.stop_after_attempt(num_retries), reraise=True)\n    return retryer(original_function, *args, **kwargs)","sourceCodeStart":5775,"sourceCodeEnd":5811,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/main.py#L5775-L5811","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["pip install tenacity in the same environment as litellm","Or skip the helper: pass num_retries=N to litellm.completion(), which uses litellm's built-in retry loop and needs no extra package","Add tenacity to your requirements/lockfile so rebuilds keep it","Verify with python -c 'import tenacity' in the exact env that runs the code"],"exampleFix":"# before\nresp = litellm.completion_with_retries(model='gpt-4o', messages=m)  # Exception: tenacity import failed\n\n# after (option 1): pip install tenacity\n# after (option 2): use built-in retries, no extra dep\nresp = litellm.completion(model='gpt-4o', messages=m, num_retries=3)","handlingStrategy":"validation","validationCode":"import importlib.util\nif importlib.util.find_spec('tenacity') is None:\n    raise SystemExit('tenacity missing: pip install tenacity, or use completion(num_retries=3)')","typeGuard":"def tenacity_available() -> bool:\n    import importlib.util\n    return importlib.util.find_spec('tenacity') is not None","tryCatchPattern":"try:\n    resp = litellm.completion_with_retries(model='gpt-4o', messages=m)\nexcept Exception as e:\n    if 'tenacity import failed' in str(e):\n        resp = litellm.completion(model='gpt-4o', messages=m, num_retries=3)  # built-in retries\n    else:\n        raise","preventionTips":["Prefer litellm's native num_retries on completion() -- no optional dependency needed","If you use *_with_retries helpers, add tenacity to requirements next to litellm","Include a find_spec('tenacity') check in deploy-time environment validation","Test dependency presence in the same image/venv that runs production, not just locally"],"tags":["tenacity","retries","dependency","litellm"],"backgroundTag":"missing-dependency","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}