xtekky/gpt4free · error · TimeoutError

The operation timed out after {} seconds in {}

Error message

The operation timed out after {} seconds in {}

What it means

TimeoutError raised in AsyncAuthedProvider.create_async_generator while streaming an authed response: the caller passed 'stream_timeout' (or 'timeout') in kwargs, and asyncio.wait_for on response.__anext__() exceeded that limit between chunks. The message reports the configured seconds and the provider class name. It is a per-chunk timeout, not a total-request timeout.

Source

Thrown at g4f/providers/base_provider.py:498

        cache_file = cls.get_cache_file()
        try:
            auth_result = cls.get_auth_result()
            response = to_async_iterator(
                cls.create_authed(model, messages, **kwargs, auth_result=auth_result)
            )
            if "stream_timeout" in kwargs or "timeout" in kwargs:
                timeout = (
                    kwargs.get("stream_timeout")
                    if cls.use_stream_timeout
                    else kwargs.get("timeout")
                )
                while True:
                    try:
                        yield await asyncio.wait_for(
                            response.__anext__(), timeout=timeout
                        )
                    except TimeoutError as e:
                        raise TimeoutError(
                            "The operation timed out after {} seconds in {}".format(
                                timeout, cls.__name__
                            )
                        ) from e
                    except StopAsyncIteration:
                        break
            else:
                async for chunk in response:
                    yield chunk
        except (MissingAuthError, NoValidHarFileError, CloudflareError):
            # if cache_file.exists():
            #     cache_file.unlink()
            response = cls.on_auth_async(**kwargs)
            async for chunk in response:
                if isinstance(chunk, AuthResult):
                    auth_result = chunk
                else:
                    yield chunk

View on GitHub (pinned to 973504e177)

Solutions

  1. Raise the stream_timeout/timeout value, especially for slow or reasoning models (60–180s is common).
  2. Retry the request — a mid-stream stall is often transient; the same call usually succeeds.
  3. Distinguish the two kwargs: 'stream_timeout' applies only when cls.use_stream_timeout is truthy, otherwise 'timeout' is used.
  4. If stalls are persistent, switch providers or check network egress to the provider host.

Example fix

// before
chunks = provider.create_async_generator(model, messages, stream_timeout=10)

// after
chunks = provider.create_async_generator(model, messages, stream_timeout=120)
Defensive patterns

Strategy: retry

Validate before calling

timeout = kwargs.get('stream_timeout', kwargs.get('timeout'))
if timeout is not None and timeout < 30:
    kwargs['stream_timeout'] = max(timeout, 30)  # avoid guaranteed timeouts on slow models

Try / catch

try:
    chunks = [c async for c in provider.create_async_generator(model, messages, stream_timeout=120)]
except TimeoutError as e:
    if provider.__name__ in str(e):  # our stream timeout, not a generic one
        chunks = [c async for c in provider.create_async_generator(model, messages, stream_timeout=300)]
    else:
        raise

Prevention

When it happens

Trigger: Calling an authed provider with stream_timeout=30 (or timeout=30 when the provider sets use_stream_timeout=False) while the upstream stalls mid-stream — slow model, network hiccup, or upstream queue — so no chunk arrives within the window.

Common situations: Long generations with aggressively small timeouts; mobile/unstable networks where inter-chunk gaps exceed the limit; upstream providers that pause for tens of seconds before the first token (reasoning models); kwargs confusion between 'timeout' and 'stream_timeout'.

Understand the failure class

Related errors


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