Skip to main content

A retry layer for HTTPX.


HTTPX Retries implements request retry for HTTPX.

It's very common to deal with flaky and unreliable APIs. When requests fail, your program needs to be able to retry them.


Install HTTPX Retries using pip:

pip install httpx-retries

To get started, add the transport to your client:

import httpx
from httpx_retries import RetryTransport

with httpx.Client(transport=RetryTransport()) as client:
    response = client.get("https://example.com")

Async usage is just as straightforward.

async with httpx.AsyncClient(transport=RetryTransport()) as client:
    response = await client.get("https://example.com")

If you want to use a specific retry strategy, provide a Retry configuration:

from httpx_retries import Retry

retry = Retry(total=5, backoff_factor=0.5)
transport = RetryTransport(retry=retry)

with httpx.Client(transport=transport) as client:
    response = client.get("https://example.com")

Errors while reading the response body (such as ReadTimeout part-way through a download) happen after the transport has returned, so RetryTransport can't retry them. This is a niche case, but if you read very large or slow bodies and need those retried, see the retry_request / aretry_request helpers in Why wasn't my ReadTimeout retried?.

Features

HTTPX Retries builds on the patterns users will expect from urllib and requests. The typical approach has been to use urllib3's Retry utility to configure a retry policy. The equivalent code to match the above example using requests is:

from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

retry = Retry(total=5, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)

with requests.Session() as session:
    session.mount("http://", adapter)
    session.mount("https://", adapter)
    response = session.get("https://example.com")

To reduce boilerplate, this package includes a transport that works with both sync and async HTTPX Clients, with sensible defaults for simple use cases.

HTTPX adds support for asynchronous requests, and this package includes a new retry utility that can handle this behaviour. To make it easy to migrate, the API surface is almost identical to Retry from urllib3, with a few main differences:

  • total is the only parameter used to configure the number of retries.
  • asleep is an async implementation of sleep.
  • backoff_strategy can be overridden to customize backoff behavior.
  • Some options that are not strictly retry-related are not included (raise_on_status, raise_on_redirect)

Differences with other retry libraries

HTTPX Retries tries to make configuring retries simple and centralised, for the case of request / response. Other libraries are general purpose, which is good if you need to have a fully custom retry strategy. Usually though, most programs need a set of sensible defaults, configured once.

Take a look at the comparison with other libraries in the documentation.

Contributing

If you want to contribute to the project, check out the Contributing Guide.

Acknowledgements

This package builds on the great work done on HTTPX, urllib3 and requests.

Metadata

Release files for httpx-retries 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for httpx-retries 0.6.0
File Size Uploaded
httpx_retries-0.6.0.tar.gz 21.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for httpx-retries 0.6.0
File Interpreter ABI Platform
httpx_retries-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 32.9 kB

Release files / httpx_retries-0.6.0.tar.gz

Download URL httpx_retries-0.6.0.tar.gz
Size 21.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3e0b404969a564829d368417964fd21e6b400a10d17c92d29b8bc247ce8186e3
BLAKE2b-256 checksum
How to use checksums
e9d3b7a8bb09543af40009717a08a2ceba90b6d4c6f0cdf171404217d8f4c37d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / httpx_retries-0.6.0-py3-none-any.whl

Download URL httpx_retries-0.6.0-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d1e52a8f68a5df42de75ab89049d5020b2d0ab2f5f8bceacda008d12aa1257a3
BLAKE2b-256 checksum
How to use checksums
56c67f3d6ab3549267a1959161b38df4c0fb435eceaf2d531d8addfac01abaca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.0

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page