API
RetryTransport
¶
Bases: BaseTransport, AsyncBaseTransport
A transport that automatically retries requests.
with httpx.Client(transport=RetryTransport()) as client:
response = client.get("https://example.com")
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:
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")
By default, the implementation will create a sync and async transport internally, and use whichever is appropriate
for the request. If you want to configure your own transport, provide it to the transport argument:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transport
|
BaseTransport | AsyncBaseTransport | None
|
Optional transport to wrap. If not provided, async and sync transports are created internally. |
None
|
retry
|
Retry | None
|
The retry configuration. |
None
|
Source code in httpx_retries/transport.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
aclose()
async
¶
close()
¶
handle_async_request(request)
async
¶
Sends an HTTP request, possibly with retries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The request to perform. |
required |
Returns:
| Type | Description |
|---|---|
Response
|
The final response. |
Source code in httpx_retries/transport.py
handle_request(request)
¶
Sends an HTTP request, possibly with retries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The request to send. |
required |
Returns:
| Type | Description |
|---|---|
Response
|
The final response. |
Source code in httpx_retries/transport.py
Retry
¶
A class to encapsulate retry logic and configuration.
Each retry attempt will create a new Retry object with updated values, so they can safely be reused.
If backoff_factor is set, it will use an exponential backoff with configurable jitter.
For complex use cases, you can override the backoff_strategy method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total
|
int
|
The maximum number of times to retry a request before giving up. |
10
|
max_backoff_wait
|
float
|
The maximum time in seconds to wait between retries. |
120.0
|
backoff_factor
|
float
|
The factor by which the wait time increases with each retry attempt. |
0.0
|
respect_retry_after_header
|
bool
|
Whether to respect the Retry-After header in HTTP responses when deciding how long to wait before retrying. |
True
|
allowed_methods
|
Iterable[HTTPMethod, str]
|
The HTTP methods that can be retried. Defaults to ["HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE"]. |
None
|
status_forcelist
|
Iterable[HTTPStatus, int]
|
The HTTP status codes that can be retried. Defaults to [429, 502, 503, 504]. |
None
|
retry_on_exceptions
|
Iterable[type[HTTPError]]
|
The HTTP exceptions that can be retried. Defaults to [httpx.TimeoutException, httpx.NetworkError, httpx.RemoteProtocolError]. |
None
|
backoff_jitter
|
float
|
The amount of jitter to add to the backoff time, between 0 and 1. Defaults to 1 (full jitter). |
1.0
|
attempts_made
|
int
|
The number of retry attempts already made. |
0
|
total_timeout
|
float
|
The maximum cumulative time in seconds to spend sleeping between retry
attempts across a single request. Unlike |
None
|
elapsed_sleep
|
float
|
Cumulative sleep time already spent on this request. Preserved across
|
0.0
|
validate_response
|
callable
|
An optional callback called with each response that would
otherwise be returned as a "good" (non-retryable-status) response. If the callback raises, the
request is retried. May be sync or async; an async callback cannot be used with a sync transport.
Signature: |
None
|
Source code in httpx_retries/retry.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
asleep(response)
async
¶
Sleep between retry attempts asynchronously using the calculated duration.
This method will respect a server’s Retry-After response header and sleep the duration
of the time requested. If that is not present, it will use an exponential backoff. By default,
the backoff factor is 0 and this method will return immediately.
Source code in httpx_retries/retry.py
backoff_strategy()
¶
Calculate the backoff time based on the number of attempts.
For complex use cases, you can override this method to implement a custom backoff strategy.
class CustomRetry(Retry):
def backoff_strategy(self) -> float:
if self.attempts_made == 3:
return 1.0
return super().backoff_strategy()
Returns:
| Type | Description |
|---|---|
float
|
The calculated backoff time in seconds, capped by max_backoff_wait. |
Source code in httpx_retries/retry.py
copy_with(total=_UNSET, allowed_methods=_UNSET, status_forcelist=_UNSET, retry_on_exceptions=_UNSET, backoff_factor=_UNSET, respect_retry_after_header=_UNSET, max_backoff_wait=_UNSET, backoff_jitter=_UNSET, attempts_made=_UNSET, total_timeout=_UNSET, elapsed_sleep=_UNSET, validate_response=_UNSET)
¶
Return a new Retry with selected fields overridden.
Source code in httpx_retries/retry.py
increment()
¶
Return a new Retry instance with the attempt count incremented.
Source code in httpx_retries/retry.py
is_exhausted()
¶
Check if the retry attempts have been exhausted.
Source code in httpx_retries/retry.py
is_retry(method, status_code, has_retry_after)
¶
Check if a method and status code are retryable.
This functions identically to urllib3's Retry.is_retry method.
Source code in httpx_retries/retry.py
is_retryable_exception(exception)
¶
is_retryable_method(method)
¶
is_retryable_status_code(status_code)
¶
parse_retry_after(retry_after)
¶
Parse the Retry-After header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retry_after
|
str
|
The Retry-After header value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The number of seconds to wait before retrying. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the Retry-After header is not a valid number or HTTP date. |
Source code in httpx_retries/retry.py
sleep(response)
¶
Sleep between retry attempts using the calculated duration.
This method will respect a server’s Retry-After response header and sleep the duration
of the time requested. If that is not present, it will use an exponential backoff. By default,
the backoff factor is 0 and this method will return immediately.
Source code in httpx_retries/retry.py
retry_request(client, method, url, *, retry=None, **kwargs)
¶
Send a request with retries, including errors raised while reading the response body.
Unlike RetryTransport, which can only observe what flows through its
handle_request method (the response headers), this helper drives the retry loop at the client level.
Because httpx.Client.send reads the body before returning, body-phase errors such as httpx.ReadTimeout
and httpx.RemoteProtocolError("peer closed connection...") are caught here and retried.
import httpx
from httpx_retries import retry_request
with httpx.Client() as client:
response = retry_request(client, "GET", "https://example.com")
The retry configuration can be customised, just like RetryTransport:
response = retry_request(client, "GET", "https://example.com", retry=Retry(total=5, backoff_factor=0.5))
This helper buffers the full response body, so it is not suitable for streaming. Errors raised while
iterating a streaming response (client.stream(...)) cannot be retried.
Body-phase errors are a niche case; see
Why wasn't my ReadTimeout retried? for when these helpers are
worth using and when to prefer RetryTransport instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Client
|
The client used to build and send the request. |
required |
method
|
str
|
The HTTP method. |
required |
url
|
URL | str
|
The URL to request. |
required |
retry
|
Retry | None
|
The retry configuration. A per-request |
None
|
**kwargs
|
Any
|
Additional arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
The final response. |
Source code in httpx_retries/helpers.py
aretry_request(client, method, url, *, retry=None, **kwargs)
async
¶
Send a request asynchronously with retries, including errors raised while reading the response body.
This is the async counterpart to retry_request. Body-phase errors are a niche
case; see Why wasn't my ReadTimeout retried? for when these
helpers are worth using and when to prefer RetryTransport instead.
import httpx
from httpx_retries import aretry_request
async with httpx.AsyncClient() as client:
response = await aretry_request(client, "GET", "https://example.com")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
AsyncClient
|
The client used to build and send the request. |
required |
method
|
str
|
The HTTP method. |
required |
url
|
URL | str
|
The URL to request. |
required |
retry
|
Retry | None
|
The retry configuration. A per-request |
None
|
**kwargs
|
Any
|
Additional arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
The final response. |