{"id":6856,"library":"retry-requests","title":"Retry Requests","description":"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.","status":"active","version":"2.0.0","language":"en","source_language":"en","source_url":"https://github.com/bustawin/retry-requests","tags":["requests","http","retry","network","resilience","backoff"],"install":[{"cmd":"pip install retry-requests","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for making HTTP requests, which this library wraps for retry logic.","package":"requests","optional":false}],"imports":[{"note":"The primary factory function to create a retry-enabled requests Session.","symbol":"retry","correct":"from retry_requests import retry"},{"note":"A convenience Session with a default timeout.","symbol":"TSession","correct":"from retry_requests import TSession"},{"note":"A convenience Session with a timeout that always calls `raise_for_status()`.","symbol":"RSession","correct":"from retry_requests import RSession"}],"quickstart":{"code":"from retry_requests import retry\nfrom requests import Session\n\n# Basic usage with default retries (3 retries, exponential backoff)\nmy_session = retry()\ntry:\n    response = my_session.get(\"https://httpbin.org/status/503\") # Will retry on 503\n    response.raise_for_status()\n    print(f\"Success after retries: {response.status_code}\")\nexcept Exception as e:\n    print(f\"Request failed after all retries: {e}\")\n\n# Customizing retries and backoff\n# Example: 5 retries, backoff factor of 0.2\ncustom_session = retry(Session(), retries=5, backoff_factor=0.2)\ntry:\n    response = custom_session.get(\"https://httpbin.org/status/503\")\n    response.raise_for_status()\n    print(f\"Success after custom retries: {response.status_code}\")\nexcept Exception as e:\n    print(f\"Request failed after custom retries: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Ensure that if you customize retry logic for non-GET methods, the target API endpoint is truly idempotent, or implement your own idempotency keys.","message":"Retrying non-idempotent methods (e.g., POST, PUT, DELETE) by default can lead to unintended side effects if the initial request succeeded on the server but the response was lost. The underlying `urllib3.Retry` (used by requests) typically excludes POST by default, but be mindful when customising `method_whitelist` or similar parameters.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Configuring too many retries or a zero/fixed `backoff_factor` can lead to a 'retry storm' that overloads the target service, exacerbating the problem rather than solving it. This can result in IP bans or further service degradation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"While `retry-requests` provides sensible defaults for retriable HTTP status codes (5XX, 3XX, connection errors), a common mistake is to extend `status_forcelist` to include non-transient errors like 400 Bad Request, 401 Unauthorized, or 404 Not Found. Retrying these errors will not resolve them and wastes resources.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}