How to send user's custom data to agent session ?
# ask-ai
f
How to send user's custom data to agent session ?
t
Thanks for reaching out, Munkhbaysgalan. 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 send custom data from a user to an agent session in LiveKit, there are a few ways you can do it depending on what you need: • Job Metadata: You can pass custom data right when you start the dispatch using the
metadata
field. This works great for things like user profiles or setting up the session context. • AgentSession.userdata: This
userdata
attribute lets you keep session-specific info that you can update as you go. It’s type-safe and perfect for maintaining state during the session. • RPC (Remote Procedure Calls): If you want real-time, two-way communication—like submitting forms or triggering actions—RPC is the way to go. • Data Messages: Using the
publishData
API, you can send real-time updates or even transfer files. It supports both reliable and lossy data transfer, depending on what you need. • Participant Attributes: This lets you set and get user-specific settings, like preferences or language options. Each method fits different situations, so if you want, I’d be happy to walk you through how to set up any of these in detail. Just let me know! Sources: Workflows | LiveKit Docs | Web and mobile frontends | LiveKit Docs | complex-agents/turn-taking/agent.py | Data messages | LiveKit Docs | agent-console/app/api/room/send-data/route.ts
f
how to send participant attibutes and how to access in agent ?
t
Here’s a simple way to send participant attributes and access them during an agent session: • Using Client SDKs: Participants can easily update their own attributes with methods like
setAttributes()
. For example:
Copy code
await room.local_participant.set_attributes({"user.language": "en", "user.role": "speaker"})
• Using Server APIs: You can also set attributes through the RoomServiceClient. For example:
Copy code
const roomServiceClient = new RoomServiceClient('myhost', 'api-key', 'my secret');
roomServiceClient.updateParticipant('room', 'identity', {
  attributes: {
    'user.language': 'es',
    'user.role': 'moderator',
  },
});
• Accessing Attributes in an Agent: Inside an agent, you can grab participant attributes via the
JobContext
and the connected participant object. Here’s how:
Copy code
async def entrypoint(ctx: JobContext):
    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
    participant = await ctx.wait_for_participant()
    language = participant.attributes.get("user.language")
    role = participant.attributes.get("user.role")
    print(f"Language: {language}, Role: {role}")
If you have any questions or want a hand with the implementation, just let me know—I’m here to help! Sources: Participant attributes and metadata | LiveKit Docs | Participant attributes and metadata | LiveKit Docs | examples/participant_attributes.py | Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs
f
In typescript ?
t
Hey there! If you want to send and access participant attributes in TypeScript for LiveKit, here’s a simple way to do it: • Sending Participant Attributes: Just use the
setAttributes
method on your
localParticipant
like this:
Copy code
room.localParticipant.setAttributes({
  myKey: 'myValue',
  myOtherKey: 'otherValue',
});
• If you ever want to delete an attribute, just set its value to an empty string:
Copy code
room.localParticipant.setAttributes({
  myKey: '', // This will delete the attribute
});
• Accessing Participant Attributes: You can grab the
attributes
from any
Participant
object like this:
Copy code
const attributes = participant.attributes;
console.log(attributes['myKey']);
• And if you want to keep an eye on attribute changes, just listen for the
ParticipantAttributesChanged
event:
Copy code
room.on(RoomEvent.ParticipantAttributesChanged, (changed, participant) => {
  console.log('participant attributes changed', changed);
  console.log('all attributes', participant.attributes);
});
Feel free to reach out if you want me to walk you through more examples or if you have any questions! Sources: Participant attributes and metadata | LiveKit Docs | Participant attributes and metadata | LiveKit Docs | packages/core/src/observables/participant.ts