xtekky/gpt4free · error · RuntimeError

Failed to chat: {response.status} {error_text}

Error message

Failed to chat: {response.status} {error_text}

What it means

Raised by DeepAI's chat generator when the POST to 'https://api.deepai.org/hacking_is_a_serious_crime' returns a non-2xx status. This is the main completion request of the provider; the message includes the HTTP status code and response body text, which typically indicates rate limiting or rejection of the generated api key.

Source

Thrown at g4f/Provider/DeepAI.py:203

                "enabled_tools": json.dumps(
                    ["image_generator", "image_editor"], separators=(",", ":")
                ),
            }

            if attachment_uuids:
                data_dict["attachment_uuids"] = json.dumps(attachment_uuids)

            data = FormData(data_dict)

            async with session.post(
                "https://api.deepai.org/hacking_is_a_serious_crime",
                headers=headers,
                data=data,
                proxy=proxy,
            ) as response:
                if not response.ok:
                    error_text = await response.text()
                    raise RuntimeError(
                        f"Failed to chat: {response.status} {error_text}"
                    )

                buffer = ""
                async for chunk in response.content.iter_any():
                    if chunk:
                        chunk_text = chunk.decode(errors="ignore")
                        if "\x1c" in chunk_text or "\x1c" in buffer:
                            buffer += chunk_text
                        else:
                            yield chunk_text

                if not buffer:
                    return

                parts = buffer.split("\x1c")
                if parts[0].strip():
                    yield parts[0]

View on GitHub (pinned to 973504e177)

Solutions

  1. Check the status code in the message: 429 means back off and retry later; 401/403 means the generated api key/headers were rejected.
  2. Reduce request frequency or route through a different proxy/IP.
  3. Update g4f — DeepAI's key generation recipe (generate_api_key) changes often and is fixed upstream.
  4. As a fallback, switch to another image-capable provider in g4f while DeepAI is blocked.

Example fix

# before
for i in range(50):
    out = await DeepAI.create_async_generator(model, messages)

# after
for i in range(50):
    try:
        out = await DeepAI.create_async_generator(model, messages)
    except RuntimeError as e:
        if ' 429 ' in str(e):
            await asyncio.sleep(30)
            continue
        raise
Defensive patterns

Strategy: retry

Try / catch

try:
    async for chunk in DeepAI.create_async_generator(model, messages):
        ...
except RuntimeError as e:
    msg = str(e)
    if ' 429 ' in msg:
        await asyncio.sleep(30)  # backoff, then retry
    else:
        raise  # 401/403: generated api key rejected -> update g4f or switch provider

Prevention

When it happens

Trigger: Calling DeepAI chat (text or after attachment upload) when the endpoint responds 4xx/5xx — commonly 429 rate limit per api key/IP, or 401/403 when the locally generated api_key header is deemed invalid.

Common situations: Bursty traffic from one IP generating many random api keys; DeepAI tightening validation of the hacked api key scheme (provider breaks frequently); proxy or impersonation issues causing WAF blocks; outdated g4f with a stale key recipe.

Related errors


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