xtekky/gpt4free · error · ProviderException

Cannot validate GLM account (status {response.status})

Error message

Cannot validate GLM account (status {response.status})

What it means

ProviderException raised in the GLM provider's account validation helper when GET {GLM_BASE_URL}/api/v1/auths/ returns status >= 400. The provider uses a web-session API key (cls.api_key) and validates the account before chatting; any HTTP error (401 invalid key, 403 blocked, 429 rate limit, 5xx) collapses into this single message with the status embedded.

Source

Thrown at g4f/Provider/glm/__init__.py:274

    # ── Session (session.ts: getCurrentUser, getOrCreateChatSession) ────────

    @classmethod
    def _get_current_user(cls, session) -> dict:
        """Validate JWT and get user info via /api/v1/auths/ (session.ts).

        Returns dict with id, name, email, or raises ProviderException.
        """

        async def _fetch():
            async with session.get(
                f"{GLM_BASE_URL}/api/v1/auths/",
                headers={
                    "Authorization": f"Bearer {cls.api_key}",
                    "Content-Type": "application/json",
                },
            ) as response:
                if response.status >= 400:
                    raise ProviderException(
                        f"Cannot validate GLM account (status {response.status})"
                    )
                data = await response.json()
                user = data.get("user") or data
                if not user or not user.get("id"):
                    raise ProviderException("No user ID in auth response")
                return {
                    "id": str(user["id"]),
                    "name": user.get("name") or user.get("nickname") or "User",
                    "email": user.get("email", ""),
                }

        # Run in the async context — this is called from create_async_generator
        import asyncio

        return asyncio.get_event_loop().run_until_complete(_fetch())

    @classmethod

View on GitHub (pinned to 973504e177)

Solutions

  1. Refresh the GLM/Z.ai API key and re-set cls.api_key before calling the provider
  2. Confirm the status: 401/403 means credential problem, 429 means back off, 5xx means retry later
  3. Verify GLM_BASE_URL matches the endpoint the key was issued for (no proxy/region mismatch)
Defensive patterns

Strategy: try-catch

Validate before calling

import aiohttp

async def glm_key_ok(api_key: str) -> bool:
    async with aiohttp.ClientSession() as s:
        async with s.get(f'{GLM_BASE_URL}/api/v1/auths/',
                         headers={'Authorization': f'Bearer {api_key}'}) as r:
            return r.status < 400

Try / catch

from g4f.errors import ProviderException

try:
    user = await GLM._validate_account(session)
except ProviderException as e:
    if 'status 401' in str(e) or 'status 403' in str(e):
        refresh_glm_key()  # credential problem
    raise

Prevention

When it happens

Trigger: The class-level api_key is expired/wrong (401), the session was rate-limited or the endpoint rejected the request (4xx/5xx) during _validate/_fetch, typically invoked at the start of a chat request or account check.

Common situations: Hardcoded or cached GLM API key that expired; regional blocks or Cloudflare challenges on the Z.ai endpoint; key obtained from a different base URL than GLM_BASE_URL points to.

Related errors


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