HOWTO · Python

How to Set Maximum Retries for Requests in Python

This tutorial demonstrates how to set max_retries for requests in Python.

On this page

To set a bounded retry policy for Python Requests, mount an HTTPAdapter configured with Retry on a Session. This lets you choose how many connection, read, and selected HTTP status failures may be retried. Retries can help with temporary failures, but they do not fix an invalid URL, a persistent network problem, or an overloaded server.

Install Requests with python -m pip install requests if it is not already available. The example uses Retry from urllib3, which Requests uses for its adapter retry behavior.

Configure retries with a Session and HTTPAdapter

max_retries is the adapter setting that accepts a Retry policy (or a simple integer limit). A session keeps that adapter configuration for requests sent through the session; it does not change the behavior of unrelated calls made elsewhere in the program. The policy below explicitly limits total and category-specific retries, adds backoff, and selects the HTTP responses and methods eligible for status retries. total=4 allows up to four retries; the initial request is separate, so a request can make up to five attempts when every retry is eligible. Category-specific limits such as connect=3, read=2, and status=2 apply within the total retry limit.

Run this example:

import requests
from urllib3.util.retry import Retry

retry_policy = Retry(
    total=4,
    connect=3,
    read=2,
    status=2,
    backoff_factor=0.3,
    status_forcelist=(429, 502, 503, 504),
    allowed_methods=frozenset({"GET", "HEAD"}),
)

session = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_retries=retry_policy)
session.mount("https://", adapter)
session.mount("http://", adapter)

Use session.get(url, timeout=5) for a request that should follow this policy. The setup itself makes no network request. Mount both schemes only if the application needs both; mount a single scheme to restrict where the adapter applies.

Understand retry limits, backoff, and retryable responses

The total, connect, read, and status values bound different parts of retry behavior. They are limits, not a promise that every request receives that many additional attempts: a request can succeed earlier, a failure may not qualify for retry, or a category limit may be reached first. status_forcelist identifies HTTP response codes for status retries, while allowed_methods restricts those retries to the listed request methods. With the example policy, a status such as 503 is eligible for retry on GET and HEAD; a response outside the list is not selected by that status rule. Connection failures are governed by connection retry settings rather than by the HTTP status list.

backoff_factor configures increasing waits between eligible attempts. A larger factor can make the client wait longer; it does not make a server recover or guarantee success. Exact timing can depend on the installed urllib3 version and policy details, so treat backoff as a delay strategy rather than a fixed schedule. Additional attempts also increase request latency and can add load to a struggling service. Choose limits that fit the operation and the service’s retry guidance.

Diagnose Max retries exceeded

A message containing Max retries exceeded means the request could not complete within the applicable retry policy. Read the full exception and identify the URL, failure reason, and any underlying connection error. A connection or DNS failure points toward connectivity or hostname resolution; verify the URL and network path. An HTTP response such as 429 or 503 is different: it came from a server, and only configured status codes and methods are retried. If attempts are exhausted, inspect the final response or exception context to distinguish the server’s answer from a transport failure.

A malformed URL, unavailable host, persistent outage, or continued server overload will remain a problem after the configured attempts. Do not increase retry limits reflexively: first determine which failure is recurring and whether retrying that operation is appropriate. For operations that change server state, retry only when the operation and server behavior make repeating it safe.

Distinguish retries from timeouts and connection reuse

A request’s timeout limits how long Requests waits for connection establishment or response activity; it does not set the number of attempts. A scalar such as timeout=5 applies a limit to both phases. A tuple such as timeout=(3, 6) sets separate connect and read limits, respectively. A timeout can cause a failure that the retry policy may handle if it falls within an eligible category, but changing the timeout alone does not configure retries.

Keep-alive allows a session to reuse connections. It is separate from max_retries, and disabling connection reuse is not a general fix for retry exhaustion. Configure retries for transient failures, timeouts for waiting limits, and connection reuse according to the application’s performance and server requirements.