xtekky/gpt4free · error · RuntimeError

WebSocket Error: {ws.exception()}

Error message

WebSocket Error: {ws.exception()}

What it means

While reading the CopilotApp chat WebSocket, aiohttp reported a protocol-level WSMsgType.ERROR. The underlying exception from ws.exception() (connection reset, handshake failure, protocol violation) is embedded in the message. This is a transport-layer failure of the websocket, distinct from application-level error events.

Source

Thrown at g4f/Provider/CopilotApp.py:128

                    async for msg in ws:
                        if msg.type == aiohttp.WSMsgType.TEXT:
                            data = json.loads(msg.data)
                            event = data.get("event")
                            if event == "appendText":
                                yield data.get("text", "")
                            elif event == "citation":
                                sources[data.get("url")] = data
                                yield SourceLink(
                                    list(sources.keys()).index(data.get("url")),
                                    data.get("url"),
                                )
                            elif event == "done":
                                if sources:
                                    yield Sources(sources.values())
                                cls.live += 1
                                break
                        elif msg.type == aiohttp.WSMsgType.ERROR:
                            raise RuntimeError(f"WebSocket Error: {ws.exception()}")
        except Exception as e:
            cls.live -= 1
            raise RuntimeError(f"Error during CopilotApp interaction: {e}") from e

View on GitHub (pinned to 973504e177)

Solutions

  1. Check ws.exception() in the message for the precise transport cause
  2. Retry with backoff — transient resets are common for long streams
  3. If behind a proxy, ensure it supports long-lived WebSocket connections or bypass it
Defensive patterns

Strategy: retry

Try / catch

try:
    async for chunk in CopilotApp.create_async_generator(model, messages):
        yield chunk
except RuntimeError as e:
    if 'WebSocket Error' in str(e):
        await asyncio.sleep(2)
        async for chunk in CopilotApp.create_async_generator(model, messages):
            yield chunk
    else:
        raise

Prevention

When it happens

Trigger: WebSocket connection reset by server or intermediary proxy; TLS/proxy handshake issue after connect; keepalive timeout closing the socket mid-stream; server-side connection drain during maintenance.

Common situations: Corporate proxy or HTTP/2-aware middleware terminating long-lived websockets; idle timeout on NAT/firewall; high-latency links dropping the socket; too many concurrent sockets from one client.

Related errors


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