How does ctx.wait_for_participant work in the Pyth...
# ask-ai
c
How does ctx.wait_for_participant work in the Python SDK?
t
Thanks for reaching out, Anton. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The
ctx.wait_for_participant
method in the LiveKit Python SDK is a handy tool that helps you wait for a specific participant to join a room during an agent job. It’s a coroutine that returns a
rtc.RemoteParticipant
object once it finds a matching participant who has joined or is already in the room. Key Features: • Argument-based Filtering: You have the flexibility to specify an
identity
if you’re looking for a participant with a particular identity. If you don’t provide an
identity
, it will simply wait for the first participant to join. You can also filter by
kind
(like standard or SIP participants), which is pretty useful. • Immediate Return: If the participant you’re waiting for is already there, it won’t keep you waiting—it returns right away. • Asynchronous Waiting: If no matching participant is present yet, it patiently listens for the "participant_connected" event and returns the first participant that fits your criteria. • Room Connection Requirement: Just a heads-up: the room needs to be connected, or else it will raise a
RuntimeError
. Example Usage: • Wait for the first participant to join:
participant = await ctx.wait_for_participant()
• Wait for a participant with a specific identity:
participant = await ctx.wait_for_participant(identity=participant_identity)
• Wait for a standard participant:
Copy code
participant_task = asyncio.create_task(
    ctx.wait_for_participant(
        kind=[rtc.ParticipantKind.PARTICIPANT_KIND_STANDARD]
    )
)
If you have any questions or need a bit more help, please don’t hesitate to ask—I’m here to support you! Sources: examples/voice_agents/realtime_joke_teller.py | agent.py | agent.py
c
Is it an intended usage of this method to wait for the user to join a call with the assistant before greeting the user with session.say()? Or are there other ways and methods to ensure we only start speaking after the user has joined?
t
Yes,
ctx.wait_for_participant
is designed to wait for a user to join before using
session.say
. This helps make sure the agent doesn’t try to speak before the participant has joined the room, which can prevent missed audio messages or any unexpected behavior. Alternative Methods: • Event Listeners: You can use the
@ctx.room.on("participant_connected")
event to respond dynamically when a participant joins. For example:
Copy code
@ctx.room.on("participant_connected")
async def on_participant_connected(participant):
    await session.say("Hello! I'm ready to assist you.")
• Custom Agent Hooks: If your agent inherits from the
Agent
class, you might consider overriding the
on_enter()
method to start speaking once the agent is activated and ready to interact. For straightforward situations,
ctx.wait_for_participant
is a solid and recommended choice. But if your workflow is a bit more complex, event-driven logic can offer extra flexibility. Please feel free to reach out if you'd like more details or some examples—I’m here to help! Sources: agent.py | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki | telephony/sip_lifecycle.py | agent/main.py | agent.py
c
What is the participant's identity?
t
In LiveKit, a participant's identity is a unique string that you, as the developer, provide to identify a user or process within a room. It’s important that this identity is unique for each room and is set when the participant connects. If someone tries to join a room using an identity that’s already taken, the server will disconnect the previous participant with that identity. This identity is really helpful because it lets you link a LiveKit session to a specific user in your application, like a user ID or device ID. Just to clarify, the identity is different from the server-generated session ID (SID) and is part of the access token used to connect to a room. If you have any questions or want me to explain anything further, please don’t hesitate to ask—I’m here to help! Sources: Sources/LiveKit/Participant/Participant+Identifiable.swift | packages/react/src/hooks/useRemoteParticipant.ts | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki | Rooms, participants, and tracks | LiveKit Docs | Connecting to LiveKit | LiveKit Docs
c
What does this code do? str(next(iter(ctx.room.remote_participants)))
t
I'm happy to help you understand the code
str(next(iter(ctx.room.remote_participants)))
! This snippet retrieves the identity of the very first remote participant in a LiveKit room and turns it into a string. Let me walk you through how it works: •
ctx.room.remote_participants
is like a dictionary that holds all the remote participants in the room, using their identities as keys. •
iter(ctx.room.remote_participants)
creates an iterator over those keys, which are the participant identities. •
next(iter(...))
grabs the first identity from that iterator. •
str(...)
converts this identity to a string—though since it’s usually already a string, this step is mostly just for safety. This approach is handy when you want to quickly reference the first participant’s identity, for example, to send RPC calls or take specific actions in your app. If you’d like, I’m here to provide more examples or clarify anything further—just let me know! Sources: livekit-rtc/livekit/rtc/room.py | Docs::Agents | complex-agents/drive-thru/tools/management_tools.py
c
Will this work for getting the participant identity of a non-agent participant, and then waiting for them to join? Return the code if yes
t
Hey there! Just a quick heads-up: the code
str(next(iter(ctx.room.remote_participants)))
works to get the identity of a non-agent participant only if someone has already joined the room. It doesn’t wait for participants to join, so if there’s no one there yet, it might raise an error. To make things smoother and safer, I recommend using
ctx.wait_for_participant()
. This handy method checks if any participants are already present and patiently waits for new ones if needed. Here’s a little example for you:
Copy code
# Wait for any non-agent participant (standard or SIP)
participant = await ctx.wait_for_participant()
user_identity = participant.identity
And if you’re looking to wait for a specific participant by identity, you can do it like this:
Copy code
import livekit.rtc as rtc

