Retry API Reference¶
Auto-generated API documentation for the retry module.
retry
¶
Zero-dependency retry with configurable backoff strategies.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Decorator-based retry with exponential / linear / fixed backoff, jitter, exception and result filtering, and async support.
Basic usage::
@retry(max_retries=3)
def call_api():
return get("https://api.example.com/data")
Async usage::
@retry(max_retries=3, retry_on=(ConnectionError, TimeoutError))
async def call_api():
return await async_get("https://api.example.com/data")
Imperative usage::
result = retry_call(call_api, max_retries=5)
HTTP status filtering::
@retry(retry_on=retry_if_status(429, 502, 503))
def call_api():
resp = get("https://api.example.com/data")
resp.raise_for_status()
return resp
RetryError
¶
Bases: Exception
Raised when all retry attempts are exhausted.
Attributes:
| Name | Type | Description |
|---|---|---|
last_exception |
The exception from the final attempt, or |
|
attempts |
Total number of calls made (initial + retries). |
Source code in retry/retry.py
RetryState
dataclass
¶
Information about the current retry, passed to on_retry callback.
Attributes:
| Name | Type | Description |
|---|---|---|
attempt |
int
|
1-based retry number (1 = first retry, not the initial call). |
exception |
BaseException | None
|
The exception that triggered this retry, or |
result |
Any
|
The return value that triggered this retry, or |
delay |
float
|
Seconds to sleep before the next attempt. |
elapsed |
float
|
Seconds elapsed since the initial call. |
Source code in retry/retry.py
retry_if_exception(*exc_types)
¶
Build a predicate that matches specific exception types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*exc_types
|
type[BaseException]
|
Exception classes to retry on. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[BaseException], bool]
|
A callable |
Source code in retry/retry.py
retry_if_result(predicate)
¶
Mark a callable as a result-retry predicate (identity helper).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[Any], bool]
|
A callable |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Any], bool]
|
The same callable, for self-documenting call sites. |
Source code in retry/retry.py
retry_if_status(*status_codes)
¶
Build a predicate that retries on HTTP status codes.
Works with any exception carrying a status_code attribute
(e.g. httpclient.HTTPError).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*status_codes
|
int
|
HTTP status codes to retry on (e.g. 429, 502, 503). |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[BaseException], bool]
|
A callable |
Source code in retry/retry.py
retry(fn=None, *, max_retries=DEFAULT_MAX_RETRIES, base_delay=DEFAULT_BASE_DELAY, max_delay=DEFAULT_MAX_DELAY, backoff='exponential', backoff_factor=DEFAULT_BACKOFF_FACTOR, jitter='full', retry_on=(Exception,), retry_on_result=None, on_retry=None)
¶
Decorator that retries a function on failure with configurable backoff.
Can be used with or without arguments::
@retry
def f(): ...
@retry()
def g(): ...
@retry(max_retries=5, backoff="linear")
def h(): ...
Automatically detects async functions and uses asyncio.sleep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[..., Any] | None
|
The function to decorate (set automatically when used as
|
None
|
max_retries
|
int
|
Maximum number of retries (not counting the initial call). |
DEFAULT_MAX_RETRIES
|
base_delay
|
float
|
Base delay in seconds before the first retry. |
DEFAULT_BASE_DELAY
|
max_delay
|
float
|
Upper bound on computed delay. |
DEFAULT_MAX_DELAY
|
backoff
|
str
|
Backoff strategy — |
'exponential'
|
backoff_factor
|
float
|
Multiplier for exponential backoff. |
DEFAULT_BACKOFF_FACTOR
|
jitter
|
str
|
Jitter mode — |
'full'
|
retry_on
|
tuple[type[BaseException], ...] | Callable[[BaseException], bool]
|
Exception types or a callable |
(Exception,)
|
retry_on_result
|
Callable[[Any], bool] | None
|
Optional callable |
None
|
on_retry
|
Callable[[RetryState], None] | None
|
Optional callback invoked before each retry sleep with a
:class: |
None
|
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
The decorated function (sync or async, matching the original). |
Raises:
| Type | Description |
|---|---|
RetryError
|
When retries are exhausted due to retry_on_result. |
The original exception
|
When retries are exhausted due to exceptions. |
Source code in retry/retry.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | |
retry_call(fn, args=(), kwargs=None, **retry_kwargs)
¶
Call fn with retry logic without using a decorator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[..., Any]
|
The callable to invoke. |
required |
args
|
tuple[Any, ...]
|
Positional arguments for fn. |
()
|
kwargs
|
dict[str, Any] | None
|
Keyword arguments for fn. |
None
|
**retry_kwargs
|
Any
|
Same keyword arguments accepted by :func: |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The return value of fn on success. |