{"record":{"id":"c1407be44a7e896d","repo":"xtekky/gpt4free","slug":"response-response-status-code-html-content-i","errorCode":null,"errorMessage":"Response {response.status_code}: {'HTML content' if is_html else message}","messagePattern":"Response (.+?): (.+?)","errorType":"exception","errorClass":"ResponseStatusError","httpStatus":null,"severity":"error","filePath":"g4f/requests/raise_for_status.py","lineNumber":120,"sourceCode":"    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":102,"sourceCodeEnd":123,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/requests/raise_for_status.py#L102-L123","documentation":"Sync fallback branch: every non-ok status not matched by the specific branches (429/402, 401, fingerprinted 403, 502, 504, 400-bad-key) raises ResponseStatusError, substituting 'HTML content' when the body is HTML (content-type text/html or body starts with <!DOCTYPE).","triggerScenarios":"Sync g4f calls returning 400 (validation), 404 (moved/removed endpoint), 405, 500, 503, or any HTML error page without a known fingerprint.","commonSituations":"Provider API changed shape (404/400) after an update; upstream 500/503 during incidents; stale g4f version targeting dead endpoints.","solutions":["Parse the status code from the message to find the real cause.","Update g4f to the latest release for current provider endpoints.","For 5xx, retry later or switch provider.","For 400, check model name and message payload against the provider API."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"from g4f.errors import ResponseStatusError, CloudflareError, RateLimitError\ndef is_generic_status(err: BaseException) -> bool:\n    return isinstance(err, ResponseStatusError) and not isinstance(err, (CloudflareError, RateLimitError))","tryCatchPattern":"from g4f.errors import ResponseStatusError\ntry:\n    resp = g4f.ChatCompletion.create(model, messages)\nexcept ResponseStatusError as e:\n    status = int(str(e).split(':')[0].split()[-1])\n    if status >= 500:\n        time.sleep(5); resp = g4f.ChatCompletion.create(model, messages)\n    else:\n        raise","preventionTips":["Extract the status code from the message to choose handling","Update g4f when providers change endpoints (404)","Validate model names and message shapes (400)","Retry 5xx only, and only a bounded number of times"],"tags":["http-status","fallback","provider-error","sync"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}