Tenacity – The Most Elegant Retry Library in Python 2026
Tenacity makes retry logic clean and powerful with decorators.
Practical Example
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
import requests
@retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=1, min=2, max=30),
retry=retry_if_exception_type(requests.exceptions.RequestException)
)
def call_external_api(url):
response = requests.get(url, timeout=10)
response.raise_for_status()
return response.json()
# Usage
data = call_external_api("https://api.example.com/data")
Conclusion
Tenacity is essential for any robust automation script in 2026.