how to pass in user data to system instructions
# ask-ai
a
how to pass in user data to system instructions
t
Thanks for reaching out, Shajahan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to pass user data into system instructions — here are the common options and a short example for each so you can pick which fits your use case. • Job metadata: Send JSON metadata when dispatching the job and read it in the agent entrypoint. Use it to build a system message. ◦ Example (Python):
Copy code
metadata = 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:
Copy code
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 docs
a
AttributeError: 'Assistant' object has no attribute '_activity'
t
You're seeing "AttributeError: 'Assistant' object has no attribute '_activity'" — that means your code is trying to access a private/internal attribute
_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 docs
a
instructions=system_prompt.format(name=self.session.userdata.user_name, user_context=self.session.userdata.user_context)
t
You're formatting the system prompt with session userdata — that's the right idea. A couple quick checks and fixes to avoid the
_activity
error and ensure this works: 1. Confirm
self.session.userdata
exists and has the fields:
Copy code
# 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:
Copy code
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.
a
so I am accessing this in class Assistant(Agent): def __init__(self) -> None super().__init__( instructions=system_prompt.format(name=self.session.userdata.user_name, user_context=self.session.userdata.user_context), )
t
You're instantiating the agent before
self.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)
Copy code
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
Copy code
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