How do I get the participant metadata in add_shutd...
# ask-ai
m
How do I get the participant metadata in add_shutdown_callback() method? Following is my code in Python:
Copy code
async def entrypoint(ctx: JobContext):
     async def write_transcript():
        participant = ctx.room.local_participant
        user_id = participant.metadata
        room_id = ctx.room.name

ctx.add_shutdown_callback(write_transcript)
await ctx.connect()
t
Thanks for reaching out, Rishabh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! To grab participant metadata inside the
add_shutdown_callback()
method, you'll want to make sure you’ve already got a reference to the participant saved earlier in your job’s lifecycle. Here’s a friendly step-by-step on how to do that: Steps: 1. Use
ctx.wait_for_participant()
to wait for someone to join and get their metadata. This way, you have their info before the shutdown callback kicks in. 2. Save the participant’s metadata or identity in a variable or some structure you can easily access later inside the shutdown callback. 3. Then, inside the
add_shutdown_callback()
method, just pull that stored metadata or refresh it from
ctx.room.remote_participants
if you need to. Here’s a little example to make it clearer:
Copy code
async def entrypoint(ctx: JobContext):
    # Wait for the first participant to join
    participant = await ctx.wait_for_participant()
    
    # Store participant metadata
    participant_data = {
        "identity": participant.identity,
        "metadata": participant.metadata
    }

    # Define shutdown callback
    async def shutdown_callback(reason: str):
        print(f"Shutting down: {reason}")
        print(f"Metadata for {participant_data['identity']}: {participant_data['metadata']}")

    # Register the shutdown callback
    ctx.add_shutdown_callback(shutdown_callback)

    # Connect to the room
    await ctx.connect()
This way, you’ll have the participant’s metadata handy when the shutdown happens. If anything’s unclear or you want to dive deeper, just give me a shout—I’m here to help! Sources: Participant attributes and metadata | LiveKit Docs | livekit-rtc/livekit/rtc/_proto/room_pb2.pyi | livekit-agents/livekit/agents/job.py
m
wait_for_participant raise RuntimeError("room is not connected") RuntimeError: room is not connected
t
Hey there! The error
RuntimeError: room is not connected
pops up because
wait_for_participant
is being called before the room actually connects to the LiveKit server. To fix this, just make sure you connect the room first by calling
await ctx.connect()
before using
ctx.wait_for_participant()
. Easy fix! Here’s how the sequence should look:
Copy code
async def entrypoint(ctx: JobContext):\n    # Connect to the room first\n    await ctx.connect()\n\n    # Now wait for a participant to join\n    participant = await ctx.wait_for_participant()\n\n    # Store participant metadata\n    participant_data = {\n        \"identity\": participant.identity,\n        \"metadata\": participant.metadata\n    }\n\n    # Define shutdown callback\n    async def shutdown_callback(reason: str):\n        print(f\"Shutting down: {reason}\")\n        print(f\"Metadata for {participant_data['identity']}: {participant_data['metadata']}\")\n\n    # Register the shutdown callback\n    ctx.add_shutdown_callback(shutdown_callback)
By connecting the room first, you’ll sidestep this error for sure. If you have any other questions or need a hand, just let me know—I’m here to help! Sources: livekit-rtc/livekit/rtc/room.py | livekit-agents/livekit/agents/voice/avatar/_datastream_io.py | Inside a session | LiveKit Docs