{"record":{"id":"e6d7d0b3140f08d0","repo":"xtekky/gpt4free","slug":"the-operation-timed-out-after-seconds-in","errorCode":null,"errorMessage":"The operation timed out after {} seconds in {}","messagePattern":"The operation timed out after (.+?) seconds in (.+?)","errorType":"exception","errorClass":"TimeoutError","httpStatus":null,"severity":"error","filePath":"g4f/providers/base_provider.py","lineNumber":498,"sourceCode":"        cache_file = cls.get_cache_file()\n        try:\n            auth_result = cls.get_auth_result()\n            response = to_async_iterator(\n                cls.create_authed(model, messages, **kwargs, auth_result=auth_result)\n            )\n            if \"stream_timeout\" in kwargs or \"timeout\" in kwargs:\n                timeout = (\n                    kwargs.get(\"stream_timeout\")\n                    if cls.use_stream_timeout\n                    else kwargs.get(\"timeout\")\n                )\n                while True:\n                    try:\n                        yield await asyncio.wait_for(\n                            response.__anext__(), timeout=timeout\n                        )\n                    except TimeoutError as e:\n                        raise TimeoutError(\n                            \"The operation timed out after {} seconds in {}\".format(\n                                timeout, cls.__name__\n                            )\n                        ) from e\n                    except StopAsyncIteration:\n                        break\n            else:\n                async for chunk in response:\n                    yield chunk\n        except (MissingAuthError, NoValidHarFileError, CloudflareError):\n            # if cache_file.exists():\n            #     cache_file.unlink()\n            response = cls.on_auth_async(**kwargs)\n            async for chunk in response:\n                if isinstance(chunk, AuthResult):\n                    auth_result = chunk\n                else:\n                    yield chunk","sourceCodeStart":480,"sourceCodeEnd":516,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/providers/base_provider.py#L480-L516","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","solutions":["Raise the stream_timeout/timeout value, especially for slow or reasoning models (60–180s is common).","Retry the request — a mid-stream stall is often transient; the same call usually succeeds.","Distinguish the two kwargs: 'stream_timeout' applies only when cls.use_stream_timeout is truthy, otherwise 'timeout' is used.","If stalls are persistent, switch providers or check network egress to the provider host."],"exampleFix":"// before\nchunks = provider.create_async_generator(model, messages, stream_timeout=10)\n\n// after\nchunks = provider.create_async_generator(model, messages, stream_timeout=120)","handlingStrategy":"retry","validationCode":"timeout = kwargs.get('stream_timeout', kwargs.get('timeout'))\nif timeout is not None and timeout < 30:\n    kwargs['stream_timeout'] = max(timeout, 30)  # avoid guaranteed timeouts on slow models","typeGuard":null,"tryCatchPattern":"try:\n    chunks = [c async for c in provider.create_async_generator(model, messages, stream_timeout=120)]\nexcept TimeoutError as e:\n    if provider.__name__ in str(e):  # our stream timeout, not a generic one\n        chunks = [c async for c in provider.create_async_generator(model, messages, stream_timeout=300)]\n    else:\n        raise","preventionTips":["Size stream_timeout to the model (reasoning models need 60-300s inter-chunk).","Remember 'timeout' is used instead of 'stream_timeout' when the provider sets use_stream_timeout=False.","Retry once on stream timeout — stalls are frequently transient."],"tags":["timeout","streaming","asyncio","network","g4f"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}