xtekky/gpt4free · error · RuntimeError

Failed to extract nonce from PhindAi response

Error message

Failed to extract nonce from PhindAi response

What it means

Raised by PhindAi's WordPress-AJAX flow when neither regex for a nonce ('"nonce":"hex"' nor "'nonce':'hex'") matches the fetched page HTML. The nonce is an anti-CSRF token required by the admin-ajax.php endpoint; its absence means the page returned is not the expected app (a challenge page, error page, or redesigned markup).

Source

Thrown at g4f/Provider/PhindAi.py:53

        headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",
        }

        async with StreamSession(
            headers=headers, impersonate="chrome", proxy=proxy, timeout=120
        ) as session:
            # 1. Fetch main page to get nonce
            async with session.get(cls.url) as response:
                await raise_for_status(response)
                html = await response.text()

                match = re.search(r'"nonce":"([a-f0-9]+)"', html)
                if not match:
                    match = re.search(r"'nonce':'([a-f0-9]+)'", html)
                if not match:
                    raise RuntimeError("Failed to extract nonce from PhindAi response")
                nonce = match.group(1)

            # 2. Fetch the response
            ajax_url = f"{cls.url}/wp-admin/admin-ajax.php"
            payload = {"action": "phind_ai_send", "nonce": nonce, "message": prompt}

            async with session.post(ajax_url, data=payload) as response:
                await raise_for_status(response)
                try:
                    data = await response.json()
                except json.JSONDecodeError:
                    text = await response.text()
                    raise RuntimeError(
                        f"Failed to decode JSON from PhindAi response: {text}"
                    )

                if not data.get("success"):
                    raise RuntimeError(f"PhindAi API returned success=False: {data}")

View on GitHub (pinned to 973504e177)

Solutions

  1. Retry after a short delay — challenge/outage pages are often transient.
  2. Use a residential proxy or different egress IP.
  3. Update g4f so the nonce regex matches the current PhindAi markup.
  4. If persistent, the site likely changed its theme/nonce mechanism — check the page manually and patch the regex.

Example fix

# before
result = await PhindAi.create_async_generator(model, messages)

# after - tolerate transient nonce failure once
for attempt in range(2):
    try:
        result = await PhindAi.create_async_generator(model, messages)
        break
    except RuntimeError as e:
        if 'nonce' not in str(e) or attempt == 1:
            raise
        await asyncio.sleep(5)
Defensive patterns

Strategy: retry

Try / catch

try:
    result = await PhindAi.create_async_generator(model, messages)
except RuntimeError as e:
    if 'nonce' in str(e):
        await asyncio.sleep(5)
        result = await PhindAi.create_async_generator(model, messages)
    else:
        raise

Prevention

When it happens

Trigger: First GET to the PhindAi site returning HTML without an embedded nonce: Cloudflare/bot challenge, site redesign changing the nonce format, temporary outage page, or proxy returning a block page.

Common situations: Datacenter IPs getting challenged; site maintenance; g4f version lagging a markup change; running without chrome impersonation headers so the server serves a different page.

Related errors


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