xtekky/gpt4free · error · ResponseError

{error["code"]}: {error["details"]}

Error message

{error["code"]}: {error["details"]}

What it means

Raised as ResponseError when an SSE chunk inside Qwen's completion stream contains an 'error' object; the message is '{code}: {details}' from the server. Streaming had started (response.created received) but the backend aborted mid-stream with a structured error.

Source

Thrown at g4f/Provider/Qwen.py:681

                                "data", {}
                            ).get("code"):
                                raise RuntimeError(f"Response: {resp_json}")
                            else:
                                # cant stream resp after `resp_json = await resp.json()`, so it stick
                                raise RuntimeError(f"Response: {resp_json}")
                        # args["cookies"] = merge_cookies(args.get("cookies"), resp)
                        thinking_started = False
                        usage = None
                        async for chunk in sse_stream(resp):
                            try:
                                if "response.created" in chunk:
                                    conversation.parent_id = chunk.get(
                                        "response.created", {}
                                    ).get("response_id")
                                    yield conversation
                                error = chunk.get("error", {})
                                if error:
                                    raise ResponseError(
                                        f'{error["code"]}: {error["details"]}'
                                    )
                                usage = chunk.get("usage", usage)
                                choices = chunk.get("choices", [])
                                if not choices:
                                    continue
                                delta = choices[0].get("delta", {})
                                phase = delta.get("phase")
                                content = delta.get("content")
                                status = delta.get("status")
                                extra = delta.get("extra", {})
                                if phase == "think" and not thinking_started:
                                    thinking_started = True
                                elif phase == "answer" and thinking_started:
                                    thinking_started = False
                                elif phase == "image_gen" and status == "typing":
                                    yield ImageResponse(content, prompt, extra)
                                    continue

View on GitHub (pinned to 973504e177)

Solutions

  1. Check code:details — moderation failures need prompt changes; capacity/limit errors need backoff and retry.
  2. Retry with the same conversation (parent_id is preserved per chunk) or a fresh one.
  3. Shorten the prompt / split the task for moderation-triggered errors.
  4. Update g4f for new error-code handling.

Example fix

# before
async for chunk in Qwen.create_async_generator(model, msgs):
    print(chunk)

# after
from g4f.errors import ResponseError
try:
    async for chunk in Qwen.create_async_generator(model, msgs):
        print(chunk)
except ResponseError as e:
    if 'content' in str(e).lower():
        msgs[-1]['content'] = soften(msgs[-1]['content'])
    async for chunk in Qwen.create_async_generator(model, msgs):
        print(chunk)
Defensive patterns

Strategy: try-catch

Type guard

def is_moderation_error(exc: Exception) -> bool:
    from g4f.errors import ResponseError
    return isinstance(exc, ResponseError) and any(
        k in str(exc).lower() for k in ('content', 'sensitive', 'moderation', 'policy')
    )

Try / catch

from g4f.errors import ResponseError
try:
    async for chunk in Qwen.create_async_generator(model, msgs):
        handle(chunk)
except ResponseError as e:
    if 'rate' in str(e).lower():
        await asyncio.sleep(120)
        async for chunk in Qwen.create_async_generator(model, msgs):
            handle(chunk)
    else:  # moderation / policy
        raise

Prevention

When it happens

Trigger: Mid-stream failures: content moderation tripping on the prompt partway through, model overloaded (server-side error chunks), free-tier token-bucket exhausted mid-generation, or image-generation phase failing.

Common situations: Long generations hitting limits near completion; sensitive prompts; peak-hour load on chat.qwen.ai; image models failing on blocked content.

Related errors


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