{"record":{"id":"3d8908aa5970ea69","repo":"BerriAI/litellm","slug":"unexpected-error-while-closing-websocket-close-e","errorCode":null,"errorMessage":"Unexpected error while closing WebSocket: {close_error}","messagePattern":"Unexpected error while closing WebSocket: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/llms/openai/realtime/handler.py","lineNumber":188,"sourceCode":"                    force_transcription_model=(\n                        model if (query_params or {}).get(\"intent\") == \"transcription\" else None\n                    ),\n                    event_normalizer=self._make_event_normalizer(),\n                )\n                await realtime_streaming.bidirectional_forward()\n\n        except websockets.exceptions.InvalidStatusCode as e:\n            await websocket.close(code=e.status_code, reason=_redact_string(str(e)))\n        except Exception as e:\n            try:\n                await websocket.close(code=1011, reason=_redact_string(f\"Internal server error: {e}\"))\n            except RuntimeError as close_error:\n                if \"already completed\" in str(close_error) or \"websocket.close\" in str(close_error):\n                    # The WebSocket is already closed or the response is completed, so we can ignore this error\n                    pass\n                else:\n                    # If it's a different RuntimeError, we might want to log it or handle it differently\n                    raise Exception(f\"Unexpected error while closing WebSocket: {close_error}\")\n","sourceCodeStart":170,"sourceCodeEnd":189,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/openai/realtime/handler.py#L170-L189","documentation":"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.","triggerScenarios":"Thrown at litellm/llms/openai/realtime/handler.py:188 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["The realtime session already ended abnormally; inspect the close error in the message and reconnect if needed."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}