Registry / http-networking / stamina

stamina

JSON →
library26.1.0pypypi✓ verified 24d ago

Stamina is a Python library that provides production-grade retries made easy, acting as an opinionated wrapper around Tenacity. It ensures resilient systems by handling transient failures with sensible defaults like exponential backoff with jitter, configurable exception handling, and automatic async support. It releases regularly, typically every few months, and is designed to minimize potential for misuse.

pip install stamina
INSTALL
IMPORT
SIG · STAMINA
S
stamina
http-networkingpythonv26.1.0
Install
2.0s avg
Import
98ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v26.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.104s · 22.2MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.0s · import 0.092s · 23MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

retry
from stamina import retry
Main decorator for retrying functions.
retry_context
from stamina import retry_context
For retrying arbitrary code blocks using a context manager.
set_testing
from stamina import set_testing
For activating/deactivating test mode and controlling retry behavior during tests.
RetryingCaller
from stamina import RetryingCaller
For retrying single function/method calls without decorating.

This quickstart demonstrates using the `@stamina.retry` decorator with `httpx` to handle transient HTTP errors. It configures retries for `httpx.HTTPStatusError` but includes a custom check to only retry on 5xx status codes, explicitly not retrying on 4xx client errors, and limits attempts and total timeout.

import httpx import stamina import os @stamina.retry(on=httpx.HTTPStatusError, attempts=3, timeout=5) def fetch_url_with_retry(url: str) -> str: try: response = httpx.get(url, timeout=1) response.raise_for_status() # Raises HTTPStatusError for 4xx/5xx responses return f"Successfully fetched: {response.status_code}" except httpx.HTTPStatusError as e: # Example: only retry on 5xx errors if e.response.status_code >= 500: print(f"Retrying on {e.response.status_code} for {url}...") raise else: print(f"Not retrying on {e.response.status_code} for {url}.") raise except httpx.RequestError as e: print(f"Request error for {url}: {e}, retrying...") raise # Example usage: try: # This URL will fail with 500 and retry print(fetch_url_with_retry("https://httpbin.org/status/500")) except (httpx.HTTPStatusError, httpx.RequestError) as e: print(f"Final failure after retries for 500: {e}") try: # This URL will fail with 404 and not retry (due to the if e.response.status_code >= 500 check) print(fetch_url_with_retry("https://httpbin.org/status/404")) except (httpx.HTTPStatusError, httpx.RequestError) as e: print(f"Final failure for 404 (not retried): {e}") try: # This URL will succeed print(fetch_url_with_retry("https://httpbin.org/status/200")) except (httpx.HTTPStatusError, httpx.RequestError) as e: print(f"Unexpected failure for 200: {e}")
Debug
Known issues
breakingStamina versions 25.2.0 and newer require Python 3.10 or higher. Projects on older Python versions (3.8, 3.9) must update their Python environment before upgrading to the latest stamina versions.
fix
Upgrade your Python environment to 3.10 or newer.
affects: >=25.2.0
gotchaThe `on` parameter in `stamina.retry()` is mandatory and has no default value. Unlike some other retry libraries, `stamina` explicitly forces you to define which exceptions trigger a retry, preventing accidental retries on non-transient errors (e.g., `Exception` generally).
fix
Always explicitly specify the `on` parameter, e.g., `@stamina.retry(on=httpx.HTTPError)` or `on=(ConnectionError, TimeoutError)`.
affects: All versions
gotchaWhen testing retry logic, `stamina.set_testing()` is now recommended to be used as a context manager. While older global activation might still work, using it as a context manager ensures proper cleanup and isolated test environments, preventing test interference.
fix
Wrap your test code that modifies retry behavior in a `with stamina.set_testing(...)` block.
affects: >=25.1.0
gotchaSupport for retrying generator functions and async generator functions (introduced in 25.2.0) is provisional and may be removed in future versions if it causes unforeseen complexities.
fix
Be aware that relying heavily on generator function retries might be subject to future changes. Monitor release notes for updates.
affects: >=25.2.0
gotchaVersions prior to 24.1.0 contained a bug where deactivating `stamina` globally (e.g., for testing) might not have worked correctly, leading to decorated blocks being retried even when deactivated.
fix
Ensure you are using `stamina` version 24.1.0 or newer to guarantee correct deactivation behavior. For testing, also consider using `stamina.set_testing()` as a context manager.
affects: <24.1.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'stamina'
The 'stamina' library is not installed in the current Python environment.
fix
pip install stamina
TypeError: retry() got an unexpected keyword argument 'wait'
The `stamina.retry` decorator does not accept a `wait` argument. It simplifies Tenacity's wait strategies by providing direct parameters like `wait_min` and `wait_max`.
fix
Replace `wait` with appropriate `stamina.retry` parameters such as `wait_min`, `wait_max`, `wait_initial`, `wait_multiplier`, or `wait_jitter`. Example: `@stamina.retry(wait_min=0.1, wait_max=10)`.
AttributeError: module 'stamina' has no attribute 'wait_fixed'
The `stamina` library simplifies Tenacity's complex wait strategies and does not expose specific wait functions like `wait_fixed` or `wait_exponential` as separate importable objects.
fix
Configure retry delay behavior using the `stamina.retry` decorator's built-in parameters: `wait_min`, `wait_max`, `wait_initial`, `wait_multiplier`, and `wait_jitter`.
TypeError: retry() missing 1 required positional argument: 'func'
When specifying multiple exception types for the `on` argument in `stamina.retry`, they must be provided as a single tuple, not as separate keyword arguments. The second exception is misinterpreted as the decorated function.
fix
Wrap multiple exception types in a tuple for the `on` argument. Example: `@stamina.retry(on=(ValueError, KeyError))`.
Upgrade
Version history
26.1.0latest on PyPI · released Apr 13, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.10 or newer.
httpxoptionalCommonly used in examples for HTTP requests; not a core dependency but useful for quickstart.
prometheus-clientoptionalOptional for Prometheus instrumentation of retries.
structlogoptionalOptional for structlog instrumentation of retries.
Agent activity
24 hits · last 30 days
node
20
OpenAI (training)
1
Resources