xtekky/gpt4free · error · RateLimitError

Response {response.status}: Gateway Timeout

Error message

Response {response.status}: Gateway Timeout 

What it means

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.

Source

Thrown at g4f/requests/raise_for_status.py:79

            message = (await response.text()).strip()
            is_html = content_type.startswith(
                "text/html"
            ) or message.lower().startswith("<!DOCTYPE".lower())
    if message is None or is_html:
        if response.status == 520:
            message = "Unknown error (Cloudflare)"
    if response.status in (429, 402):
        raise RateLimitError(f"Response {response.status}: {message}")
    if response.status == 401:
        raise MissingAuthError(f"Response {response.status}: {message}")
    if response.status == 403 and is_cloudflare(message):
        raise CloudflareError(f"Response {response.status}: Cloudflare detected")
    elif response.status == 403 and (is_openai(message) or is_lmarena(message)):
        raise MissingAuthError(f"Response {response.status}: OpenAI Bot detected")
    elif response.status == 502:
        raise ResponseStatusError(f"Response {response.status}: Bad Gateway")
    elif response.status == 504:
        raise RateLimitError(f"Response {response.status}: Gateway Timeout ")
    elif response.status == 400 and "API key not valid" in message:
        raise MissingAuthError(f"Response {response.status}: Invalid API key")
    else:
        raise ResponseStatusError(
            f"Response {response.status}: {'HTML content' if is_html else message}"
        )


def raise_for_status(
    response: Union[Response, StreamResponse, ClientResponse, RequestsResponse],
    message: str = None,
):
    if hasattr(response, "status"):
        return raise_for_status_async(response, message)
    if response.ok:
        return
    is_html = False
    if message is None:

View on GitHub (pinned to 973504e177)

Solutions

  1. Retry with backoff — treat like rate limiting (g4f deliberately maps 504 to RateLimitError).
  2. Reduce max_tokens / split the generation so it finishes faster.
  3. Switch to a faster or official provider.
  4. Enable streaming if supported, to keep the connection active past gateway timeouts.
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:
    result = await client.chat.completions.async_create(...)
except RateLimitError as e:
    if 'Gateway Timeout' in str(e):
        result = await client.chat.completions.async_create(
            ..., max_tokens=reduced_tokens)  # shrink work and retry
    else:
        await asyncio.sleep(60)  # true 429
        result = await client.chat.completions.async_create(...)

Prevention

When it happens

Trigger: 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.

Common situations: Very long prompts/max_tokens on a slow free provider; provider under heavy load; generating large outputs that exceed the gateway's upstream timeout.

Understand the failure class

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/f96d8ce11c9c27da. Report an issue: GitHub.