how to fix this error: 5-07-30 05:43:52,105 - ERR...
# ask-ai
n
how to fix this error: 2025-07-30 054352,105 - ERROR livekit.agents - unhandled exception while running the job task Traceback (most recent call last): File “/usr/local/lib/python3.12/site-packages/opentelemetry/util/_decorator.py”, line 71, in async_wrapper return await func(*args, **kwargs) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “/usr/local/lib/python3.12/site-packages/livekit/agents/ipc/job_proc_lazy_main.py”, line 240, in _traceable_entrypoint await self._job_entrypoint_fnc(job_ctx) File “/src/entrypoint2.py”, line 105, in entrypoint participant = await ctx.wait_for_participant() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “/usr/local/lib/python3.12/site-packages/livekit/agents/job.py”, line 250, in wait_for_participant return await wait_for_participant(self._room, identity=identity, kind=kind) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “/usr/local/lib/python3.12/site-packages/livekit/agents/utils/participant.py”, line 20, in wait_for_participant raise RuntimeError(“room is not connected”) RuntimeError: room is not connected
t
Thanks for reaching out, Tareq. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The error
RuntimeError: room is not connected
occurs when operations are attempted on a LiveKit room that is not connected. Here are steps to resolve this issue: 1. Check Connection Status Before Operations Ensure the room is connected before performing any operations:
Copy code
if room.isconnected():
    # Perform operations
    stats = await room.get_rtc_stats()
else:
    print("Room is not connected")
2. Ensure Proper Connection Make sure to connect to the room before using it:
Copy code
room = Room()
try:
    await room.connect("<ws://localhost:7880>", "your_token")
    # Now you can safely use room methods
    stats = await room.get_rtc_stats()
except ConnectError as e:
    print(f"Failed to connect: {e.message}")
3. Handle Connection State Changes Listen for connection state changes to handle disconnections:
Copy code
@room.on("disconnected")
def on_disconnected(reason):
    print(f"Room disconnected: {reason}")

@room.on("connected")
def on_connected():
    print("Room connected successfully")
4. Wait for Connection in Utility Functions Ensure the room is connected before using utility functions like `wait_for_participant()`:
Copy code
if not room.isconnected():
    await room.connect(url, token)

participant = await wait_for_participant(room, identity="user123")
5. Proper Error Handling Wrap room operations in try-catch blocks to handle connection errors:
Copy code
try:
    if not room.isconnected():
        await room.connect(url, token)

    participant = await wait_for_participant(room)

except RuntimeError as e:
    if "room is not connected" in str(e):
        print("Room connection lost, attempting to reconnect...")
        await room.connect(url, token)
    else:
        raise
By following these steps, you can avoid and handle the "room is not connected" error effectively. Sources: livekit-agents/livekit/agents/utils/participant.py | livekit-rtc/livekit/rtc/room.py | packages/livekit-rtc/src/room.ts