billions-lawyer-8588
07/10/2025, 3:51 PMasync def entrypoint(ctx: JobContext):
@ctx.room.on('participant_disconnected')
def participant_disconnected(participant):
print("-----------room disconnected---------",participant)
await ctx.connect()
await session.start(
agent=agent,
room=ctx.room,
room_input_options=RoomInputOptions(
noise_cancellation=noise_cancellation.BVCTelephony(),
text_enabled=False,
),
)
// code for adding SIP participant
The issue that I am facing is the participant_disconnected event is not triggering in my code. How can I fix this?tall-airline-16756
07/10/2025, 3:51 PMtall-airline-16756
07/10/2025, 3:52 PMparticipant_disconnected
event may not trigger in your agent code due to several potential causes. Here are practical steps to debug and resolve this in the LiveKit Python agent context:
• Ensure Proper Event Handler Registration: Confirm the event handler is registered before connecting to the room. The annotation should reference ctx.room
if used, but the critical point is the registration must happen prior to await ctx.connect()
. Incorrect timing can prevent callbacks from firing.
@ctx.room.on("participant_disconnected")
def participant_disconnected(participant):
print(f"Participant disconnected: {participant.identity}")
• Check Agent Session Automatic Closure: Note that the LiveKit agent framework may automatically close agent sessions when participants disconnect, especially if close_on_disconnect
is enabled and the disconnect reason matches standard triggers. This might result in no further participant_disconnected
callback being fired if the session itself is closed.
• Add Logging to Debug Events and Exceptions: Implement logging in all events to verify which callbacks execute and catch any hidden exceptions:
import logging
logging.basicConfig(level=logging.DEBUG)
@ctx.room.on("participant_disconnected")
def participant_disconnected(participant):
<http://logging.info|logging.info>(f"Participant disconnected: {participant.identity}")
Check your logs for any reported errors: error running user callback for ...
.
• Monitor Room Connection State: Ensure your room remains fully connected, as the event system only operates for an active session. Log the connection state to verify: if ctx.room.isconnected(): ...
• Verify Remote Participants: The event will only fire for participants that successfully joined and were tracked. If the participant wasn’t properly registered (e.g., due to connection issues), their disconnect won’t trigger the event.
If you are still unable to trigger the event, try a minimal test case (with only connection and event handlers) to isolate the problem. See the knowledge base for further agent session details and event troubleshooting steps.
Let me know if you need example code or if you observe anything unusual in your logs.
Sources: livekit-rtc/livekit/rtc/room.py | .github/next-release/changeset-8a44d9bf.md