python-backoff is a Python library providing function decorators for configurable backoff and retry strategies. It allows you to wrap functions to automatically retry their execution until a specified condition is met, such as handling transient failures from unreliable network resources or external APIs. The library supports both synchronous and asynchronous (asyncio) code, offering various wait generators like exponential, fibonacci, and constant backoff. It is actively maintained, with recent releases in late 2025, and the current version is 2.3.1.
pip install backoffVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use the `@backoff.on_exception` decorator to automatically retry a function that might fail intermittently. It configures exponential backoff with jitter, sets a maximum number of tries and total elapsed time, and includes `on_backoff` and `on_giveup` handlers for logging retry attempts. The example simulates a flaky API that raises exceptions for the first few calls, eventually succeeding or giving up.
Upgrade your Python environment to 3.8 or newer before upgrading `backoff`.
If your event handlers inspect `details['target']` and expect a string, update your logic to handle a function object instead. For example, use `details['target'].__name__` to get the function name.
Ensure that the exceptions you want `backoff` to handle are propagated out of the decorated function. Avoid broad `except:` clauses, or explicitly `raise` the exception after handling it internally if retries are still desired.
Always specify the wait strategy and the exceptions. Example: `@backoff.on_exception(backoff.expo, SomeError, max_tries=5)`.
No dependency data recorded yet.