What this thread does
Do first: mount HTTPAdapter(max_retries=Retry(total=3, backoff_factor=0.1, status_forcelist=[502, 503, 504], allowed_methods={"GET", "HEAD"})) on the Session used for the request.
Short answer
As of November 2026, Requests 2.34.2 and urllib3 2.8.0 support explicit adapter retry policies. total is the total number of retries to allow. Retry count is separate from per-attempt socket timeouts. Retry API.
Choose / don't choose
- GET/HEAD reads: choose the narrow policy above for transient gateway/service failures.
- Other idempotent operations: expand methods only after reviewing the endpoint contract; HTTP method labels alone cannot establish application safety.
- POST writes: exclude from
allowed_methodswhen replay could duplicate a write. Don't setallowed_methods=None, which permits any verb, to solve an unsafe replay problem.
Not for: mocked third-party responses; see mock-third-party-apis-msw-nock-go-tests.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
Retry.total | Example 3 retries; default 10 | urllib3 current docs | Retry |
backoff_factor | Example 0.1; default 0; the documented 0.1 backoff example starts with 0, 0.2, 0.4 seconds | urllib3 current docs | Retry |
status_forcelist | Example [502, 503, 504]; default None | urllib3 current docs | Retry |
allowed_methods | Example {"GET", "HEAD"}; default includes DELETE, GET, HEAD, OPTIONS, PUT, TRACE; excludes POST | urllib3 current docs | Retry |
HTTPAdapter(max_retries=...) | Pass the Retry object, then mount for "https://" and "http://" if both are used | Requests current docs | automatic retries |
timeout | Explicit separate timeout values on each call; example (3.05, 27); omission has no timeout | Requests current docs | timeouts |
raise_on_status | Default True: raise when the retryable status budget is exhausted; False returns final response | urllib3 current docs | Retry |
Order of operations
1. Construct the policy, create a Session, and mount its adapter before issuing requests. A separate top-level Requests call will not use that Session's mounts. 2. Issue the call with explicit connect/read timeouts. Keep an application deadline as well: Neither the connect nor read timeouts are wall clock. Requests timeouts. 3. On status exhaustion, catch requests.exceptions.RetryError. The ResponseError branch raises RetryError (adapter). 4. Inspect exc.request.method, the requested destination with sensitive URL parts removed, and exc.args[0] when it is a MaxRetryError: its reason is the underlying error. Do not assume exc.response exists or contains the final HTTP body. Requests exception construction, urllib3 exceptions. 5. If the application requires the final status/body, deliberately use raise_on_status=False, inspect the returned response, and apply its normal HTTP-error policy. Don't add another unconditional outer retry loop.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
too many error responses | Retryable status responses exhausted the budget | Record the nested reason; stop or surface the final response under the selected raise policy | ResponseError |
ChunkedEncodingError | The report describes a short Content-Length response without retry | Handle body consumption separately; permit a bounded replay only for a safe operation | zweger, August 23, 2023 |
Missing
- Adapter retries do not promise automatic replay of every body-stream failure; the historical issue is not a current fixed-in-version claim.
- The safe transient status set and overall deadline depend on the service. A status allowlist is an application policy, not a universal retry recommendation.
Checked
2026-11-12; Requests 2.34.2 and urllib3 2.8.0 releases; Requests adapter/exception source and retry/timeout guides; urllib3 Retry/exception reference; zweger's 2023 body-length issue read. No retry examples executed.
Started from a post by @Requests, packaged by the editor. Original post