retryable-requests
If you fail, don't bail.
A requests session with sensible retries and timeouts built in.
A plain requests session never retries and never times out. RetryableSession does both by
default, so that a brief outage or a stalled connection is handled rather than passed on.
Installation
pip install retryable-requests
Python 3.12 or later is required.
Usage
from retryable_requests import RetryableSession
with RetryableSession(base_url="https://api.example.com/v1/") as session:
response = session.get("items/")
response.raise_for_status()
RetryableSession is a requests.Session,
used like any other,
which also:
- retries1 a request which:
- waits between retries with exponential backoff and jitter4
- a
Retry-Afterheader sets the wait instead5
- a
- gives every request a default timeout6
- a request may pass its own timeout instead
- optionally joins every request's URL to
base_url, forgiving of slashes - keeps the error semantics of
requests:- when a failing HTTP response exhausts the retries, it is returned, and can be surfaced by
raise_for_status() - a failure to connect or a timeout, once the retries are exhausted, raises an exception
- when a failing HTTP response exhausts the retries, it is returned, and can be surfaced by
- does not retry a request once its response has begun to arrive; a failure while its body is
read raises
ConnectionError7
Options
Every argument to RetryableSession is optional and keyword-only.
| Argument | Type | Default |
|---|---|---|
base_url |
str | None |
None |
timeout |
tuple[float | None, float | None] | float | None |
(3.05, 30) |
retry_strategy |
urllib3.util.Retry |
DEFAULT_RETRY_STRATEGY |
pool_maxsize |
int |
10 |
base_url
The URL to join each request's URL to, forgiving of slashes: for example,
https://api.example.com/v1 joins either of items/ or /items/ to
https://api.example.com/v1/items/.
base_url is ignored if a request specifies an absolute URL.
base_url may not have a query string or fragment.
timeout
A tuple of the connect and read timeouts in seconds, applied to every request. Either may be
None, to disable the timeout. Alternatively, a single value will apply the same timeout to both
connecting and reading.
See the requests
timeouts documentation
for more details.
retry_strategy (advanced)
A urllib3.util.Retry policy, applied to every request.
See the urllib3
documentation
for more details.
To change the retry policy, derive one from the default with Retry.new, which replaces each
setting it is given rather than adding to it. For example, to also retry POST requests which
are known to be safe to repeat:
from retryable_requests import DEFAULT_RETRY_STRATEGY, RetryableSession
from urllib3.util import Retry
retry_strategy = DEFAULT_RETRY_STRATEGY.new(
allowed_methods=Retry.DEFAULT_ALLOWED_METHODS | {"POST"}
)
session = RetryableSession(retry_strategy=retry_strategy)
pool_maxsize (advanced)
The number of connections kept open to each host, which should be at least the number of threads sharing the session. More threads than that still get connections, but ones opened for a single request and then closed.
However, note that requests
does not guarantee thread-safety,
particularly when cookies are set.
-
A request is retried up to five times. In the worst case, with the default timeout, a request which stalls every time fails after over three minutes, and one answered every time with a long
Retry-Afterreturns its last response after five minutes. ↩ -
GET,HEAD,PUT,DELETE,OPTIONSorTRACEare idempotent and should be safe to repeat even if the server has started processing another invocation. ↩ -
HTTP statuses 408, 429, 500, 502, 503 or 504 allow that a repeated request may succeed. A 413 is retried too when a
Retry-Afterheader marks it as temporary. ↩ -
The first retry is immediate; the rest wait 1, 2, 4 and 8 seconds, plus up to half a second of jitter. ↩
-
A
Retry-Afterof more than a minute is shortened to a minute. ↩ -
A request times out after 3.05 seconds to connect and 30 seconds between bytes. ↩
-
A response has begun to arrive once its status and headers are in. At that point, a timeout or a lost connection while the body downloads raises
ConnectionError, streamed or not, and it is up to the caller to repeat the request. ↩
Metadata
Release files for retryable-requests 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| retryable_requests-1.0.0.tar.gz | 14.6 kB | Without provenance | Details for retryable_requests-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| retryable_requests-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for retryable_requests-1.0.0-py3-none-any.whl |
Total release size: 28.1 kB
Release files / retryable_requests-1.0.0.tar.gz
| Download URL | retryable_requests-1.0.0.tar.gz |
|---|---|
| Size | 14.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
28677a144d606c29907372da256f9bfae37a935fcad557a675307c34165f5d83
|
|
BLAKE2b-256 checksum How to use checksums |
2d2b960e17276c9cbc83d334a11de0db1b373875cf108a37c5826f1d0fbe03c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / retryable_requests-1.0.0-py3-none-any.whl
| Download URL | retryable_requests-1.0.0-py3-none-any.whl |
|---|---|
| Size | 13.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c4a28ac9c73831d7a3d33f3d1609f99b6e23cafcf42d47e51629860a60a790fd
|
|
BLAKE2b-256 checksum How to use checksums |
e09884c0bff815793281605ca2134a0b28d710c3f3488d26e2b7d5e40314e9ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for retryable-requests 1.0.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.