{"record":{"id":"3fe74b1259d20e1a","repo":"xtekky/gpt4free","slug":"response-response-status-code-message","errorCode":null,"errorMessage":"Response {response.status_code}: {message}","messagePattern":"Response (.+?): (.+?)","errorType":"exception","errorClass":"RateLimitError","httpStatus":429,"severity":"error","filePath":"g4f/requests/raise_for_status.py","lineNumber":106,"sourceCode":"def raise_for_status(\n    response: Union[Response, StreamResponse, ClientResponse, RequestsResponse],\n    message: str = None,\n):\n    if hasattr(response, \"status\"):\n        return raise_for_status_async(response, message)\n    if response.ok:\n        return\n    is_html = False\n    if message is None:\n        is_html = response.headers.get(\"content-type\", \"\").startswith(\n            \"text/html\"\n        ) or response.text.startswith(\"<!DOCTYPE\")\n        message = response.text\n    if message is None or is_html:\n        if response.status_code == 520:\n            message = \"Unknown error (Cloudflare)\"\n    if response.status_code in (429, 402):\n        raise RateLimitError(f\"Response {response.status_code}: {message}\")\n    if response.status_code == 401:\n        raise MissingAuthError(f\"Response {response.status_code}: {message}\")\n    if response.status_code == 403 and is_cloudflare(response.text):\n        raise CloudflareError(f\"Response {response.status_code}: Cloudflare detected\")\n    elif response.status_code == 403 and is_openai(response.text):\n        raise MissingAuthError(f\"Response {response.status_code}: OpenAI Bot detected\")\n    elif response.status_code == 502:\n        raise ResponseStatusError(f\"Response {response.status_code}: Bad Gateway\")\n    elif response.status_code == 504:\n        raise RateLimitError(f\"Response {response.status_code}: Gateway Timeout \")\n    elif response.status_code == 400 and \"API key not valid\" in message:\n        raise MissingAuthError(f\"Response {response.status_code}: Invalid API key\")\n    else:\n        raise ResponseStatusError(\n            f\"Response {response.status_code}: {'HTML content' if is_html else message}\"\n        )\n","sourceCodeStart":88,"sourceCodeEnd":123,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/requests/raise_for_status.py#L88-L123","documentation":"Sync twin of the async helper: raise_for_status inspects a requests-style Response (no 'status' attribute, has status_code) and raises RateLimitError for HTTP 429 or 402. The raw response text is used as the message without the JSON unwrapping the async path does.","triggerScenarios":"Any synchronous g4f call path using requests whose upstream provider answers 429 (quota exceeded) or 402 (payment required).","commonSituations":"Bursty sync scripts exhausting free-tier quota; provider throttles per-minute requests; endpoint requiring credits.","solutions":["Retry with exponential backoff and jitter.","Add a client-side throttle (sleep between calls / token bucket).","Rotate to another g4f provider.","If 402: the endpoint is paid — fund the account or choose a free provider."],"exampleFix":"from g4f.errors import RateLimitError\n\n# before\nfor prompt in prompts:\n    resp = g4f.ChatCompletion.create(model, messages=[{'role':'user','content':prompt}])\n\n# after\nfor i, prompt in enumerate(prompts):\n    try:\n        resp = g4f.ChatCompletion.create(model, messages=[{'role':'user','content':prompt}])\n    except RateLimitError:\n        time.sleep(min(60, 2 ** i))\n        resp = g4f.ChatCompletion.create(model, messages=[{'role':'user','content':prompt}])","handlingStrategy":"retry","validationCode":null,"typeGuard":"from g4f.errors import RateLimitError\ndef is_rate_limit(err: BaseException) -> bool:\n    return isinstance(err, RateLimitError)","tryCatchPattern":"from g4f.errors import RateLimitError\nimport time\nfor attempt in range(5):\n    try:\n        resp = g4f.ChatCompletion.create(model, messages)\n        break\n    except RateLimitError as e:\n        if 'Response 402' in str(e):\n            raise\n        time.sleep(min(60, 2 ** attempt))","preventionTips":["Throttle sync loops with time.sleep between calls","Cache responses for repeated prompts","Branch on 402 vs 429 in the message","Consider the async client to use semaphores for concurrency control"],"tags":["rate-limit","http-429","sync","g4f"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}