How Poly Data Handles API Rate Limiting
Poly Data handles API rate limiting by detecting HTTP 429 responses and implementing a fixed 10‑second backoff before retrying, ensuring uninterrupted data downloads from the Polymarket gamma‑API.
Poly Data is a Python library designed to download market data from the Polymarket gamma‑API. Since the API enforces strict request limits, the codebase includes defensive logic to manage throttling automatically. The implementation centers on detecting HTTP 429 status codes and pausing execution with a fixed backoff strategy.
Core Implementation Locations
Main Market Updates in update_markets.py
The primary pagination loop in update_utils/update_markets.py handles rate limiting during bulk market downloads. When the API returns a 429 status code, the function prints a message, sleeps for 10 seconds, and uses continue to restart the while True loop, effectively retrying the same request.
According to the source code in warproxxx/poly_data, this logic appears at lines 78–82.
Token Fetching with update_missing_tokens
The update_missing_tokens function in poly_utils/utils.py implements a more granular retry mechanism. It wraps each token fetch in a retry loop (while retry_count < max_retries) with a maximum of three attempts.
When a 429 response is detected, the function prints a "Rate limited" notice, sleeps 10 seconds, and continues the retry loop. For non-200 responses that are not rate-limit errors, it increments the retry counter, sleeps 2 seconds, and retries. This logic is located at lines 106–110.
Rate Limiting Mechanism Explained
Detection – Both functions inspect response.status_code for the value 429, the standard HTTP status indicating rate limit enforcement.
Back‑off Strategy – A fixed sleep duration of 10 seconds provides the API time to reset its request counters before the client resumes.
Retry Logic – update_missing_tokens limits retries to three attempts to prevent infinite loops, while update_markets relies on its outer pagination loop to keep attempting requests until successful. The use of continue ensures the code skips normal processing (JSON parsing, CSV writing) and reissues the request without breaking the overall workflow.
Practical Code Examples
Fetch markets with built‑in rate‑limit handling:
from update_utils.update_markets import update_markets
# Fetch markets in batches of 500; automatically pauses 10s on 429
update_markets(csv_filename="markets.csv", batch_size=500)
Fetch missing tokens with retry logic:
from poly_utils.utils import update_missing_tokens
missing_tokens = [
"0xabcdef1234567890...",
"0x1234567890abcdef...",
]
# Retries up to 3 times with 10s backoff on rate limits
update_missing_tokens(missing_tokens, csv_filename="missing_markets.csv")
Summary
- Poly Data detects HTTP 429 status codes to identify when the Polymarket API enforces rate limits.
- A fixed 10‑second backoff delay allows the API to reset before subsequent requests.
update_utils/update_markets.pyimplements open‑ended retries via awhile Trueloop withcontinuestatements.poly_utils/utils.pycaps token fetching at three retry attempts with incremental backoff for non‑429 errors.- These mechanisms ensure robust, uninterrupted bulk data downloads without manual intervention.
Frequently Asked Questions
What HTTP status code does Poly Data check for rate limiting?
Poly Data specifically checks for HTTP status code 429 ("Too Many Requests"). This is the standard status code returned by the Polymarket gamma‑API when request limits are exceeded, as implemented in both update_markets.py and utils.py.
How long does Poly Data wait when it hits a rate limit?
The library implements a fixed backoff of 10 seconds whenever a 429 response is received. This pause occurs in both update_markets and update_missing_tokens before the respective retry logic executes, giving the API time to reset its counters.
Does Poly Data have a maximum retry limit?
The update_missing_tokens function limits retries to three attempts using a retry_count variable. However, update_markets in update_utils/update_markets.py does not cap retries, instead relying on its outer while True pagination loop to continue until a successful response is obtained.
Where is the rate limiting logic implemented in Poly Data?
The rate limiting logic is implemented in two locations: update_utils/update_markets.py for bulk market pagination (lines 78–82) and poly_utils/utils.py within the update_missing_tokens function (lines 106–110) for individual token fetching.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →