{"record":{"id":"1e81e189a153c8ac","repo":"xtekky/gpt4free","slug":"failed-to-obtain-turnstile-token-for-deepinfra-req","errorCode":null,"errorMessage":"Failed to obtain Turnstile token for DeepInfra request.","messagePattern":"Failed to obtain Turnstile token for DeepInfra request\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/DeepInfra.py","lineNumber":222,"sourceCode":"            ]\n            if cls.live == 0 and cls.models:\n                cls.live += 1\n\n        return cls.models\n\n    @classmethod\n    async def create_async_generator(\n        cls, model, messages, api_key=None, headers=None, **kwargs\n    ):\n        if not api_key or not cls.is_provider_api_key(api_key):\n            # Generate a Turnstile token for each request (required without an API key)\n            token = await get_turnstile_token_async(model)\n            if token:\n                if headers is None:\n                    headers = {}\n                headers[\"X-DeepInfra-Turnstile\"] = token\n            else:\n                raise ValueError(\n                    \"Failed to obtain Turnstile token for DeepInfra request.\"\n                )\n\n        async for chunk in super().create_async_generator(\n            model, messages, api_key=api_key, headers=headers, **kwargs\n        ):\n            yield chunk\n\n    @classmethod\n    def get_headers(\n        cls, stream: bool, api_key: str = None, headers: dict = None\n    ) -> dict:\n        if not api_key or not cls.is_provider_api_key(api_key):\n            if headers is None:\n                headers = {}\n            headers[\"X-DeepInfra-Source\"] = \"web-page\"\n            headers[\"Origin\"] = \"https://deepinfra.com\"\n            headers[\"Referer\"] = \"https://deepinfra.com/\"","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/DeepInfra.py#L204-L240","documentation":"Raised by DeepInfra's provider wrapper when no API key is supplied and the helper get_turnstile_token_async(model) fails to solve/obtain a Cloudflare Turnstile token, which DeepInfra requires for anonymous requests. Without either an API key or a Turnstile token, the request cannot be authorized and the provider aborts before sending anything.","triggerScenarios":"Calling DeepInfra without api_key (or with a key that fails is_provider_api_key) while the Turnstile solver returns None — solver timeout, headless browser detection, Cloudflare challenge changes, or the model name not being recognized by the solver.","commonSituations":"Running in CI/containers without a working browser environment for the Turnstile solver; Cloudflare updating its challenge so the bundled solver breaks; missing or misconfigured nodriver/camoufox dependencies; passing an empty-string or malformed api_key.","solutions":["Supply a valid DeepInfra API key (from deepinfra.com) so the Turnstile flow is skipped entirely.","Ensure browser-automation dependencies for the Turnstile solver are installed and a display/headless setup works in your environment.","Update g4f — Turnstile solvers are patched frequently as Cloudflare changes challenges.","Retry after a delay; transient solver failures often resolve, and rotating proxy/IP helps."],"exampleFix":"# before\nresponse = await DeepInfra.create_async_generator(model='Qwen/Qwen2.5-72B-Instruct', messages=msgs)\n\n# after\nresponse = await DeepInfra.create_async_generator(\n    model='Qwen/Qwen2.5-72B-Instruct', messages=msgs,\n    api_key=os.environ['DEEPINFRA_API_KEY'],\n)","handlingStrategy":"validation","validationCode":"from g4f.Provider.DEEPINFRA import DeepInfra\n\ndef has_deepinfra_key(api_key):\n    return bool(api_key) and DeepInfra.is_provider_api_key(api_key)","typeGuard":"def usable_deepinfra_auth(api_key: str | None) -> bool:\n    \"\"\"True when auth will skip the Turnstile solver.\"\"\"\n    return isinstance(api_key, str) and DeepInfra.is_provider_api_key(api_key)","tryCatchPattern":"try:\n    ...await DeepInfra.create_async_generator(...)\nexcept ValueError as e:\n    if 'Turnstile' in str(e):\n        # retry with a real API key or another provider\n        ...","preventionTips":["Set DEEPINFRA_API_KEY to bypass Turnstile entirely","Keep the browser/solver environment working if going anonymous","Update g4f when Cloudflare changes the Turnstile challenge","Treat Turnstile failures as expected noise; retry with backoff"],"tags":["deepinfra","turnstile","cloudflare","auth","captcha","api-key"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}