xtekky/gpt4free · error · TimeoutError
Aliyun captcha SDK failed to load
Error message
Aliyun captcha SDK failed to load
What it means
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.
Source
Thrown at g4f/Provider/glm/captcha_solver.py:244
await tab.evaluate(
f"window.AliyunCaptchaConfig = {{region: {CAPTCHA_CONFIG['region']!r}, "
f"prefix: {CAPTCHA_CONFIG['prefix']!r}}};",
await_promise=False,
)
# Wait for the SDK to expose initAliyunCaptcha.
# The SDK is embedded directly in the HTML, so it should be available
# immediately after the page loads.
waited = 0
while True:
ready = await tab.evaluate(
"typeof window.initAliyunCaptcha === 'function'",
await_promise=False,
)
if ready:
break
if waited >= SDK_LOAD_TIMEOUT_MS:
raise TimeoutError("Aliyun captcha SDK failed to load")
await asyncio.sleep(0.5)
waited += 500
# Solve the captcha — identical JS to the TS reference.
cfg = CAPTCHA_CONFIG
solve_js = """
(async (cfg) => {
return new Promise((resolve, reject) => {
const timeout = setTimeout(
() => reject(new Error('Captcha solve timeout after ' + cfg.timeout + 'ms')),
cfg.timeout
);
window.initAliyunCaptcha({
SceneId: cfg.sceneId,
mode: 'popup',
region: cfg.region,
prefix: cfg.prefix,
language: 'en',View on GitHub (pinned to 973504e177)
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.
Example fix
// before (slow CI machine, 20s not enough) // SDK_LOAD_TIMEOUT_MS = 20_000 // after SDK_LOAD_TIMEOUT_MS = 60_000
Defensive patterns
Strategy: retry
Validate before calling
from pathlib import Path
sdk = Path(__import__('g4f.Provider.glm.captcha_solver', fromlist=['_BUNDLED_SDK_PATH']).__file__).parent / 'AliyunCaptcha.js.txt'
assert sdk.exists() and sdk.stat().st_size > 10_000, 'Bundled Aliyun captcha SDK missing or truncated' Try / catch
try:
token = await get_captcha_verify_param()
except TimeoutError as e:
if 'SDK failed to load' in str(e):
# browser/SDK environment problem — retrying immediately rarely helps
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Captcha solver returned an invalid token: {param!r}
- GLM captcha solving failed after {SOLVE_RETRIES} attempts: {
- {captcha}
- Failed to obtain Turnstile token for DeepInfra request.
- Upload timeout for {file_id}
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/3948b1f29c9c7602.
Report an issue: GitHub.