ambitious-ice-35806
08/03/2025, 3:43 PMfrom dotenv import load_dotenv
from livekit import agents
from livekit.agents import AgentSession, Agent, RoomInputOptions
from livekit.plugins import elevenlabs,google,deepgram,noise_cancellation,silero
from livekit.plugins.turn_detector.multilingual import MultilingualModel
from Config import saveQualifiedLead,disqualifyLead,CallbackTime
from Prompts import SYSTEM_PROMPT
load_dotenv()
class Assistant(Agent):
def __init__(self) -> None:
super().__init__(instructions=SYSTEM_PROMPT,
stt=deepgram.STT(model="nova-2-phonecall"),
llm=google.LLM(model="gemini-2.5-flash-preview-05-20"),
tts=elevenlabs.TTS(model="eleven_turbo_v2_5",voice_id="UgBBYS2sOqTuMpoF3BR0"),
vad=silero.VAD.load(),
turn_detection=MultilingualModel(),
tools=[saveQualifiedLead,disqualifyLead,CallbackTime]
)
async def entrypoint(ctx: agents.JobContext):
session = AgentSession(allow_interruptions=False)
await session.start(
room=ctx.room,
agent=Assistant(),
room_input_options=RoomInputOptions(
audio_enabled=True,
video_enabled=False,
noise_cancellation=noise_cancellation.BVC(),
),
)
await session.generate_reply( instructions="Simply Say 'Hello!! How are you Doing!!' ")
if __name__ == "__main__":
agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint,agent_name="Umer"))
❌ Error:
2025-08-03 20:39:17,262 - INFO livekit.agents - initializing process {"pid": 3276, "inference": true}
2025-08-03 20:39:20,104 - DEBUG livekit.agents - initializing inference runner {"runner": "lk_end_of_utterance_multilingual", "pid": 3276, "inference": true}
2025-08-03 20:39:27,270 - INFO livekit.agents - killing process {"pid": 3276, "inference": true}
2025-08-03 20:39:27,270 - ERROR livekit.agents - worker failed
Traceback (most recent call last):
File "C:\Python313\Lib\asyncio\tasks.py", line 507, in wait_for
return await fut
^^^^^^^^^
File "D:\livekit-Agent-development\.venv\Lib\site-packages\livekit\agents\ipc\channel.py", line 47, in arecv_message
return _read_message(await dplx.recv_bytes(), messages)
^^^^^^^^^^^^^^^^^^^^^^^
File "D:\livekit-Agent-development\.venv\Lib\site-packages\livekit\agents\utils\aio\duplex_unix.py", line 35, in recv_bytes
len_bytes = await self._reader.readexactly(4)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python313\Lib\asyncio\streams.py", line 769, in readexactly
await self._wait_for_data('readexactly')
File "C:\Python313\Lib\asyncio\streams.py", line 539, in _wait_for_data
await self._waiter
asyncio.exceptions.CancelledError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\livekit-Agent-development\.venv\Lib\site-packages\livekit\agents\cli\_run.py", line 79, in _worker_run
await worker.run()
File "D:\livekit-Agent-development\.venv\Lib\site-packages\livekit\agents\worker.py", line 387, in run
await self._inference_executor.initialize()
File "D:\livekit-Agent-development\.venv\Lib\site-packages\livekit\agents\ipc\supervised_proc.py", line 169, in initialize
init_res = await asyncio.wait_for(
^^^^^^^^^^^^^^^^^^^^^^^
...<2 lines>...
)
^
File "C:\Python313\Lib\asyncio\tasks.py", line 506, in wait_for
async with timeouts.timeout(timeout):
~~~~~~~~~~~~~~~~^^^^^^^^^
File "C:\Python313\Lib\asyncio\timeouts.py", line 116, in __aexit__
raise TimeoutError from exc_val
TimeoutError
2025-08-03 20:39:27,277 - ERROR livekit.agents - Error in _read_ipc_task
Traceback (most recent call last):
File "D:\livekit-Agent-development\.venv\Lib\site-packages\livekit\agents\utils\aio\duplex_unix.py", line 35, in recv_bytes
len_bytes = await self._reader.readexactly(4)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python313\Lib\asyncio\streams.py", line 767, in readexactly
raise exceptions.IncompleteReadError(incomplete, n)
asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 4 expected bytes
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\livekit-Agent-development\.venv\Lib\site-packages\livekit\agents\utils\log.py", line 16, in async_fn_logs
return await fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\livekit-Agent-development\.venv\Lib\site-packages\livekit\agents\cli\watcher.py", line 120, in _read_ipc_task
msg = await channel.arecv_message(self._pch, proto.IPC_MESSAGES)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\livekit-Agent-development\.venv\Lib\site-packages\livekit\agents\ipc\channel.py", line 47, in arecv_message
return _read_message(await dplx.recv_bytes(), messages)
^^^^^^^^^^^^^^^^^^^^^^^
File "D:\livekit-Agent-development\.venv\Lib\site-packages\livekit\agents\utils\aio\duplex_unix.py", line 43, in recv_bytes
raise DuplexClosed() from e
livekit.agents.utils.aio.duplex_unix.DuplexClosedtall-airline-16756
08/03/2025, 3:43 PMtall-airline-16756
08/03/2025, 3:44 PMambitious-ice-35806
08/03/2025, 3:46 PMtall-airline-16756
08/03/2025, 3:47 PMlivekit.agents.utils.aio.duplex_unix.DuplexClosed pops up when a duplex connection closes unexpectedly or just stops working. This might happen because of network hiccups, a process crashing, or even if the connection was closed on purpose.
Here’s how you can tackle this:
• Wrap your duplex operations (like send_bytes, recv_bytes, aclose) inside try-except blocks to catch that DuplexClosed exception.
• Log the error so you know what’s going on, and then decide what to do next — maybe reconnect or stop further actions.
• Think about adding some retry logic or reinitializing the connection if you can bounce back from this.
Here’s a quick example:
try:
await duplex.send_bytes(data)
except DuplexClosed:
print("Oops! Duplex connection closed unexpectedly. Shutting down.")
await duplex.aclose()
# You can also try reconnecting or handle the closure as needed
Also, it’s a good idea to clean up resources properly, keep an eye on connection health, and use logging to track what’s happening with the connection. If you want, I’m here to help you out with anything else!