{"record":{"id":"2f2fabf9b02fa345","repo":"xtekky/gpt4free","slug":"failed-to-decode-json-from-phindai-response-text","errorCode":null,"errorMessage":"Failed to decode JSON from PhindAi response: {text}","messagePattern":"Failed to decode JSON from PhindAi response: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/PhindAi.py","lineNumber":66,"sourceCode":"\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}\")\n\n                response_text = data.get(\"data\", {}).get(\"response\", \"\")\n                return response_text\n","sourceCodeStart":48,"sourceCodeEnd":75,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/PhindAi.py#L48-L75","documentation":"Raised when the POST to PhindAi's admin-ajax.php returns a body that is not valid JSON (json.JSONDecodeError on response.json()). The raw text is embedded in the message, which usually exposes an HTML error/challenge page, a PHP fatal-error notice, or an empty body instead of the expected JSON envelope.","triggerScenarios":"The ajax request returning HTML: invalid/expired nonce triggering a WP error page, rate-limit page, Cloudflare block, or server 500 with HTML body (raise_for_status already passed for 2xx only, so this is a 200-with-HTML or similar case the status check missed).","commonSituations":"Nonce expired between page fetch and ajax POST; aggressive request volume hitting WP rate limits; shared-hosting errors from phindafenough.fun; g4f out of sync with endpoint changes.","solutions":["Inspect the embedded text — HTML title/Cloudflare markers tell you whether it is a block, an error page, or empty.","Back off and retry to get a fresh nonce on a new session.","Route through a different IP/proxy.","Update g4f for endpoint/response changes."],"exampleFix":"# before - hammering the endpoint in a tight loop\nfor msg in many_messages:\n    r = await PhindAi.create_async_generator(model, [msg])\n\n# after - fresh session + delay between calls\nasync with asyncio.Semaphore(1):\n    for msg in many_messages:\n        r = await PhindAi.create_async_generator(model, [msg])\n        await asyncio.sleep(2)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    result = await PhindAi.create_async_generator(model, messages)\nexcept RuntimeError as e:\n    if 'Failed to decode JSON' in str(e):\n        if 'cloudflare' in str(e).lower():\n            raise  # blocked: change IP\n        await asyncio.sleep(5)\n        result = await PhindAi.create_async_generator(model, messages)\n    else:\n        raise","preventionTips":["One prompt per fresh session to keep nonces valid","Pace requests to avoid WP rate limits","Check embedded text for challenge vs error page","Update g4f for endpoint changes"],"tags":["phind","json-decode","scraping","rate-limit","upstream-api"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}