how to update agent participant metadata
# ask-ai
r
how to update agent participant metadata
t
Thanks for reaching out, Ivan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To update agent participant metadata in LiveKit, you can use the
UpdateParticipant
API from the server-side SDKs. This is a great way to modify the metadata of any participant in a room, including agents, and I'm here to help you through it! Here are some examples for different SDKs to get you started: • Python:
Copy code
import livekit.api
lkapi = livekit.api.LiveKitAPI()

await lkapi.room.update_participant(
    UpdateParticipantRequest(
        room="roomName",
        identity="participantIdentity",
        metadata="new metadata",
    ),
)
• Node.js:
Copy code
import { RoomServiceClient } from 'livekit-server-sdk';

const roomServiceClient = new RoomServiceClient('myhost', 'api-key', 'my secret');
roomServiceClient.updateParticipant('room', 'identity', {
    metadata: 'updated metadata',
});
• Go:
Copy code
import (
    "context"
    lksdk "github.com/livekit/server-sdk-go/v2"
)

func updateMetadata() {
    roomClient := lksdk.NewRoomServiceClient(host, apiKey, apiSecret)

    _, err := roomClient.UpdateParticipant(context.Background(), &livekit.UpdateParticipantRequest{
        Room:     "roomName",
        Identity: "participantIdentity",
        Metadata: "new metadata",
    })
}
Just a quick reminder: make sure your server has the
roomAdmin
grant to perform this action. Once the metadata is updated, all connected clients will receive a
ParticipantMetadataChanged
event, keeping everyone in the loop. If you have any questions or if there's a specific scenario you're working on, please don’t hesitate to reach out—I’m here to help! Sources: Managing participants | LiveKit docs | Participant attributes and metadata | LiveKit docs | server/agent.py
r
what if I want to update the agent's metadata? can I do it inside the
entrypoint
?
t
Absolutely, you can update the agent's metadata right inside the
entrypoint
. This is a supported feature and is often used to initialize or tweak the agent-specific state as it runs, which can be really handy. For instance, in the
TextStreamingAgent
example, the agent updates its metadata using the
update_metadata
method like this:
Copy code
async def update_agent_state(self, state: str):
    metadata = json.dumps({"agent_state": state})
    await self.ctx.room.local_participant.update_metadata(metadata)
You can call this method during the agent’s execution, including from within the
start()
method that the
entrypoint
triggers. Just make sure the agent’s token includes the
canUpdateOwnMetadata
permission. You can add this permission using the
allow-update-metadata
flag when generating the token, or programmatically through the video grant. If you’d like, I’m here to help you step through the implementation or answer any questions you have! Sources: Participant attributes and metadata | LiveKit docs | cmd/lk/token.go | livekit-android-sdk/src/main/java/io/livekit/android/room/participant/Local... | Sources/LiveKit/Protocols/ParticipantDelegate.swift | Job lifecycle | LiveKit Docs
r
I want to update metadata on event
user_state_changed
using this template function, how to do that?
Copy code
# setting attributes & metadata are async functions
async def myfunc():
    await room.local_participant.set_attributes({"foo": "bar"})
    await room.local_participant.set_metadata("some metadata")
t
I'd be happy to help you update metadata in response to the
user_state_changed
event using your provided function. Here’s a simple way to approach it: 1. Handle the Event: First, make sure you’re listening for the
user_state_changed
event in your agent session. This event gives you important details about the participant’s state changes, like when they start "speaking" or "listening". 2. Implement Custom Logic: Next, you can write some logic to decide how you want to update the metadata based on those state changes. 3. Update Metadata: Then, use the
set_metadata
method in your function to update the metadata. Here’s a quick example:
Copy code
async def user_state_changed_handler(event):
    if event.new_state == "speaking":
        await room.local_participant.set_metadata("User is speaking")
    elif event.new_state == "listening":
        await room.local_participant.set_metadata("User is listening")
Just a quick note: make sure the participant has the
canUpdateOwnMetadata
permission included in their token so they’re allowed to make these updates. If you’d like, I’m here to help you with any part of this implementation—just let me know! Sources: Events and error handling | LiveKit docs | agent-console/app/_components/participant-action-panel.tsx | Agents v0.x migration guide - Python | LiveKit docs | packages/livekit-rtc/src/proto/room_pb.ts | Participant attributes and metadata | LiveKit docs
r
Cannot register an async callback with
.on()
. Use
asyncio.create_task
within your synchronous callback instead. {"room": "playground-m5XA-TSkk", "pid": 90340, "job_id": "AJ_RvnEvZG8bZSg"}
t
I understand how this error can be a bit confusing! The reason it happens is that LiveKit's Python SDK doesn’t support registering
async
functions directly with
.on()
. But don’t worry, there’s a simple way around this: you can use a synchronous callback and then start your asynchronous logic with
asyncio.create_task()
. Here’s a friendly example to guide you through it:
Copy code
import asyncio
from livekit import rtc

room = rtc.Room()

async def async_handler(event_data):
    # Your async logic here
    await asyncio.sleep(1)
    print(f"Handled event: {event_data}")

@room.on("user_state_changed")
def on_user_state_changed(event_data):
    # Launch async task from synchronous callback
    asyncio.create_task(async_handler(event_data))
Just a quick tip: be sure to register your event listeners before calling
room.connect()
. This way, everything runs smoothly and safely with your asynchronous tasks. If you have any more questions or want me to walk you through this further, I’m here to help! Sources: Knowledge Base Content | livekit/livekit_composite | DeepWiki | livekit-rtc/livekit/rtc/room.py
r
is
_ctx_.room.local_participant.set_metadata(_content_)
the only way to manipulate local participant metadata?
t
I'm happy to help you understand how to manage local participant metadata! The main way to do this is with
ctx.room.local_participant.set_metadata(content)
. But there are a few other helpful methods to keep in mind for managing participant state: 1. Set Attributes: You can use
set_attributes()
to update structured key-value pairs for the participant. This is a bit different from metadata but works nicely alongside it. 2. Set Name: If you want to update the participant's display name,
set_name()
is the way to go. 3. Access Token Initialization: It’s also possible to set metadata, attributes, and name right in the participant’s access token before they even join the room. 4. Server-Side Updates: For server-side changes, the
UpdateParticipant
API lets you modify metadata or attributes smoothly from the server. Just a quick note: all these methods need the right permissions, like
canUpdateOwnMetadata
, and when changes happen, related events such as
ParticipantMetadataChanged
will be triggered. If you have a particular scenario in mind or want me to walk you through anything in more detail, please don’t hesitate to ask—I’m here to help! Sources: livekit-android-sdk/src/main/java/io/livekit/android/room/participant/Parti... | lib/src/participant/local.dart | Sources/LiveKit/Protos/livekit_rtc.pb.swift | livekit/src/room/mod.rs | Runtime/Scripts/Room/Participant/LocalParticipant.cs