Skip to content

How to fix TooManyRedirects error in Python requests?

A TooManyRedirects exception means the HTTP client kept following redirects and never reached a final response.

OUTPUT FORMATSHTMLJSONMarkdown

By default, requests stops after 30 redirects in a row and raises the error. Before changing that limit, check whether the destination is genuinely redirecting more than 30 times or whether the site is deliberately looping automated traffic.

What the error actually means

The requests library follows redirects automatically. Each response with a redirect status points to another URL, and the client follows it. When the count exceeds the default limit of 30 redirects, requests raises TooManyRedirects instead of continuing. The exception is a safety stop, not a network failure: the connection worked, but the redirect chain did not terminate within the allowed number of hops.

Decision checklist: legitimate chain or loop?

Use these checks before you touch max_redirects:

  1. Inspect the chain. Log each response URL and status code to see whether the hops are distinct pages or the same pair repeating.
  2. Confirm the site is not buggy. A site that redirects more than 30 times is unusual; a misconfigured rule or a broken canonical redirect can cause it.
  3. Consider bot detection. A site may treat your requests as automated and intentionally send you in a redirection loop.
  4. If the chain is legitimate and finite, raise the limit. If the chain is a loop, raising the limit only delays the failure.
  5. If the loop is caused by bot detection, address the detection cause rather than the redirect count.

Raising the redirect limit on a Session

The redirect limit is a property of a requests.Session object, not a per-request argument. Create a session, set session.max_redirects to the number you need, then make the request through that session. The example below sets the limit to 40 and requests a URL that redirects 39 times, so the chain completes within the new limit.

Field map

requests.Session() creates the session object that holds the redirect setting. session.max_redirects is the attribute you assign the new redirect count to; it applies to requests made through that session. session.get(url) sends the request and follows redirects up to the session limit. The default limit is 30 redirects; the example raises it to 40.

When raising the limit is the wrong fix

If the redirect chain repeats the same URLs, the site is looping you and a higher limit will not produce a final page. That pattern often points to bot detection or a broken redirect rule on the server. In those cases, fix the request behavior or the server configuration instead of increasing max_redirects. Raising the limit is appropriate only when you have confirmed the chain is finite and each hop is a real navigation step.

Frequently asked questions

What is the default redirect limit in Python requests?

The default is 30 redirects. If a request encounters more than 30 redirects in a row, requests raises TooManyRedirects.

How do I increase the redirect limit?

Create a requests.Session, set session.max_redirects to the number you want, and send the request through that session. The setting applies to requests made with that session.

Why does the site keep redirecting me?

The site may be buggy, or it may be detecting your requests as automated and intentionally sending you in a redirection loop. Check the chain before raising the limit.

Does raising max_redirects always fix the error?

No. It helps only when the redirect chain is legitimate and finite. If the chain is a loop, the error will recur once the new limit is reached.