In this python code snippet below which is generat...
# ask-ai
v
In this python code snippet below which is generating a livekit token - the
with_attributes
refers to participant attributes or room attributes? and how do I later access this attribute in a tool cool?
Copy code
token = api.AccessToken(os.getenv('LIVEKIT_API_KEY'), os.getenv('LIVEKIT_API_SECRET')) \
        .with_identity("identity") \
        .with_name("name") \
        .with_grants(api.VideoGrants(room_join=True, room=room_name)) \
        .with_attributes({"user_name": "name", "group_id": "100"})
t
Thanks for reaching out, Ruchir. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I’d love to help clarify this for you! The
with_attributes
method in your code snippet actually refers to participant attributes, not room attributes. These attributes are key-value pairs tied to a participant, which you can use to store any custom metadata you need. When you want to access these attributes later on, you can simply use the
attributes
property of the participant object. Here’s a quick example:
Copy code
# Access attributes of a participant
participant_attributes = participant.attributes
Also, if you want to keep track of any changes to participant attributes, you can listen for the
participant_attributes_changed
event like this:
Copy code
@room.on("participant_attributes_changed")
def on_participant_attributes_changed(changed_attributes, participant):
    print("Attributes changed:", changed_attributes)
    print("Current attributes:", participant.attributes)
If anything feels unclear or if you’d like me to walk you through it step-by-step, just let me know—I’m here to help! Sources: Core Platform Architecture & Authentication | livekit/livekit_composite | D... | livekit-rtc/livekit/rtc/_proto/data_stream_pb2.pyi | Participant attributes and metadata | LiveKit Docs
v
Copy code
participant_attributes = participant.attributes
I didn't quite follow this code snippet - could you give me a sample of how to get the participant attribute within a tool call? do I have to use the run context? is that the most latency optimised method? any other method
t
Thanks for reaching out! Just to make sure I’m on the right track, are you trying to access LiveKit’s synchronized participant attributes (the ones set using
with_attributes
) or some custom session data stored in the RunContext? Also, would you like this data to be shared and synchronized across all participants in the room, or is it something specific to your current session? I’m here to help you get this sorted out! Sources: packages/react/src/hooks/useParticipantAttributes.ts | Runtime/Scripts/Participant.cs | Participant attributes and metadata | LiveKit Docs | examples/voice_agents/realtime_joke_teller.py | basics/tool_calling.py
v
so on I generate the token on the server using the code snippet below:
Copy code
token = api.AccessToken(os.getenv('LIVEKIT_API_KEY'), os.getenv('LIVEKIT_API_SECRET')) \
        .with_identity("identity") \
        .with_name("name") \
        .with_grants(api.VideoGrants(room_join=True, room=room_name)) \
        .with_attributes({"user_name": "name", "group_id": "100"})
so I want to access the group_id I set (using the with_attributes..) within a tool call in python
t
To help you access the
group_id
attribute set using
with_attributes
in your token, you can retrieve it from the participant's attributes during a tool call. Let me walk you through how you can do this: Example:
Copy code
@function_tool
def get_group_id(context: RunContext):
    # Access the current room
    room = context.room

    # Access local participant attributes
    local_attrs = room.local_participant.attributes
    print("Local participant attributes:", local_attrs)

    # Retrieve the 'group_id' attribute
    group_id = local_attrs.get("group_id")
    print("Group ID:", group_id)

    return group_id
Key Notes: • Attributes are stored as key-value pairs in the
attributes
property of the participant object. • Make sure the participant has the
canUpdateOwnMetadata
permission in their access token to update or access attributes. • You can also access attributes of remote participants using
room.remote_participants
. If you have any questions or need further clarification, please don’t hesitate to ask—I’m here to help! Sources: livekit-rtc/livekit/rtc/room.py | Participant attributes and metadata | LiveKit Docs | telephony/sip_lifecycle.py | ivr-nagivator/README.mdoc
v
getting this error:
AttributeError: 'RunContext' object has no attribute 'room'
t
It looks like you're encountering the error
AttributeError: 'RunContext' object has no attribute 'room'
, which usually means the room object isn't directly accessible from the
RunContext
. A good way to handle this is by accessing the room through the
userdata.ctx
property or a similar context setup. Here's a refreshed example that might help:
Copy code
@function_tool
def get_group_id(context: RunContext[UserData]):
    # Access userdata context
    userdata = context.userdata

    # Check if context and room are available
    if not userdata.ctx or not userdata.ctx.room:
        return "Error: Room context is not available."

    room = userdata.ctx.room

    # Access local participant attributes
    local_attrs = room.local_participant.attributes
    group_id = local_attrs.get("group_id")

    return group_id
If your setup doesn’t include
userdata.ctx
, no worries—just double-check how your tool is configured to access the room or participants. Feel free to reach out if you’d like me to walk you through it or if you have any other questions. I’m here to help! Sources: context-readmes/USERDATA_GUIDE.md | livekit-agents/livekit/agents/job.py