python network_error ai_generated true

httpx.HTTPStatusError: Client error '429 Too Many Requests' for url 'https://api.example.com/v1/data'

ID: python/httpx-httpstatuserror-429

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-04-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

The server is rate-limiting the client due to excessive requests in a short time.

generic

中文

由于短时间内请求过多,服务器对客户端进行速率限制。

Workarounds

  1. 90% success Implement exponential backoff and retry logic
    import time
    for attempt in range(5):
        try:
            response = httpx.get('https://api.example.com/v1/data')
            response.raise_for_status()
            break
        except httpx.HTTPStatusError as e:
            if e.response.status_code == 429:
                time.sleep(2 ** attempt)
            else:
                raise
  2. 60% success Add a custom User-Agent header to identify your application
    headers = {'User-Agent': 'MyApp/1.0'}
    httpx.get('https://api.example.com/v1/data', headers=headers)

Dead Ends

Common approaches that don't work:

  1. Retrying immediately with the same request 95% fail

    Retrying without delay will likely hit the same rate limit again.

  2. Ignoring the error and continuing to send requests 80% fail

    The server may temporarily block the client after repeated violations.