Skip to main content

stamina: Production-grade Retries Made Easy

Transient failures are common in distributed systems. To make your systems resilient, you need to retry failed operations. But bad retries can make things much worse.

stamina is an opinionated wrapper around the great-but-unopinionated Tenacity package. Our goal is to be as ergonomic as possible, while doing the right thing by default, and minimizing the potential for misuse. It is the result of years of copy-pasting the same configuration over and over again:

  • Retry only on certain exceptions – or even a subset of them by introspecting them first using a backoff hook.
  • Exponential backoff with jitter between retries.
  • Limit the number of retries and total time.
  • Automatic async support – including Trio.
  • Preserve type hints of the decorated callable.
  • Flexible instrumentation with Prometheus, structlog, and standard library's logging support out-of-the-box.
  • Dedicated support for testing that allows to globally deactivate retries, or to limit the number of retries and to remove backoffs.

For example:

import httpx2

import stamina


@stamina.retry(on=httpx2.HTTPError, attempts=3)
def do_it(code: int) -> httpx2.Response:
    resp = httpx2.get(f"https://httpbin.org/status/{code}")
    resp.raise_for_status()

    return resp

Async callables work use the same API and it's possible to retry arbitrary blocks, too. Check out our tutorial for more examples!

Or, if you prefer video, here's a brief introduction into retries and stamina: Watch the video

Release Information

Removed

  • Support for Python 3.10.

Deprecated

  • Raising tenacity.TryAgain in code that stamina retries now emits a DeprecationWarning. It only ever worked because stamina uses Tenacity internally. Raise stamina.exceptions.TryAgain instead.

    Generally speaking, we do not support "hybrid" usage of Tenacity features via stamina and will break such cases without warning in the future. #158

Added

  • stamina.exceptions.TryAgain that you can raise to force a retry, whatever on says. If stamina runs out of attempts, it raises the exception that you raised TryAgain from. Without one, your caller gets the TryAgain.

    Pass after to wait at least a number of seconds, a datetime.timedelta, or until a timezone-aware datetime.datetime before the next attempt. #158

  • stamina.instrumentation.set_process_details() that lets you change the stamina.instrumentation.RetryDetails before stamina passes them to the retry hooks. For example, you can remove secrets from the arguments before they are logged. stamina.instrumentation.get_process_details() returns the current one. #160

  • stamina.retry_context(), stamina.RetryingCaller(), and stamina.AsyncRetryingCaller() now accept a keyword-only name for retry instrumentation, like Prometheus labels. The default remains <context block>. #165

Changed

  • The type hints of on now accept subclasses of BaseException, backoff hooks are typed to receive a BaseException, and stamina.instrumentation.RetryDetails.caused_by is typed as a BaseException. This matches what stamina always did at runtime; we've only imposed our opinion via type hints which isn't always useful. If you annotated the argument of a backoff hook with Exception, your type checker now wants BaseException. #155

  • If a backoff would end after timeout, stamina now gives up right away. Before, it waited past timeout and attempted once more. This also applies to custom backoffs from backoff hooks. #158

  • On Python 3.15 and later, stamina now uses lazy imports. #159

Fixed

  • Context managers returned by stamina.instrumentation.RetryHooks are now exited only once. #157

Full Changelog →

Credits

stamina is written by Hynek Schlawack and distributed under the terms of the MIT license.

The development is kindly supported by my employer Variomedia AG and all my amazing GitHub Sponsors.

This project would not be possible without the years of incredible work that went into Tenacity.

stamina for Enterprise

Available as part of the Tidelift Subscription.

The maintainers of stamina and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open-source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use.

Metadata

Release files for stamina 26.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stamina 26.2.0
File Size Uploaded Provenance Actions
stamina-26.2.0.tar.gz 670.2 kB With provenance Details for stamina-26.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for stamina 26.2.0
File Interpreter ABI Platform Actions
stamina-26.2.0-py3-none-any.whl Python 3 none any Details for stamina-26.2.0-py3-none-any.whl

Total release size: 693.5 kB

Release files / stamina-26.2.0.tar.gz

Download URL stamina-26.2.0.tar.gz
Size 670.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c81efa445d6cd6f898584faa7556df09f6e4d25c16e219914b6422eeb3372985
BLAKE2b-256 checksum
How to use checksums
4e46e5491b7985761f6870bf11af1c461f57102f564686f2fe7b8637ebcbb0f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Release files / stamina-26.2.0-py3-none-any.whl

Download URL stamina-26.2.0-py3-none-any.whl
Size 23.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4c9688f204d90544cd0c5bdd2a66fd82c6e7cb8248d70687ac69da3e7f5434f6
BLAKE2b-256 checksum
How to use checksums
fe14ff4255606425fb66f0f4e0f5ac30e98c87a1895df615fe97ce662c551e52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Security for stamina 26.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for stamina-26.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page