participant = await ctx.wait_for_participant(
    identity="user123",
    kind=[
        rtc.ParticipantKind.PARTICIPANT_KIND_SIP,
        rtc.ParticipantKind.PARTICIPANT_KIND_STANDARD
    ]
)
user_identity = participant.identity
This way, you’ll have a reliable way to catch the right non-agent participants without any hiccups. If anything’s unclear or you want to dive deeper, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/voice/room_io/room_io.py | Rooms, participants, and tracks | LiveKit Docs | Core Platform Architecture & Authentication | livekit/livekit_composite | D... | examples/rpc.py | agents/src/utils.ts
c
Won't wait_for_participant auto-return since the agent is already in the room?
t
Absolutely! The
wait_for_participant
function will return right away if it finds a matching participant already in the room. If you don’t provide a specific identity, it simply returns the first participant it comes across. Also, if the agent happens to be the only participant, it won’t match itself since it’s designed to wait for remote participants (those who aren’t local). If you’d like to make sure it waits for a particular non-agent participant, you can specify the
identity
or use the
kind
filter to exclude agents. Please feel free to ask if you want me to walk you through some examples or if anything needs more clarity—I’m here to help! Sources: livekit-agents/livekit/agents/job.py | agents/src/utils.ts
c
Are you absolutely sure it won't return when only the agent is in the room? Provide sources
t
wait_for_participant
will not return when only the agent is in the room. Here’s why: • The function only checks
room.remote_participants
, which contains non-local participants. The agent is a local participant and is not included in this collection. • By default, the function filters participants by kind, excluding agents. The default kinds are
STANDARD
and
SIP
, ensuring that only non-agent participants are matched. • Usage examples consistently show
wait_for_participant
being used to wait for user participants after the agent has already connected. • The Node.js equivalent explicitly excludes participants of kind
AGENT
, reinforcing the intended behavior across SDKs. Sources: • <#a5510711-aef0-484b-8bfb-226a6ac9f504|Python SDK implementation of wait_for_participant> • <#8ca9f0a6-e5a8-4c0d-84d7-8481a8859e79|Node.js SDK implementation of waitForParticipant> • <#ecff523c-3f7b-41b1-aa43-769feb8bc9ad|Example usage in Python SDK> If you have any questions or would like me to walk you through this with some examples, please don’t hesitate to ask—I’m here to help! Sources: examples/voice_agents/realtime_joke_teller.py | sip-refer/agent.py | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki