explaingit

jd/tenacity

8,600PythonAudience · developerComplexity · 2/5LicenseSetup · easy

TLDR

Tenacity is a Python library that adds retry logic to any function via a decorator, with configurable stop conditions, wait strategies including exponential backoff, and full async support.

Mindmap

mindmap
  root((tenacity))
    Decorator API
      retry decorator
      Retrying class
      AsyncRetrying
    Stop Conditions
      Number of attempts
      Time limit
      Combined with pipe
    Wait Strategies
      Fixed wait
      Random wait
      Exponential backoff
      Jitter
    Retry Conditions
      Exception types
      Return value checks
      Combined conditions
    Callbacks
      before and after
      before_sleep logging
      retry_state details
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Wrap an HTTP API call with retry logic that automatically retries up to 5 times with exponential backoff when a connection error occurs.

USE CASE 2

Retry a database query that fails due to transient connection issues, with a fixed 2-second wait between attempts.

USE CASE 3

Retry a function only on specific exception types while immediately re-raising other errors, avoiding retries on validation failures.

USE CASE 4

Add logging to a retry loop so each failed attempt writes the attempt number and elapsed time to a log file.

Tech stack

Python

Getting it running

Difficulty · easy Time to first run · 5min
Use, modify, and distribute freely including in commercial products, as long as you include the Apache 2.0 license notice.

In plain English

Tenacity is a Python library that makes it easy to add retry logic to any function. When you call an external service, database, or API, requests sometimes fail due to network hiccups or temporary server problems. Rather than writing your own loop with sleep and error handling, you add the @retry decorator to your function and Tenacity handles the retrying automatically. The basic use is a one-liner: add @retry above the function and it will retry forever whenever an exception is raised. From there, you can tune the behavior. You can stop after a specific number of attempts, stop after a total elapsed time, or combine both conditions with the | operator. You can control how long to wait between attempts: a fixed delay, a random delay within a range, or exponential backoff, where each wait is longer than the last. Exponential backoff with random jitter is a common pattern for calling distributed services because it reduces the chance that many clients all retry at exactly the same time. You can also control which situations trigger a retry. By default, any exception restarts the call. But you can tell Tenacity to only retry on specific exception types, skip retries on certain errors like client-side validation failures, or even retry when a function returns a result you do not want, such as None or False. For logging and monitoring, Tenacity accepts callback functions that run before each attempt, after each failed attempt, or before each sleep period. These callbacks receive a retry_state object with details like the attempt number and elapsed time. Tenacity works with async functions on asyncio, Trio, and Tornado, and supports retrying a code block using a for loop and context manager instead of a decorator. It is Apache 2.0 licensed.

Copy-paste prompts

Prompt 1
I'm using tenacity to retry a call to an external API that sometimes returns a rate limit error. Show me how to retry only on that specific exception with exponential backoff and a max of 5 attempts.
Prompt 2
I want to use tenacity with an async function that calls an external service. How do I set up the @retry decorator for async/await and add a before_sleep log callback?
Prompt 3
I'm calling a function that returns None when it fails instead of raising an exception. How do I use tenacity to retry it until it returns a non-None value?
Prompt 4
How do I disable tenacity retries in my test suite so tests fail fast without waiting for all retry attempts to exhaust?
Open on GitHub → Explain another repo

← jd on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.