i am gettung these error when i am session is ende...
# ask-ai
e
i am gettung these error when i am session is ended Traceback (most recent call last): File "/home/shehryar/Documents/multi_agent/voice-ai-multiagent-backend/livekit/agents/utils/log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/shehryar/Documents/multi_agent/voice-ai-multiagent-backend/livekit/plugins/deepgram/stt.py", line 502, in recv_task raise APIStatusError(message="deepgram connection closed unexpectedly") livekit.agents._exceptions.APIStatusError: deepgram connection closed unexpectedly (status_code=-1, request_id=None, body=None, retryable=True) {"pid": 144682, "job_id": "AJ_xEnNDCBq5R9d"} 2025-11-21 064831,670 - INFO multi_agent - transfer_call_status--app.py--inside while true: False {"pid": 144682, "job_id": "AJ_xEnNDCBq5R9d"} 2025-11-21 064831,670 - INFO multi_agent - [STT_FINAL] {"pid": 144682, "job_id": "AJ_xEnNDCBq5R9d"} 2025-11-21 064831,672 - DEBUG livekit.agents - input stream detached {"participant": "human", "source": "SOURCE_UNKNOWN", "accepted_sources": ["SOURCE_MICROPHONE"], "pid": 144682, "job_id": "AJ_xEnNDCBq5R9d"} 2025-11-21 064831,683 - WARNING livekit.agents - failed to recognize speech, retrying in 0.1s Traceback (most recent call last): File "/home/shehryar/Documents/multi_agent/voice-ai-multiagent-backend/livekit/agents/stt/stt.py", line 287, in _main_task return await self._run() ^^^^^^^^^^^^^^^^^ File "/home/shehryar/Documents/multi_agent/voice-ai-multiagent-backend/livekit/plugins/deepgram/stt.py", line 534, in _run task.result() File "/home/shehryar/Documents/multi_agent/voice-ai-multiagent-backend/livekit/agents/utils/log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/shehryar/Documents/multi_agent/voice-ai-multiagent-backend/livekit/plugins/deepgram/stt.py", line 502, in recv_task raise APIStatusError(message="deepgram connection closed unexpectedly") livekit.agents._exceptions.APIStatusError: deepgram connection closed unexpectedly (status_code=-1, request_id=None, body=None, retryable=True) {"t\
t
Thanks for reaching out, Shehryar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding about the "deepgram connection closed unexpectedly" error you see when a session ends. What this usually means • The Deepgram streaming connection closed before the STT plugin finished processing. That can happen during normal session teardown, a network interruption, or a service-side disconnect. The error is marked retryable. What you can check and try • Confirm this happens only at session end. If it appears during active transcription, it’s more likely a network/API or key issue. • Validate your Deepgram API key and that the service is reachable from the host running the plugin. • Inspect network stability and firewall/NAT timeouts between your server and Deepgram (short-lived connections can drop during teardown). • Add or tune retry handling for the STT task so transient disconnects don’t fail the job. • Consider using the Agents
FallbackAdapter
to route to a backup STT provider if Deepgram disconnects frequently. • Review logs around the disconnect time to see whether the room/participant was being torn down or if there’s a preceding network error. Quick diagnostic info I need to help further 1. Does this error only occur when you intentionally end the session, or also during ongoing calls? 2. Any recent changes to network, firewall, or API keys? 3. Which LiveKit Agents/Deepgram plugin version are you running (or your repo commit/requirements)? Tell me answers to those and I’ll suggest precise next steps or code-level changes. Sources: Deepgram STT plugin guide | LiveKit docs | Events and error handling | LiveKit docs | Understanding the STATE_MISMATCH Disconnect Reason | AI Service Plugin Ecosystem | livekit/livekit_composite | DeepWiki
e
now can i end the session gracefully then
t
You're asking how to end the session gracefully to avoid the Deepgram "connection closed unexpectedly" error — yes, you can. Follow these steps to shut down cleanly: 1. Signal end of audio to the STT stream • Call the STT stream's end method before closing (example):
stt_stream.end_input()
then
await stt_stream.aclose()
so Deepgram can finalize transcription. 1. Drain pending work with a graceful shutdown • Use the session/ctx shutdown that waits for in-flight tasks to finish: ◦ Python:
await session.shutdown(drain=True)
◦ Node:
ctx.shutdown('Session ended')
1. Cancel background tasks in a shutdown hook • Stop any silence monitors or background loops before/while shutting down to avoid post-disconnect activity. 1. Prefer shutdown(drain=True) over immediate close • Don’t call immediate close/`aclose()` on the session without draining — that causes abrupt disconnects and the error you saw. 1. (Optional) Use a fallback or retries • Add retry logic for transient STT disconnects and consider the Agents
FallbackAdapter
to switch providers if Deepgram disconnects frequently. If you want, share which code path you call to end the session (your shutdown snippet or where you currently call
aclose()
), your STT stream calls, and the LiveKit/Deepgram plugin version — I’ll suggest exact code edits. Sources: Job lifecycle | LiveKit docs | How to detect and handle silence during calls