{"record":{"id":"f96d8ce11c9c27da","repo":"xtekky/gpt4free","slug":"response-response-status-gateway-timeout","errorCode":null,"errorMessage":"Response {response.status}: Gateway Timeout ","messagePattern":"Response (.+?): Gateway Timeout ","errorType":"exception","errorClass":"RateLimitError","httpStatus":504,"severity":"error","filePath":"g4f/requests/raise_for_status.py","lineNumber":79,"sourceCode":"            message = (await response.text()).strip()\n            is_html = content_type.startswith(\n                \"text/html\"\n            ) or message.lower().startswith(\"<!DOCTYPE\".lower())\n    if message is None or is_html:\n        if response.status == 520:\n            message = \"Unknown error (Cloudflare)\"\n    if response.status in (429, 402):\n        raise RateLimitError(f\"Response {response.status}: {message}\")\n    if response.status == 401:\n        raise MissingAuthError(f\"Response {response.status}: {message}\")\n    if response.status == 403 and is_cloudflare(message):\n        raise CloudflareError(f\"Response {response.status}: Cloudflare detected\")\n    elif response.status == 403 and (is_openai(message) or is_lmarena(message)):\n        raise MissingAuthError(f\"Response {response.status}: OpenAI Bot detected\")\n    elif response.status == 502:\n        raise ResponseStatusError(f\"Response {response.status}: Bad Gateway\")\n    elif response.status == 504:\n        raise RateLimitError(f\"Response {response.status}: Gateway Timeout \")\n    elif response.status == 400 and \"API key not valid\" in message:\n        raise MissingAuthError(f\"Response {response.status}: Invalid API key\")\n    else:\n        raise ResponseStatusError(\n            f\"Response {response.status}: {'HTML content' if is_html else message}\"\n        )\n\n\ndef 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:","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/requests/raise_for_status.py#L61-L97","documentation":"Raised when the upstream answers HTTP 504 Gateway Timeout; g4f classifies it as RateLimitError rather than a generic status error, on the assumption that the gateway timed out because the backend is saturated. Note this is a different condition from 384's 502 even though both are gateway errors.","triggerScenarios":"Long LLM generations where the provider's proxy kills the connection after its timeout; backend queues full so the gateway times out waiting; slow streaming endpoints behind short proxy timeouts.","commonSituations":"Very long prompts/max_tokens on a slow free provider; provider under heavy load; generating large outputs that exceed the gateway's upstream timeout.","solutions":["Retry with backoff — treat like rate limiting (g4f deliberately maps 504 to RateLimitError).","Reduce max_tokens / split the generation so it finishes faster.","Switch to a faster or official provider.","Enable streaming if supported, to keep the connection active past gateway timeouts."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":"from g4f.errors import RateLimitError\ndef is_gateway_timeout(err: BaseException) -> bool:\n    return isinstance(err, RateLimitError) and 'Gateway Timeout' in str(err)","tryCatchPattern":"from g4f.errors import RateLimitError\ntry:\n    result = await client.chat.completions.async_create(...)\nexcept RateLimitError as e:\n    if 'Gateway Timeout' in str(e):\n        result = await client.chat.completions.async_create(\n            ..., max_tokens=reduced_tokens)  # shrink work and retry\n    else:\n        await asyncio.sleep(60)  # true 429\n        result = await client.chat.completions.async_create(...)","preventionTips":["Keep max_tokens modest on slow providers","Stream long generations","Catch RateLimitError and branch on 'Gateway Timeout' vs 429 — the remedies differ","Fail over to a faster provider after repeated 504s"],"tags":["http-504","gateway-timeout","rate-limit","async"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}