xtekky/gpt4free · error · ValueError
Failed to obtain Turnstile token for DeepInfra request.
Error message
Failed to obtain Turnstile token for DeepInfra request.
What it means
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.
Source
Thrown at g4f/Provider/DeepInfra.py:222
]
if cls.live == 0 and cls.models:
cls.live += 1
return cls.models
@classmethod
async def create_async_generator(
cls, model, messages, api_key=None, headers=None, **kwargs
):
if not api_key or not cls.is_provider_api_key(api_key):
# Generate a Turnstile token for each request (required without an API key)
token = await get_turnstile_token_async(model)
if token:
if headers is None:
headers = {}
headers["X-DeepInfra-Turnstile"] = token
else:
raise ValueError(
"Failed to obtain Turnstile token for DeepInfra request."
)
async for chunk in super().create_async_generator(
model, messages, api_key=api_key, headers=headers, **kwargs
):
yield chunk
@classmethod
def get_headers(
cls, stream: bool, api_key: str = None, headers: dict = None
) -> dict:
if not api_key or not cls.is_provider_api_key(api_key):
if headers is None:
headers = {}
headers["X-DeepInfra-Source"] = "web-page"
headers["Origin"] = "https://deepinfra.com"
headers["Referer"] = "https://deepinfra.com/"View on GitHub (pinned to 973504e177)
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.
Example fix
# before
response = await DeepInfra.create_async_generator(model='Qwen/Qwen2.5-72B-Instruct', messages=msgs)
# after
response = await DeepInfra.create_async_generator(
model='Qwen/Qwen2.5-72B-Instruct', messages=msgs,
api_key=os.environ['DEEPINFRA_API_KEY'],
) Defensive patterns
Strategy: validation
Validate before calling
from g4f.Provider.DEEPINFRA import DeepInfra
def has_deepinfra_key(api_key):
return bool(api_key) and DeepInfra.is_provider_api_key(api_key) Type guard
def usable_deepinfra_auth(api_key: str | None) -> bool:
"""True when auth will skip the Turnstile solver."""
return isinstance(api_key, str) and DeepInfra.is_provider_api_key(api_key) Try / catch
try:
...await DeepInfra.create_async_generator(...)
except ValueError as e:
if 'Turnstile' in str(e):
# retry with a real API key or another provider
... Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Add a "api_key"
- Missing "api_key"
- Invalid secrets
- Response status: {response.status}
- Error processing token {token}: {exc}
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/1e81e189a153c8ac.
Report an issue: GitHub.