from dotenv import load_dotenv from livekit impor...
# ask-ai
b
from dotenv import load_dotenv from livekit import agents from livekit.agents import AgentSession, Agent, RoomInputOptions from livekit.plugins import ( openai, noise_cancellation, ) load_dotenv(".env.local") class Assistant(Agent): def __init__(self) -> None: super().__init__(instructions="You are a helpful voice AI assistant.") async def entrypoint(ctx: agents.JobContext): session = AgentSession( llm=openai.realtime.RealtimeModel( voice="coral" ) ) await session.start( room=ctx.room, agent=Assistant(), room_input_options=RoomInputOptions( # For telephony applications, use
BVCTelephony
instead for best results noise_cancellation=noise_cancellation.BVC(), ), ) await session.generate_reply( instructions="Greet the user and offer your assistance. You should start by speaking in English." ) if name == "__main__": agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint)) in room i want the callers sid how can i get that
t
Thanks for reaching out, Aman. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you retrieve the caller's SID in a LiveKit room using the Python SDK! You can easily access the
sid
property of a
RemoteParticipant
object, which is a unique identifier assigned to each participant when they join the room. For instance, you might want to use the
participant_connected
event to get the SID as soon as someone joins:
Copy code
@room.on("participant_connected")
def on_participant_connected(participant):
    print(f"Participant SID: {participant.sid}, Identity: {participant.identity}")
If you'd like to see the SIDs of all participants currently in the room, you can simply loop through the
remote_participants
dictionary like this:
Copy code
for identity, participant in room.remote_participants.items():
    print(f"SID: {participant.sid}, Identity: {identity}")
Please feel free to reach out if you have any more questions or need a hand with anything else! Sources: livekit-protocol/livekit/protocol/models.py | SIP participant | LiveKit Docs
b
CAfa6ed932bef9e057a96edd4cceec0ff7 above is the twilio call sid is there a way to get the twilio call sid in the room so that i can save the other romm related data in the db mapping it with the twilio call sid
t
I'm happy to help you retrieve the Twilio Call SID in a LiveKit room! You can find it by accessing the SIP participant's attributes. If you're using Twilio as your SIP provider, the
sip.twilio.callSid
attribute is conveniently included automatically in the participant's attributes. Here's a simple way to access it:
Copy code
if participant.kind == ParticipantKind.SIP:
    print(participant.attributes['sip.twilio.callSid'])
Just a few things to keep in mind: • Make sure you’re using Twilio SIP trunks, as this attribute shows up only when Twilio is your SIP provider. • Check that the
X-Twilio-CallSid
SIP header is correctly mapped to the
sip.twilio.callSid
attribute. • Confirm the participant is indeed a SIP participant by verifying
participant.kind == ParticipantKind.SIP
. If you don’t see the attribute, it’s a good idea to double-check your SIP trunk setup and ensure the necessary headers are being sent. Please feel free to reach out if you want me to walk you through any of these steps—I’m here to help! Sources: SIP participant | LiveKit Docs | pkg/sip/inbound.go
b
partecipant kind = 3
t
Participant kind 3 represents a SIP (Session Initiation Protocol) participant, which is a telephony user connected to a LiveKit session via SIP integration. Since your participant is of this type, you can find the Twilio Call SID in the participant's attributes if Twilio is your SIP provider. Take a look at the
sip.twilio.callSid
attribute within the participant's attributes:
Copy code
if participant.kind == ParticipantKind.SIP:
    print(participant.attributes['sip.twilio.callSid'])
If you don’t see this attribute, please double-check that: • You are indeed using Twilio SIP trunks, as this attribute appears only when Twilio is the SIP provider. • The
X-Twilio-CallSid
SIP header is correctly mapped to the
sip.twilio.callSid
attribute. If you have any questions or need a hand with this, just let me know—I’m here to help! Sources: livekit-protocol/livekit/protocol/models.pyi | packages/livekit-rtc/src/proto/participant_pb.ts | lib/src/proto/livekit_models.pbenum.dart | livekit/livekit_models.pb.go | livekit-ffi/src/livekit.proto.rs
b
{"message": "failed to emit event participant_connected\nTraceback (most recent call last):\n File \"/home/amansharma/projects/voice_agent/.venv/lib/python3.10/site-packages/livekit/rtc/event_emitter.py\", line 58, in emit\n callback(*callback_args)\n File \"/home/amansharma/projects/voice_agent/agent_dev.py\", line 128, in on_participant_connected\n if participant.kind == ParticipantKind.SIP:\n File \"/home/amansharma/projects/voice_agent/.venv/lib/python3.10/site-packages/google/protobuf/internal/enum_type_wrapper.py\", line 102, in __getattr__\n raise AttributeError('Enum {} has no value defined for name {!r}'.format(\nAttributeError: Enum ParticipantKind has no value defined for name 'SIP'", "level": "ERROR", "name": "livekit", "pid": 1592851, "job_id": "AJ_3qAsHze4trqp", "timestamp": "2025-09-30T101843.145319+00:00"}
t
It looks like the error happens because the Python SDK expects the full constant name for enums. To fix this, you can replace
ParticipantKind.SIP
with
rtc.ParticipantKind.PARTICIPANT_KIND_SIP
. Here’s a little snippet with the corrected code:
Copy code
if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
    # Handle SIP participant
    pass
Also, just to make sure everything runs smoothly, please check the following: • That your
livekit-rtc
package is up to date by running
pip install --upgrade livekit-rtc
. • You’re importing the enum properly with
from livekit import rtc
. • If you want to peek at the available enum values for debugging, you can use:
Copy code
import livekit.rtc._proto.participant_pb2 as proto_participant
print([attr for attr in dir(proto_participant.ParticipantKind) if not attr.startswith('_')])
If you run into any more questions or need a hand with this, just let me know—I’m here to help! Sources: packages/livekit-rtc/src/proto/participant_pb.ts | Runtime/Scripts/Proto/Participant.cs | Docs::Telephony | Rooms, participants, and tracks | LiveKit Docs | livekit-protocol/livekit/protocol/sip.pyi
b
@ctx.room.on("participant_disconnected") def on_participant_disconnected(participant: rtc.RemoteParticipant): nonlocal participant_count participant_count -= 1 print("partecipant :-",participant) print(f"Participant disconnected: {participant.name} (Remaining: {participant_count}) partecipant kind = {participant.kind}") print("participant.attributes :- ",participant.attributes) if participant.kind == ParticipantKind.SIP: print(participant.attributes['sip.twilio.callSid']) # If no participants left, end conversation if participant_count == 0: print("All participants disconnected - ending conversation")
t
I'm glad you're looking to handle participant attributes and kind in the
participant_disconnected
event! You can easily access these properties directly from the
Participant
object. Here's a little example to guide you:
Copy code
@ctx.room.on("participant_disconnected")
def on_participant_disconnected(participant: rtc.Participant):
    print(f"Participant disconnected: {participant.identity}")
    print(f"Participant kind: {participant.kind}")
    print(f"Attributes: {participant.attributes}")

    if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
        print(participant.attributes['sip.twilio.callSid'])

    # Optionally, check disconnect reason
    if participant.disconnect_reason:
        print(f"Disconnected reason: {participant.disconnect_reason}")
Just a quick tip: make sure to use
rtc.ParticipantKind.PARTICIPANT_KIND_SIP
when working with SIP participants. If anything feels unclear or if you want to dive deeper, please don’t hesitate to ask—I’m here to help! Sources: livekit-rtc/livekit/rtc/room.py | Rooms, participants, and tracks | LiveKit Docs