python network_error ai_generated true

aiohttp.client_exceptions.ClientResponseError: 429, message='Too Many Requests', url=URL('https://api.example.com/v1/items')

ID: python/aiohttp-client-response-error

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active — — —
3.9 active — — —
3.10 active — — —
3.11 active — — —
3.12 active — — —

Root Cause

The remote server returned a 4xx/5xx status and the session/request was configured with raise_for_status=True, so aiohttp raised ClientResponseError instead of returning the response.

generic

中文

远程服务器返回了 4xx/5xx 状态码,且会话/请求配置了 raise_for_status=True,因此 aiohttp 抛出 ClientResponseError 而非返回响应。

Workarounds

  1. 88% success
    import asyncio, random
    for attempt in range(5):
        async with session.get(url) as r:
            if r.status == 429:
                await asyncio.sleep((2 ** attempt) + random.random())
                continue
            r.raise_for_status()
            return await r.json()
  2. 90% success
    sem = asyncio.Semaphore(5)
    async def fetch(u):
        async with sem:
            async with session.get(u) as r:
                r.raise_for_status()
                return await r.json()

Dead Ends

Common approaches that don't work:

  1. 75% fail

    Hides 5xx and auth failures; downstream code receives HTML error pages parsed as JSON.

  2. 85% fail

    Aggravates rate limiting and often escalates to a 403 block.