xtekky/gpt4free · error · RateLimitError
Response {response.status_code}: Gateway Timeout
Error message
Response {response.status_code}: Gateway Timeout What it means
Sync variant: HTTP 504 Gateway Timeout is reclassified as RateLimitError, mirroring the async path. g4f treats a timing-out gateway as saturation, so callers can apply the same backoff logic as for 429.
Source
Thrown at g4f/requests/raise_for_status.py:116
is_html = response.headers.get("content-type", "").startswith(
"text/html"
) or response.text.startswith("<!DOCTYPE")
message = response.text
if message is None or is_html:
if response.status_code == 520:
message = "Unknown error (Cloudflare)"
if response.status_code in (429, 402):
raise RateLimitError(f"Response {response.status_code}: {message}")
if response.status_code == 401:
raise MissingAuthError(f"Response {response.status_code}: {message}")
if response.status_code == 403 and is_cloudflare(response.text):
raise CloudflareError(f"Response {response.status_code}: Cloudflare detected")
elif response.status_code == 403 and is_openai(response.text):
raise MissingAuthError(f"Response {response.status_code}: OpenAI Bot detected")
elif response.status_code == 502:
raise ResponseStatusError(f"Response {response.status_code}: Bad Gateway")
elif response.status_code == 504:
raise RateLimitError(f"Response {response.status_code}: Gateway Timeout ")
elif response.status_code == 400 and "API key not valid" in message:
raise MissingAuthError(f"Response {response.status_code}: Invalid API key")
else:
raise ResponseStatusError(
f"Response {response.status_code}: {'HTML content' if is_html else message}"
)
View on GitHub (pinned to 973504e177)
Solutions
- Retry with backoff (same handling as RateLimitError).
- Lower max_tokens or split the task.
- Enable streaming to keep the connection alive.
- Move to a faster provider.
Defensive patterns
Strategy: retry
Type guard
from g4f.errors import RateLimitError
def is_gateway_timeout(err: BaseException) -> bool:
return isinstance(err, RateLimitError) and 'Gateway Timeout' in str(err) Try / catch
from g4f.errors import RateLimitError
try:
resp = g4f.ChatCompletion.create(model, messages)
except RateLimitError as e:
if 'Gateway Timeout' in str(e):
resp = g4f.ChatCompletion.create(model, messages, max_tokens=512) # smaller, retry
else:
time.sleep(60)
resp = g4f.ChatCompletion.create(model, messages) Prevention
- Distinguish 'Gateway Timeout' from 429 within RateLimitError
- Cap max_tokens on slow providers
- Use streaming for long outputs
- Fail over after repeated timeouts
When it happens
Trigger: Sync g4f calls where long generations exceed the provider gateway's upstream timeout, or the backend queue is saturated.
Common situations: Big max_tokens on slow providers; provider under load; non-streaming requests that idle past proxy timeout.
Understand the failure class
- Timeouts: ETIMEDOUT, deadlines, and hung requests — what actually expires when a request times out.
- HTTP status errors: handling 4xx and 5xx responses — how to handle 4xx and 5xx responses properly.
Related errors
- Response {response.status}: Gateway Timeout
- Response {response.status_code}: {message}
- Failed to chat: {response.status} {error_text}
- Failed to decode JSON from PhindAi response: {text}
- PhindAi API returned success=False: {data}
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/13ea33620763c25a.
Report an issue: GitHub.