xtekky/gpt4free · error · TimeoutError

The operation timed out after {} seconds

Error message

The operation timed out after {} seconds

What it means

Raised by the wait_for helper in base_provider when a single __anext__() on a streaming response exceeds the per-chunk timeout: asyncio.wait_for raises TimeoutError and it is re-raised with a message naming the timeout value. Unlike a total deadline, this is evaluated per chunk, so a long gap between tokens (common during model 'thinking') kills the stream even if overall progress is fine.

Source

Thrown at g4f/providers/base_provider.py:112

        "conversation_id": "550e8400-e29b-11d4-a716-...",
        "message_id": "550e8400-e29b-11d4-a716-...",
    },
    "seed": 42,
    "tools": [],
    "width": 1024,
    "height": 1024,
    "reasoning_effort": "medium",
    "aspect_ratio": "1:1",
}


async def wait_for(response: AsyncIterator, timeout: int = None) -> AsyncIterator:
    if timeout is not None:
        while True:
            try:
                yield await asyncio.wait_for(response.__anext__(), timeout=timeout)
            except TimeoutError as e:
                raise TimeoutError(
                    "The operation timed out after {} seconds".format(timeout)
                ) from e
            except StopAsyncIteration:
                break
    else:
        async for chunk in response:
            yield chunk


def get_async_provider_method(provider: type) -> Optional[callable]:
    if hasattr(provider, "create_async_generator"):
        return provider.create_async_generator
    if hasattr(provider, "create_async"):

        async def wrapper(*args, **kwargs):
            yield await provider.create_async(*args, **kwargs)

        return wrapper

View on GitHub (pinned to 973504e177)

Solutions

  1. Increase the stream timeout for the request (e.g. stream_timeout=120)
  2. Pick a provider/model combination that streams keep-alives or first tokens quickly
  3. Disable the per-stream timeout if the provider's use_stream_timeout permits it
  4. Catch TimeoutError and retry without streaming for long generations

Example fix

# before
response = wait_for(provider_stream, timeout=20)

# after
response = wait_for(provider_stream, timeout=120)
Defensive patterns

Strategy: retry

Validate before calling

# before calling, estimate: reasoning models need much larger per-chunk budgets
stream_timeout = 120 if model_is_reasoning(model) else 30
response = wait_for(gen, timeout=stream_timeout)

Try / catch

try:
    async for chunk in wait_for(gen, timeout=t):
        yield chunk
except TimeoutError as e:
    if "timed out after" in str(e):
        async for chunk in wait_for(gen, timeout=t * 3):
            yield chunk

Prevention

When it happens

Trigger: wait_for(response, timeout=N) where the provider yields no chunk for N consecutive seconds — e.g. reasoning models with long pre-token thinking, or stalled connections. Providers with use_stream_timeout=True get this applied automatically from the request's stream_timeout setting.

Common situations: Default stream timeout too small for o1-style/reasoning models; slow proxies adding latency between chunks; mobile/high-latency networks.

Understand the failure class

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/341e750fb721b7d1. Report an issue: GitHub.