retry-requests is a Python library that enhances `requests.Session` objects to automatically retry failed HTTP requests. It handles transient issues like connection errors, timeouts, and specific HTTP response codes (5XX and 3XX by default) with exponential backoff. Currently at version 2.0.0, the library is actively maintained, with releases as needed based on contributions and bug fixes.
pip install retry-requestsVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to create a retry-enabled requests Session using the `retry` function, both with default settings and with custom retry attempts and backoff factor. It shows how the session automatically retries on specified HTTP status codes (like 503) or connection errors.
Ensure that if you customize retry logic for non-GET methods, the target API endpoint is truly idempotent, or implement your own idempotency keys.
Use a reasonable `retries` count (e.g., 3-5) and always apply an exponential `backoff_factor` to avoid hammering the server. Default values often provide a good starting point.
Only retry on transient error codes (e.g., 429, 500, 502, 503, 504) and connection issues. Handle client-side errors (4XX) by checking your request parameters or authentication before retrying.
First, install the library using pip: `pip install retry-requests`. Then, ensure the import statement is correct: `from retry_requests import retry`.
Pass an instantiated `requests.Session` object to the `retry` function: `import requests; from retry_requests import retry; session = retry(requests.Session())`.
Use the correct parameter name `retries` when configuring the retry behavior: `session = retry(requests.Session(), retries=5)`.
Use the correct import statement to get the `retry` function from the `retry-requests` package: `from retry_requests import retry`.