{"record":{"id":"3948b1f29c9c7602","repo":"xtekky/gpt4free","slug":"aliyun-captcha-sdk-failed-to-load","errorCode":null,"errorMessage":"Aliyun captcha SDK failed to load","messagePattern":"Aliyun captcha SDK failed to load","errorType":"exception","errorClass":"TimeoutError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/glm/captcha_solver.py","lineNumber":244,"sourceCode":"        await tab.evaluate(\n            f\"window.AliyunCaptchaConfig = {{region: {CAPTCHA_CONFIG['region']!r}, \"\n            f\"prefix: {CAPTCHA_CONFIG['prefix']!r}}};\",\n            await_promise=False,\n        )\n\n        # Wait for the SDK to expose initAliyunCaptcha.\n        # The SDK is embedded directly in the HTML, so it should be available\n        # immediately after the page loads.\n        waited = 0\n        while True:\n            ready = await tab.evaluate(\n                \"typeof window.initAliyunCaptcha === 'function'\",\n                await_promise=False,\n            )\n            if ready:\n                break\n            if waited >= SDK_LOAD_TIMEOUT_MS:\n                raise TimeoutError(\"Aliyun captcha SDK failed to load\")\n            await asyncio.sleep(0.5)\n            waited += 500\n\n        # Solve the captcha — identical JS to the TS reference.\n        cfg = CAPTCHA_CONFIG\n        solve_js = \"\"\"\n        (async (cfg) => {\n            return new Promise((resolve, reject) => {\n                const timeout = setTimeout(\n                    () => reject(new Error('Captcha solve timeout after ' + cfg.timeout + 'ms')),\n                    cfg.timeout\n                );\n                window.initAliyunCaptcha({\n                    SceneId: cfg.sceneId,\n                    mode: 'popup',\n                    region: cfg.region,\n                    prefix: cfg.prefix,\n                    language: 'en',","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/glm/captcha_solver.py#L226-L262","documentation":"Raised by the GLM (z.ai) Aliyun captcha solver when the bundled AliyunCaptcha SDK does not expose window.initAliyunCaptcha within SDK_LOAD_TIMEOUT_MS (20s) after the headless Chromium page loads. The SDK is embedded directly into the page HTML from AliyunCaptcha.js.txt, so this timeout means the embedded script never executed successfully. It is a TimeoutError raised inside _solve_once(), which the retry wrapper (_solve_with_retry) will retry up to SOLVE_RETRIES (3) times.","triggerScenarios":"Calling any GLM provider flow that invokes get_captcha_verify_param() when: the bundled AliyunCaptcha.js.txt file is missing/corrupt (Path.read_text would fail earlier, but a truncated file yields a script that throws on parse); the headless browser (zendriver/nodriver) failed to execute page JS (e.g. sandbox restrictions, missing Chrome binary flags in restricted containers); the stealth init script or CDP Fetch interception setup broke page initialization; or the tab crashed/navigated away between load and the evaluate() polling loop.","commonSituations":"Running g4f in Docker/CI where Chrome cannot start properly with --no-sandbox under restrictive seccomp profiles; upgrading zendriver/nodriver versions where tab.evaluate semantics change; the bundled SDK copy going stale after Aliyun ships a breaking SDK update; slow machines where first JS execution exceeds 20s.","solutions":["Verify the bundled SDK exists and is valid: check g4f/Provider/glm/AliyunCaptcha.js.txt is non-empty and parses as JavaScript (node --check).","Confirm a working headless Chromium: run a minimal zendriver script that loads a page and evaluates '1+1' to prove JS execution works in your environment.","In containers, ensure Chrome can run: install libnss3/libatk dependencies and allow --no-sandbox (already in LAUNCH_ARGS) under a permissive seccomp profile.","If the SDK genuinely changed, update the bundled AliyunCaptcha.js.txt from the upstream Aliyun captcha SDK and re-test.","As a last resort, raise SDK_LOAD_TIMEOUT_MS in g4f/Provider/glm/captcha_solver.py for slow machines."],"exampleFix":"// before (slow CI machine, 20s not enough)\n// SDK_LOAD_TIMEOUT_MS = 20_000\n\n// after\nSDK_LOAD_TIMEOUT_MS = 60_000","handlingStrategy":"retry","validationCode":"from pathlib import Path\nsdk = Path(__import__('g4f.Provider.glm.captcha_solver', fromlist=['_BUNDLED_SDK_PATH']).__file__).parent / 'AliyunCaptcha.js.txt'\nassert sdk.exists() and sdk.stat().st_size > 10_000, 'Bundled Aliyun captcha SDK missing or truncated'","typeGuard":null,"tryCatchPattern":"try:\n    token = await get_captcha_verify_param()\nexcept TimeoutError as e:\n    if 'SDK failed to load' in str(e):\n        # browser/SDK environment problem — retrying immediately rarely helps\n        raise","preventionTips":["Smoke-test headless Chrome in your deployment environment before relying on GLM.","Keep the bundled AliyunCaptcha.js.txt in sync with the SDK z.ai ships.","Monitor solve latency; creeping load times predict this timeout before it fires."],"tags":["captcha","browser","timeout","glm","headless"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}