{"record":{"id":"626b227fbd7586b9","repo":"xtekky/gpt4free","slug":"failed-to-extract-nonce-from-phindai-response","errorCode":null,"errorMessage":"Failed to extract nonce from PhindAi response","messagePattern":"Failed to extract nonce from PhindAi response","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/PhindAi.py","lineNumber":53,"sourceCode":"\n        headers = {\n            \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8\",\n            \"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\",\n        }\n\n        async with StreamSession(\n            headers=headers, impersonate=\"chrome\", proxy=proxy, timeout=120\n        ) as session:\n            # 1. Fetch main page to get nonce\n            async with session.get(cls.url) as response:\n                await raise_for_status(response)\n                html = await response.text()\n\n                match = re.search(r'\"nonce\":\"([a-f0-9]+)\"', html)\n                if not match:\n                    match = re.search(r\"'nonce':'([a-f0-9]+)'\", html)\n                if not match:\n                    raise RuntimeError(\"Failed to extract nonce from PhindAi response\")\n                nonce = match.group(1)\n\n            # 2. Fetch the response\n            ajax_url = f\"{cls.url}/wp-admin/admin-ajax.php\"\n            payload = {\"action\": \"phind_ai_send\", \"nonce\": nonce, \"message\": prompt}\n\n            async with session.post(ajax_url, data=payload) as response:\n                await raise_for_status(response)\n                try:\n                    data = await response.json()\n                except json.JSONDecodeError:\n                    text = await response.text()\n                    raise RuntimeError(\n                        f\"Failed to decode JSON from PhindAi response: {text}\"\n                    )\n\n                if not data.get(\"success\"):\n                    raise RuntimeError(f\"PhindAi API returned success=False: {data}\")","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/PhindAi.py#L35-L71","documentation":"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).","triggerScenarios":"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.","commonSituations":"Datacenter IPs getting challenged; site maintenance; g4f version lagging a markup change; running without chrome impersonation headers so the server serves a different page.","solutions":["Retry after a short delay — challenge/outage pages are often transient.","Use a residential proxy or different egress IP.","Update g4f so the nonce regex matches the current PhindAi markup.","If persistent, the site likely changed its theme/nonce mechanism — check the page manually and patch the regex."],"exampleFix":"# before\nresult = await PhindAi.create_async_generator(model, messages)\n\n# after - tolerate transient nonce failure once\nfor attempt in range(2):\n    try:\n        result = await PhindAi.create_async_generator(model, messages)\n        break\n    except RuntimeError as e:\n        if 'nonce' not in str(e) or attempt == 1:\n            raise\n        await asyncio.sleep(5)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    result = await PhindAi.create_async_generator(model, messages)\nexcept RuntimeError as e:\n    if 'nonce' in str(e):\n        await asyncio.sleep(5)\n        result = await PhindAi.create_async_generator(model, messages)\n    else:\n        raise","preventionTips":["Use chrome-impersonated sessions (provider does this already; keep it)","Route via residential proxy","Retry once after delay for challenge pages","Update g4f when PhindAi markup changes"],"tags":["phind","nonce","scraping","wordpress","anti-csrf"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}