{"record":{"id":"b2a2c5897f26db68","repo":"xtekky/gpt4free","slug":"failed-to-extract-bx-umidtoken","errorCode":null,"errorMessage":"Failed to extract bx-umidtoken.","messagePattern":"Failed to extract bx-umidtoken\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/Qwen.py","lineNumber":472,"sourceCode":"            # Fix 'FAIL_SYS_USER_VALIDATE'\r\n            \"X-Accel-Buffering\": \"no\",\r\n        }\r\n        if token:\r\n            headers[\"Authorization\"] = f\"Bearer {token}\"\r\n        return headers\r\n\r\n    @classmethod\r\n    async def _get_req_headers(cls, session, proxy=None):\r\n        if not cls._midtoken:\r\n            debug.log(\"[Qwen] INFO: No active midtoken. Fetching a new one...\")\r\n            async with session.get(\r\n                \"https://sg-wum.alibaba.com/w/wu.json\", proxy=proxy\r\n            ) as r:\r\n                r.raise_for_status()\r\n                text = await r.text()\r\n                match = re.search(r\"(?:umx\\.wu|__fycb)\\('([^']+)'\\)\", text)\r\n                if not match:\r\n                    raise RuntimeError(\"Failed to extract bx-umidtoken.\")\r\n                cls._midtoken = match.group(1)\r\n                cls._midtoken_uses = 1\r\n                debug.log(\r\n                    f\"[Qwen] INFO: New midtoken obtained. Use count: {cls._midtoken_uses}. Midtoken: {cls._midtoken}\"\r\n                )\r\n        else:\r\n            cls._midtoken_uses += 1\r\n            debug.log(f\"[Qwen] INFO: Reusing midtoken. Use count: {cls._midtoken_uses}\")\r\n\r\n        req_headers = session.headers.copy()\r\n        req_headers[\"bx-umidtoken\"] = cls._midtoken\r\n        req_headers[\"bx-v\"] = \"2.5.36\"\r\n        # fix error [g4f.errors.CloudflareError:aliyun_waf_aa]\r\n        req_headers[\"x-request-id\"] = str(uuid.uuid4())\r\n        return req_headers\r\n\r\n    @classmethod\r\n    async def get_quota(cls, api_key: Optional[str] = None, **kwargs) -> dict:\r","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/Qwen.py#L454-L490","documentation":"Raised by Qwen._get_req_headers when the fetched wu.json from sg-wum.alibaba.com does not contain a umid token matching either regex (umx.wu('...') or __fycb('...')). The bx-umidtoken is Alibaba's device-fingerprint token required in request headers; a non-matching response means Alibaba changed the script format or served an error/obfuscated payload.","triggerScenarios":"First request after startup (or after midtoken invalidation) when the wu.js/wu.json device script no longer contains the expected call pattern: format change, geo-varied payload, or proxy mangling the JS response.","commonSituations":"g4f version older than an Alibaba script rotation; corporate proxies rewriting JS; region-specific wu.json variants; transient CDN errors returning error bodies with status 200.","solutions":["Retry the request — transient CDN/format glitches often resolve and the token fetch is retried.","Update g4f to pick up new extraction regexes after Alibaba changes.","Bypass JS-rewriting proxies when fetching alibaba domains.","If persistent, inspect wu.json manually and patch the regex in _get_req_headers."],"exampleFix":"# before\nr = await Qwen.create_async_generator(model, msgs)\n\n# after\nfor attempt in range(2):\n    try:\n        r = await Qwen.create_async_generator(model, msgs)\n        break\n    except RuntimeError as e:\n        if 'bx-umidtoken' not in str(e) or attempt == 1:\n            raise\n        await asyncio.sleep(5)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    r = await Qwen.create_async_generator(model, msgs)\nexcept RuntimeError as e:\n    if 'bx-umidtoken' in str(e):\n        await asyncio.sleep(5)\n        r = await Qwen.create_async_generator(model, msgs)  # refetches wu.json\n    else:\n        raise","preventionTips":["Update g4f promptly — umid script format rotates","Avoid proxies that rewrite JS bodies","Retry once on token extraction failure before escalating","File an upstream issue if it persists; regex needs updating"],"tags":["qwen","umidtoken","device-fingerprint","anti-bot","scraping"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}