How can I access the attributes passes to a job? `...
# ask-ai
a
How can I access the attributes passes to a job?
Copy code
const requestFunc = async (req: JobRequest) => {
  req.accept('test1', 'test2', attributes));
};

cli.runApp(new ServerOptions({ agent: fileURLToPath(import.meta.url), agentName: 'myAgent', requestFunc }));
t
Thanks for reaching out, Ahron. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you’re asking how to access the attributes you pass when accepting a job. You can read those attributes from the agent’s participant after connecting to the room on the JobContext. Key points: • Attributes passed into
req.accept(...)
are applied to the agent’s participant in the room. • After your agent connects, read them from
ctx.room.localParticipant.attributes
(Node.js) or
ctx.room.local_participant.attributes
(Python). Example (Node.js / TypeScript):
Copy code
import { defineAgent } from '@livekit/agents';

const requestFunc = async (req: JobRequest) => {
  await req.accept('test1', 'test2', {
    user_language: 'en',
    custom_field: 'value',
  });
};

export default defineAgent({
  entry: async (ctx: JobContext) => {
    // join/connect the room
    await ctx.connect();

    // attributes set during accept are available here
    const attributes = ctx.room.localParticipant.attributes;

    // read all attributes
    for (const [k, v] of Object.entries(attributes)) {
      console.log(`Attribute ${k}: ${v}`);
    }

    // read a specific attribute
    const language = attributes['user_language'];
    console.log(`User language: ${language}`);
  },
  requestFunc,
});
Example (Python):
Copy code
async def request_fnc(req: JobRequest):
    await req.accept(
        name="test1",
        identity="test2",
        attributes={
            "user_language": "en",
            "custom_field": "value"
        }
    )

@server.rtc_session(agent_name="my-agent", on_request=request_fnc)
async def my_agent(ctx: JobContext):
    await ctx.connect()

    attributes = ctx.room.local_participant.attributes
    language = attributes.get("user_language")
    print(f"User language: {language}")
If the attributes aren’t present where you expect them: • Confirm you call
await ctx.connect()
before reading
ctx.room.localParticipant
. • Confirm the attributes object passed to
req.accept()
is correctly formed (plain object/dict). • Tell me which language/runtime you’re using and a short snippet of your agent’s entry function if you want me to check it. Sources: Job lifecycle | LiveKit Documentation
a
passing attributes will override any existing attributes already set?
t
Thanks — you’re asking whether attributes passed to
req.accept()
will override any existing participant attributes. Short answer: •
req.accept()
sets the participant’s initial attributes when the agent joins the room. • It does not retroactively override attributes set later during the session. After joining, use the participant API to update attributes (e.g.,
room.localParticipant.setAttributes()
), which updates individual keys as needed. Details to keep in mind: •
req.accept()
establishes the starting attribute map for that job’s agent when it joins. • Later calls to set attributes will modify or replace specific keys depending on the method used; they don’t get magically merged by
req.accept()
after connection. • You generally call
req.accept()
once per job, so there isn’t a typical scenario of calling it multiple times on the same participant within one job. If you want, tell me: • Which runtime you’re using (Node or Python), and • Whether you need an example showing how to update attributes after join and I’ll provide a short code snippet. Sources: Server options | LiveKit Documentation | Build a multilingual voice agent that automatically switches languages | Li... | Job lifecycle | LiveKit Documentation
a
Isn't it JobAcceptArguments
t
Good question — yes, the arguments you pass to
req.accept()
are defined by the
JobAcceptArguments
type. That type includes fields such as
name
,
identity
, and an optional
attributes
object for participant attributes. What that means in practice: • When you call
req.accept(...)
you should supply values that match
JobAcceptArguments
(for example
name
,
identity
, and
attributes
). • The
attributes
you pass here become the participant’s initial attributes when the agent joins the room. • After joining, you can update attributes via the participant API (for example
room.localParticipant.setAttributes()
), which modifies attribute keys during the session. If you want, I can show a concise typed example showing
JobAcceptArguments
usage in your runtime (Node/TypeScript or Python). Which one would you like?
a
no that is only oin the job info object
t
Thanks — you’re right to call that out. Let me clarify the distinction. •
JobAcceptArguments
is the typed shape used when accepting a job (the arguments you pass to
req.accept()
). • The job info (often
req.job
or similar) is a separate object describing the job metadata returned from the server. That job info contains fields such as the job’s id, status, and the original attributes sent with the job request. Key point: • The attributes you pass into
req.accept(..., attributes: {...})
set the agent participant’s initial attributes when it joins the room. • The job info object contains the job’s attributes as part of its metadata, but that object is not the same as
JobAcceptArguments
. Passing
JobAcceptArguments
to
req.accept()
does not modify the job info object on the server — it sets how the agent joins the room for that job. If helpful, I can: • Show the exact TypeScript type for
JobAcceptArguments
and the shape of the job info object, or • Give a short Node/Python snippet showing where job info vs. accept-arguments live in the request lifecycle Which would you prefer?