astonishing-salesclerk-9701
12/13/2025, 2:15 PMtall-airline-16756
12/13/2025, 2:15 PMtall-airline-16756
12/13/2025, 2:16 PMmetadata = json.loads(ctx.job.metadata)
initial_ctx = llm.ChatContext().append(
role="system",
text=f"The user's name is {metadata['user_name']}."
)
• Agent Builder variables: Put placeholders in instructions like The user's name is {{metadata.user_name}}. The builder will substitute values from job metadata automatically.
• Participant attributes: Read participant attributes (e.g., participant.attributes.get("user.language")) and append a system instruction (e.g., Respond in {language}.).
• Session `userdata`: Store session-wide structured state and reference it when generating instructions.
◦ Example:
await self.session.generate_reply(
instructions=f"Welcome back, {self.session.userdata.user_name}"
)
Which method are you planning to use (job metadata, Agent Builder, participant attributes, or session userdata)? I can provide a focused code sample for that approach.
Sources: Job lifecycle | LiveKit docs | Agent Builder | LiveKit docs | Agents and handoffs | LiveKit docsastonishing-salesclerk-9701
12/13/2025, 2:17 PMtall-airline-16756
12/13/2025, 2:17 PM_activity on the agent instance, which isn't exposed. I can help fix this, but I need a bit more detail.
Please share:
1. The code snippet where the error happens (the method or line referencing _activity).
2. How you instantiate/use the Assistant (agent) class.
3. The full stack trace (or at least the top few lines).
Meanwhile, common fixes you can try:
• Stop accessing self._activity. Use public session APIs instead:
◦ Use self.session.generate_reply() to produce agent replies.
◦ Use self.update_instructions() to change behavior dynamically.
• If you intended to inspect agent state, listen for session events like agent_state_changed rather than reading internal attributes.
Send the snippet and stack trace and I’ll point out the exact change.
Sources: Agents (Python) | livekit/livekit_composite | DeepWiki | Agents and handoffs | LiveKit docs | Building an Automated IVR Menu Caller | LiveKit docs | Agent session | LiveKit docs | Events and error handling | LiveKit docsastonishing-salesclerk-9701
12/13/2025, 2:18 PMtall-airline-16756
12/13/2025, 2:18 PM_activity error and ensure this works:
1. Confirm self.session.userdata exists and has the fields:
# defensive check
if not getattr(self, "session", None) or not getattr(self.session, "userdata", None):
raise RuntimeError("session.userdata is not set")
name = getattr(self.session.userdata, "user_name", None)
context = getattr(self.session.userdata, "user_context", None)
1. Use safe formatting to avoid KeyError/AttributeError:
instructions = system_prompt.format(
name=name or "there",
user_context=context or ""
)
1. Don’t access self._activity. If you need to emit a reply or change instructions, use public APIs:
• Generate reply: await self.session.generate_reply(instructions=instructions)
• Update behavior: await self.update_instructions(instructions)
If you still get the _activity error, please paste the small code block where you call format(...) and the full stack trace so I can point to the exact line causing the attribute access.astonishing-salesclerk-9701
12/13/2025, 2:22 PMtall-airline-16756
12/13/2025, 2:23 PMself.session exists. self.session (and therefore self.session.userdata) is not available inside __init__, so calling system_prompt.format(...) there causes the AttributeError.
Two correct options:
1) Delay using session.userdata until the session is active (recommended)
class Assistant(Agent):
def __init__(self) -> None:
super().__init__(instructions="Placeholder")
async def on_enter(self) -> None:
name = self.session.userdata.user_name
context = self.session.userdata.user_context
instructions = system_prompt.format(name=name, user_context=context)
await self.update_instructions(instructions)
2) Pass userdata into the constructor when you create the agent
class Assistant(Agent):
def __init__(self, user_name: str, user_context: str) -> None:
instructions = system_prompt.format(name=user_name, user_context=user_context)
super().__init__(instructions=instructions)
# elsewhere, before starting session:
agent = Assistant(
user_name=session.userdata.user_name,
user_context=session.userdata.user_context
)
await session.start(agent=agent, room=room)
Which approach would you prefer? If you share the agent creation code or how you start the session, I can give the exact change.
Sources: Agents and handoffs | LiveKit docs | Building an Automated IVR Menu Caller | LiveKit docs | Job lifecycle | LiveKit docs | External data and RAG | LiveKit Docs