xtekky/gpt4free · error · ProviderException
Cannot create GLM chat session (status {response.status})
Error message
Cannot create GLM chat session (status {response.status}) What it means
ProviderException raised when POST {GLM_BASE_URL}/api/v1/chats/new returns status >= 400. Every chat completion needs a server-side chat session id; if creation fails (401 expired key, 429 rate limit, 5xx, or payload rejected after an API change) the provider aborts before sending any messages, embedding the HTTP status in the message.
Source
Thrown at g4f/Provider/glm/__init__.py:328
"enable_thinking": "glm-5" in model or "glm-4" in model,
"reasoning_effort": "max" if "glm-5" in model else "",
"auto_web_search": False,
"message_version": 1,
"extra": {},
"timestamp": int(time.time() * 1000),
"type": "default",
}
}
async with session.post(
f"{GLM_BASE_URL}/api/v1/chats/new",
json=chat_body,
headers={
"Authorization": f"Bearer {cls.api_key}",
"Content-Type": "application/json",
},
) as response:
if response.status >= 400:
raise ProviderException(
f"Cannot create GLM chat session (status {response.status})"
)
chat_data = await response.json()
return chat_data.get("id") or chat_data.get("chat", {}).get("id") or chat_id
# ── Captcha ─────────────────────────────────────────────────────────────
@classmethod
async def _get_captcha_verify_param(cls) -> str:
"""Resolve a captcha_verify_param, falling back to an empty string when
the browser-based solver is unavailable (e.g. zendriver not installed)."""
if not captcha_solver_available():
return ""
return await get_captcha_verify_param()
# ── Main entry point (pipeline.ts: proxyViaGlmWebChat) ──────────────────
@classmethodView on GitHub (pinned to 973504e177)
Solutions
- Re-authenticate/refresh the GLM api_key (401 is the most common embedded status)
- Add backoff/retry around chat creation for 429/5xx responses
- Update g4f so the chat_body payload matches the current chats/new schema
Defensive patterns
Strategy: retry
Try / catch
try:
chat_id = await GLM._create_chat(session, chat_body)
except ProviderException as e:
if 'status 429' in str(e) or 'status 5' in str(e):
await asyncio.sleep(backoff())
chat_id = await GLM._create_chat(session, chat_body)
else:
refresh_glm_key() # 401: re-authenticate
raise Prevention
- Reuse chat sessions where the API allows it instead of creating one per request
- Back off exponentially on 429 from chats/new
- Refresh the GLM key on any 401 before retrying the chat flow
When it happens
Trigger: Calling the GLM provider's create_async_generator when the session-creation request fails: invalid/expired api_key, hitting Z.ai rate limits on new-chat creation, or an upstream outage at chat-session creation time.
Common situations: Long-running processes where the key expires mid-session; bursty workloads creating many chats; library version lagging a Z.ai API contract change on the chats/new payload.
Related errors
- Failed to chat: {response.status} {error_text}
- TOKEN_ERROR
- Cannot validate GLM account (status {response.status})
- Failed to decode JSON from PhindAi response: {text}
- PhindAi API returned success=False: {data}
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/bd08903d8be02807.
Report an issue: GitHub.