xtekky/gpt4free · error · ResponseStatusError
Response {response.status}: Bad Gateway
Error message
Response {response.status}: Bad Gateway What it means
Raised when the upstream provider returns HTTP 502 Bad Gateway during g4f's async check. The provider's gateway/proxy failed to get a valid response from its backend; g4f maps it to a generic ResponseStatusError, distinct from rate-limit or auth failures.
Source
Thrown at g4f/requests/raise_for_status.py:77
message = await response.text()
else:
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:
returnView on GitHub (pinned to 973504e177)
Solutions
- Retry after a short delay — 502 is usually transient.
- Retry via a different provider in g4f.
- Check the provider's status page for outages.
- If persistent for one provider only, that provider's backend is down; wait or switch.
Defensive patterns
Strategy: retry
Type guard
from g4f.errors import ResponseStatusError, CloudflareError, RateLimitError
def is_bad_gateway(err: BaseException) -> bool:
return isinstance(err, ResponseStatusError) and not isinstance(err, (CloudflareError, RateLimitError)) and 'Bad Gateway' in str(err) Try / catch
from g4f.errors import ResponseStatusError
for attempt in range(3):
try:
return await call_provider()
except ResponseStatusError as e:
if 'Bad Gateway' not in str(e):
raise
await asyncio.sleep(2 ** attempt) Prevention
- Wrap provider calls in bounded retry for 5xx
- Keep a second provider configured for failover
- Log status codes per provider to spot chronic outages
- Do not resend identical failing payloads more than a couple of times
When it happens
Trigger: Any async g4f call where the provider's reverse proxy (nginx, Cloudflare, etc.) answers 502 — backend down, crashing worker, or bad upstream routing.
Common situations: Provider outage or maintenance; overloaded free-tier backends; transient proxy hiccup that clears in seconds.
Understand the failure class
- HTTP status errors: handling 4xx and 5xx responses — how to handle 4xx and 5xx responses properly.
Related errors
- Response {response.status_code}: Bad Gateway
- LMArena Beta encountered an error: hasArenaError
- Response {response.status}: {message}
- Response {response.status}: Cloudflare detected
- Response {response.status}: OpenAI Bot detected
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/9a1079248af53d7f.
Report an issue: GitHub.