xtekky/gpt4free · error · RuntimeError

Failed to create chat: {data}

Error message

Failed to create chat: {data}

What it means

Raised when Qwen's POST to /api/v2/chats/new returns JSON lacking success=true plus data.id — the chat container could not be created on chat.qwen.ai. The full payload is embedded; typical causes are expired/flagged cookies, WAF-adjacent soft failures, or upstream API contract changes.

Source

Thrown at g4f/Provider/Qwen.py:586

                    if conversation is None:
                        chat_payload = {
                            "title": "New Chat",
                            "models": [model_name],
                            "chat_mode": "normal",
                            "chat_type": chat_type,
                            "timestamp": now,
                            "project_id": "",
                        }
                        async with session.post(
                            f"{cls.url}/api/v2/chats/new",
                            json=chat_payload,
                            headers=req_headers,
                            proxy=proxy,
                        ) as resp:
                            await cls.raise_for_status(resp)
                            data = await resp.json()
                            if not (data.get("success") and data["data"].get("id")):
                                raise RuntimeError(f"Failed to create chat: {data}")
                        conversation = JsonConversation(
                            chat_id=data["data"]["id"],
                            cookies={key: value for key, value in resp.cookies.items()},
                            parent_id=None,
                        )
                    files = []
                    media = list(merge_media(media, messages))
                    if media:
                        files = await cls.prepare_files(
                            media, session=session, headers=req_headers
                        )

                    feature_config = (
                        {
                            "auto_thinking": auto_thinking,
                            "thinking_mode": thinking_mode,
                            # "thinking_format": "summary",
                            "thinking_enabled": enable_thinking,

View on GitHub (pinned to 973504e177)

Solutions

  1. Inspect the embedded data payload for the upstream refusal reason.
  2. Retry — the outer retry loop treats some failures as rate limits; if re-raised, restart the process to regenerate cookies.
  3. Update g4f for the current /api/v2/chats/new contract.
  4. Reduce parallel conversations from the same identity.

Example fix

# before - many concurrent conversations
await asyncio.gather(*[Qwen.create_async_generator(model, m) for m in many_msgs])

# after - serial, retry once
for m in many_msgs:
    try:
        await Qwen.create_async_generator(model, m)
    except RuntimeError:
        await asyncio.sleep(5)
        await Qwen.create_async_generator(model, m)
Defensive patterns

Strategy: try-catch

Try / catch

try:
    r = await Qwen.create_async_generator(model, msgs)
except RuntimeError as e:
    if 'Failed to create chat' in str(e):
        await asyncio.sleep(5)
        r = await Qwen.create_async_generator(model, msgs)  # new chat + new cookies
    else:
        raise

Prevention

When it happens

Trigger: First message in a conversation (no existing conversation object) when the new-chat API refuses: invalid anonymous cookies, session flagged after the outer loop reset conversation=None, or the response shape changed (id moved).

Common situations: Repeated runs with generated cookie jars that get flagged; Qwen A/B-changing its v2 API; race between nodriver cookie harvest and API use; rate pressure causing soft failures without 429.

Related errors


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