can I define session events (e.g., session.on("use...
# ask-ai
f
can I define session events (e.g., session.on("user_input_transcribed")) within Agent definition?
t
Thanks for reaching out, Taras. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can define session events like
session.on("user_input_transcribed")
right within your Agent definition in LiveKit. This is a wonderful feature of the LiveKit Agents framework that lets you handle user input transcriptions in real-time, making your agents truly interactive. For instance, if you're working with Python, you can use the
@session.on("user_input_transcribed")
decorator to set up an event handler like this:
Copy code
from livekit.agents.voice import AgentSession
from livekit.plugins import deepgram

session = AgentSession()

@session.on("user_input_transcribed")
def on_transcript(event):
    if event.is_final:
        print(f"Final transcript: {event.transcript}")
        session.say(event.transcript)

await session.start(
    agent=Agent(stt=deepgram.STT()),
    room=ctx.room
)
And if you’re using Node.js, here’s a similar approach:
Copy code
import { AgentSession } from '@livekit/agents';
import { DeepgramSTT } from '@livekit/plugins-deepgram';

const session = new AgentSession();

session.on("user_input_transcribed", (event) => {
    if (event.is_final) {
        console.log(`Final transcript: ${event.transcript}`);
        session.say(event.transcript);
    }
});

await session.start({
    agent: new Agent({ stt: new DeepgramSTT() }),
    room
});
The
user_input_transcribed
event comes with helpful properties like
transcript
,
is_final
, and you can also access
language
and
speaker_id
if needed. This flexibility really empowers you to create responsive and engaging agents. If you have any questions or want to explore this further, I’m here to help! Sources: translators/tts_translator.py | Events and error handling | LiveKit Docs
f
I mean inside my Agent class
t
I completely understand how this can be a bit confusing at first! Session events like
session.on("user_input_transcribed")
aren’t meant to be defined directly inside the
Agent
class. Instead, they work best when you attach listeners to the
AgentSession
instance, since these events are all about the session level. Here’s a friendly example to illustrate how you can define the event listener outside the
Agent
class:
Copy code
session = AgentSession()

@session.on("user_input_transcribed")
def on_transcript(event):
    if event.is_final:
        session.say(event.transcript)
Another approach is to register event listeners dynamically inside the
Agent
class during runtime—maybe in an initialization or setup method—by using
self.session
. Just a heads up, this isn’t the usual way and is a bit less common. If you’d like me to walk you through this with more examples or if anything feels unclear, please don’t hesitate to ask. I’m here to help! Sources: Events and error handling | LiveKit Docs | translators/tts_translator.py | livekit-agents/livekit/agents/voice/agent_session.py