xtekky/gpt4free · error · TimeoutError

The operation timed out after {} seconds

Error message

The operation timed out after {} seconds

What it means

Raised by await_callback when the awaited callable exceeds the given timeout: asyncio.wait_for raises TimeoutError, which is re-raised with a uniform message including the configured limit. This helper exists partly to avoid the 'async generator ignored GeneratorExit' RuntimeError on abandoned coroutines.

Source

Thrown at g4f/providers/asyncio.py:51

            elif check_nested:
                raise NestAsyncioError(
                    'Install "nest-asyncio2" package | pip install -U nest-asyncio2'
                )
        return loop
    except RuntimeError:
        pass


# Fix for RuntimeError: async generator ignored GeneratorExit
async def await_callback(callback: Callable, timeout: Optional[int] = None) -> any:
    try:
        return (
            await asyncio.wait_for(callback(), timeout)
            if timeout is not None
            else await callback()
        )
    except TimeoutError as e:
        raise TimeoutError(
            "The operation timed out after {} seconds".format(timeout)
        ) from e


async def async_generator_to_list(generator: AsyncIterator) -> list:
    return [item async for item in generator]


def to_sync_generator(
    generator: AsyncIterator, stream: bool = True, timeout: int = None
) -> Iterator:
    loop = get_running_loop(check_nested=False)
    if asyncio.iscoroutine(generator):
        if loop is not None:
            try:
                result = loop.run_until_complete(generator)
            except RuntimeError as e:
                if asyncio.iscoroutine(generator):

View on GitHub (pinned to 973504e177)

Solutions

  1. Increase the timeout argument passed to await_callback
  2. Fix the underlying slowness: pass a per-request timeout to the HTTP client used inside callback
  3. Catch TimeoutError at the call site and retry with backoff for idempotent requests

Example fix

# before
result = await await_callback(fetch, timeout=5)

# after
try:
    result = await await_callback(fetch, timeout=30)
except TimeoutError:
    result = await await_callback(fetch, timeout=60)
Defensive patterns

Strategy: retry

Try / catch

try:
    result = await await_callback(fn, timeout=10)
except TimeoutError as e:
    if "timed out after" in str(e):
        result = await await_callback(fn, timeout=30)  # bounded single retry

Prevention

When it happens

Trigger: await_callback(callback, timeout=N) where callback() does not finish within N seconds — e.g. a provider's async request hanging on a slow endpoint or a dead connection without its own timeout.

Common situations: Slow upstream providers under load; timeouts set lower than the provider's typical time-to-first-token; network drops where the TCP stall outlives the configured budget.

Understand the failure class

Related errors


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