how can I pass the python backend some metadata of...
# ask-ai
t
how can I pass the python backend some metadata of which modal the user clicked from the frontend to change the agent context?
t
Thanks for reaching out, Ellis. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to pass metadata from your frontend to the Python backend in LiveKit, you can easily do this using the LiveKit client SDKs to update a participant’s metadata. This info syncs up with the backend server and helps you change the agent context smoothly. Here’s a simple way to get it done: • On the frontend, just use the
setMetadata()
method to update the local participant’s metadata. For example, in JavaScript:
Copy code
await localParticipant.setMetadata(JSON.stringify({ modalClicked: 'exampleModal' }));
• Make sure the participant’s token has the
canUpdateOwnMetadata
permission. • Then, on the backend, listen for metadata changes using LiveKit’s server-side APIs or event listeners. For example, in Node.js:
Copy code
room.on('participantMetadataChanged', (participant) => {
  const data = JSON.parse(participant.metadata);
  // Update agent context based on data.modalClicked
});
If you’re looking to send data directly to your own backend server (not just LiveKit), you can set up a separate API endpoint for that. Feel free to ask if you want me to walk you through it or if you have any other questions! Sources: src/api/SignalClient.ts | lib/src/participant/local.dart | Runtime/Scripts/Room/Participant/LocalParticipant.cs | Managing participants | LiveKit Docs | Managing participants | LiveKit Docs
t
to be clear my livekit chat has a query param that indicates which conversation this is, and i want the system prompt to switch on this.
t
Hey there! Just to make sure I’m on the right track, are you using a custom chat UI built on LiveKit or one of LiveKit’s features for system prompts? Also, are you hoping to set up the system prompt dynamically using query parameters in a web embed, or are you thinking more along the lines of a backend integration? Let me know so I can help you better! Sources: tools/server/themes/wild/index.html | Web Embed Example | livekit/livekit_composite | DeepWiki
t
the backend gets the metadata or query param of which conversation type it is, and switches the agent context accordingly
t
If you want to switch the agent context in LiveKit based on metadata or query parameters, here are a few friendly ways to do it: • Dynamic Agent Selection: You can check the participant’s metadata or attributes to decide which agent to start. For example, in the backend:
Copy code
async def entrypoint(ctx: JobContext):
    participant = ctx.room.local_participant
    requested_agent = participant.metadata.get("agent_type", "default")

    if requested_agent == "billing":
        agent = BillingAgent()
    elif requested_agent == "support":
        agent = SupportAgent()
    else:
        agent = TriageAgent()

    await session.start(agent=agent, room=ctx.room)
• Runtime Agent Switching: If you want to switch agents during a session, you can easily replace the active agent like this:
Copy code
self.session.update_agent(NewAgent())
• Preserving Context: To keep the conversation flowing smoothly when switching agents, you can copy and trim the previous agent’s chat context like this:
Copy code
chat_ctx = self.chat_ctx.copy()
chat_ctx.items = self._truncate_chat_ctx(prev_agent.chat_ctx.items, keep_last_n_messages=6)
await self.update_chat_ctx(chat_ctx)
One quick note: query parameters aren’t used directly in the backend, but you can pass them as metadata when joining a room or through a custom API call. If you’d like, I’m here to help you set that up! Sources: Web Embed Example | livekit/livekit_composite | DeepWiki | Workflows | LiveKit Docs | complex-agents/shopify-voice-shopper/shopify.py | Accepting incoming calls | LiveKit Docs
t
i set it like this in the next.js api, would this work? and if so how can i access it? const participantToken = await createParticipantToken( { identity: participantIdentity, name: participantName, metadata: JSON.stringify({ scenarioId }), }, roomName );
t
You're on the right track with setting the metadata in your Next.js API using
createParticipantToken
! The metadata will be attached to the participant as they join the room. Here’s a friendly tip on how you can access it: • On the backend or from other participants in the room, you can grab the metadata through the
Participant.metadata
field in the LiveKit SDKs. For example, in JavaScript:
Copy code
const metadata = JSON.parse(participant.metadata);
console.log(metadata.scenarioId);
• Just make sure the metadata is serialized as a JSON string when you set it, like you’ve done with
JSON.stringify({ scenarioId })
. If you have any questions or want a hand with accessing or using this metadata, feel free to ask—I’m here to help! Sources: packages/livekit-server-sdk/src/AccessToken.ts | app/api/connection-details/route.ts | Participant attributes and metadata | LiveKit Docs
t
im using the python livekit start kit, i want to get the metadata or print the people that are in the room
t
Hey there! If you want to list participants in a room and check out their metadata using the Python LiveKit starter kit, you can easily do that with the
RoomService
client. Here’s a quick example to get you started: • List all participants in a room:
Copy code
from livekit.api import LiveKitAPI
from livekit.protocol.room import ListParticipantsRequest

async def list_room_participants(room_name: str):
    async with LiveKitAPI() as api:
        participants_response = await api.room.list_participants(
            ListParticipantsRequest(room=room_name)
        )
        for participant in participants_response.participants:
            print(f"Identity: {participant.identity}, Metadata: {participant.metadata}")
        return participants_response.participants
• Get metadata for a specific participant:
Copy code
from livekit.api import LiveKitAPI
from livekit.protocol.room import RoomParticipantIdentity

async def get_participant_data(room_name: str, identity: str):
    async with LiveKitAPI() as api:
        participant = await api.room.get_participant(
            RoomParticipantIdentity(room=room_name, identity=identity)
        )
        print(f"Metadata: {participant.metadata}")
        return participant
Just a heads up—make sure your environment variables for
LIVEKIT_API_KEY
,
LIVEKIT_API_SECRET
, and
LIVEKIT_URL
are all set up correctly. If you hit any bumps or want to dive deeper, feel free to ask. I’m here to help! Sources: livekit-protocol/livekit/protocol/analytics.pyi | Managing participants | LiveKit Docs