The type of `self.session` in the agent is `sessio...
# ask-ai
b
The type of
self.session
in the agent is
session: AgentSession[Unknown]
, but I want it to be
AgentSession[UserData]
.
t
Thanks for reaching out, RikuKomiya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It sounds like you want your agent’s
session
to carry a typed userdata so it’s
AgentSession[UserData]
instead of
AgentSession[Unknown]
. You can do this by: • Defining your userdata type, then • Passing it when constructing the session and annotating the session with that type. Userdata is then available as
session.userdata
and in tools via
RunContext
. Could you share: • Which SDK you’re using (Python or TypeScript)? • The snippet where you construct or store
self.session
? I’ll provide the exact annotation/constructor syntax for your SDK. For reference on userdata with sessions, see the docs: Workflows | LiveKit Docs and Building voice agents | LiveKit Docs. Sources: Workflows | LiveKit Docs | Building voice agents | LiveKit Docs
b
How would you implement this code?
class ChatAgent(BaseAgent):
@override
async def on_enter(_self_) -> None:
@self.session.on("agent_state_changed")  _# pyright: ignore[reportUntypedFunctionDecorator, reportUnknownMemberType]_
def _on_agent_state_changed(_event_: AgentStateChangedEvent) -> None:  _# pyright: ignore[reportUnusedFunction]_
asyncio.create_task(self.on_agent_state_changed(event))  _# pyright: ignore[reportUnusedCallResult]_
return
_await_ super().on_enter()
async def on_agent_state_changed(_self_, _ev_: AgentStateChangedEvent) -> None:
ss: AgentSession[UserData] = self.session
_if_ ev.new_state == "listening"
ctx = get_job_context()
_ = _await_ ctx.api.room.delete_room(DeleteRoomRequest(_room_=ctx.room.name))
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!