polite-oil-10264
08/03/2025, 2:24 AMtall-airline-16756
08/03/2025, 2:24 AMtall-airline-16756
08/03/2025, 2:24 AMGetParticipants. Instead, you should use the LiveKit API's list_participants method to retrieve participants in a room. Here's how you can handle this:
1. Retrieve Participants:
Use the list_participants method to fetch all participants in the room:
from livekit import api
async def fetch_room_participants(room_name: str):
async def _fetch(lkapi):
list_request = api.ListParticipantsRequest(room=room_name)
participants_response = await lkapi.room.list_participants(list_request)
return [
{
"identity": participant.identity,
"name": participant.name,
"state": participant.state,
"joined_at": participant.joined_at,
"metadata": participant.metadata,
"sid": participant.sid
}
for participant in participants_response.participants
]
return await with_livekit_api(_fetch)
2. Close the Room:
If only agents remain, use the delete_room method to close the room:
from livekit import api
async def delete_livekit_room(room_name: str):
async def _delete(lkapi):
delete_request = api.DeleteRoomRequest(room=room_name)
await lkapi.room.delete_room(delete_request)
return True
return await with_livekit_api(_delete)
Example:
Check participants and close the room if only agents are present:
participants = await fetch_room_participants("my_room")
if all(p["state"] == "AGENT" for p in participants):
await delete_livekit_room("my_room")
This approach ensures you can manage room closure effectively based on participant presence. Let me know if you need further assistance!
Sources: mcp/server.py | Inside a session | LiveKit Docspolite-oil-10264
08/03/2025, 2:43 AMtall-airline-16756
08/03/2025, 2:44 AMsession.on("close") event handler in the LiveKit Python SDK. This handy feature lets you catch when the session is closing and take immediate action, like deleting the room.
Here’s a simple example to get you started:
import asyncio
import logging
from livekit.agents import AgentSession, JobContext, WorkerOptions, cli
from livekit.plugins import deepgram, openai, silero
logger = logging.getLogger("my-worker")
logger.setLevel(<http://logging.INFO|logging.INFO>)
class MyAgent(Agent):
def __init__(self):
super().__init__(instructions="You are a helpful assistant.")
async def entrypoint(ctx: JobContext):
session = AgentSession(
stt=deepgram.STT(),
llm=openai.LLM(),
tts=silero.TTS(),
vad=silero.VAD.load(),
)
# Start the session with the agent
await session.start(agent=MyAgent(), room=ctx.room)
@session.on("close")
def on_close(ev):
<http://logger.info|logger.info>(f"Session closed. Reason: {ev.reason}")
# Delete the room to stop the session immediately
ctx.delete_room()
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
Just a couple of important things to keep in mind:
• Event Handling: The session.on("close") event fires when the session is shutting down. You can peek at the reason for closure and act accordingly.
• Room Deletion: Calling ctx.delete_room() will stop the session right away when the user disconnects.
This setup helps make sure your session wraps up as soon as someone leaves. If you want me to walk you through any part of this or have other questions, just let me know—I’m here to help!