BerriAI/litellm · error · Exception
Unexpected error while closing WebSocket: {close_error}
Error message
Unexpected error while closing WebSocket: {close_error} What it means
Logged-then-raised condition in the realtime websocket relay's cleanup path: after handling a streaming error, the attempt to close the websocket with code 1011 itself threw (close_error). It indicates the client socket is already dead/unreachable during teardown, so the graceful-close fallback failed too.
Source
Thrown at litellm/llms/openai/realtime/handler.py:188
force_transcription_model=(
model if (query_params or {}).get("intent") == "transcription" else None
),
event_normalizer=self._make_event_normalizer(),
)
await realtime_streaming.bidirectional_forward()
except websockets.exceptions.InvalidStatusCode as e:
await websocket.close(code=e.status_code, reason=_redact_string(str(e)))
except Exception as e:
try:
await websocket.close(code=1011, reason=_redact_string(f"Internal server error: {e}"))
except RuntimeError as close_error:
if "already completed" in str(close_error) or "websocket.close" in str(close_error):
# The WebSocket is already closed or the response is completed, so we can ignore this error
pass
else:
# If it's a different RuntimeError, we might want to log it or handle it differently
raise Exception(f"Unexpected error while closing WebSocket: {close_error}")
View on GitHub (pinned to 77b7c6c40c)
Solutions
- The realtime session already ended abnormally; inspect the close error in the message and reconnect if needed.
Defensive patterns
Strategy: try-catch
When it happens
Trigger: Thrown at litellm/llms/openai/realtime/handler.py:188 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/3d8908aa5970ea69.
Report an issue: GitHub.