inside event how can i ask agent to speak eg after...
# ask-ai
a
inside event how can i ask agent to speak eg after user transcript created
t
Thanks for reaching out, Arun. 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 have the agent speak when a user transcript is created — for example, trigger the agent to reply right after a
user_input_transcribed
(or
conversation_item_added
) event. Two good options: • Use
session.generate_reply()
to have the LLM produce a context-aware response (best for dynamic replies). • Use
session.say()
to speak a static/predefined message. Do you want a dynamic LLM-generated reply or a fixed TTS message? Also tell me which SDK you're using (Node.js or Python) and whether you need interruptions allowed (e.g.,
allowInterruptions: true
) so I can send a short code snippet tailored to your setup. Sources: Agent speech and audio | LiveKit docs
a
session.say how can i call cause the event is sync right ?
t
You're right to question that —
session.say()
is asynchronous, but you can call it from a synchronous event handler by scheduling or awaiting it depending on your environment. Which SDK are you using so I can give a short example? • Node.js — you can make the handler
async
and
await session.say(...)
, or call
session.say(...)
without await (it returns a Promise). • Python — if the handler is synchronous, use `asyncio.create_task(session.say(...))`; if the handler is async,
await session.say(...)
. Tell me which one you want and whether you need the agent to allow interruptions (e.g.,
allowInterruptions: true
), and I’ll send a concise code snippet. Sources: Events and error handling | LiveKit docs | Agent speech and audio | LiveKit docs
a
2025-11-23 160724,085 - ERROR livekit.agents - unhandled exception while running the job task Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/opentelemetry/util/_decorator.py", line 71, in async_wrapper return await func(*args, **kwargs) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/livekit/agents/ipc/job_proc_lazy_main.py", line 240, in _traceable_entrypoint await self._job_entrypoint_fnc(job_ctx) File "/app/main.py", line 324, in entrypoint @session.on("user_input_transcribed") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/livekit/rtc/event_emitter.py", line 172, in decorator self.on(event, callback) File "/usr/local/lib/python3.11/site-packages/livekit/rtc/event_emitter.py", line 161, in on raise ValueError( ValueError: Cannot register an async callback with
.on()
. Use
asyncio.create_task
within your synchronous callback instead.
t
You're hitting the ValueError because
session.on(...)
in the Python SDK requires a synchronous callback. Inside that sync callback you should schedule the async work (like
session.say()
or
session.generate_reply()
) with
asyncio.create_task()
so it runs on the event loop without making the callback itself
async
. Try this pattern:
Copy code
import asyncio

@session.on("user_input_transcribed")
def on_user_input_transcribed(event):
    if event.is_final:
        # schedule the async call (won't block the handler)
        asyncio.create_task(session.say("Thanks — I'll respond now!", allow_interruptions=True))
If you already have async helper logic you want to run, wrap it similarly:
Copy code
async def handle_transcript(event):
    await session.generate_reply(instructions="Respond warmly to the user.")

@session.on("user_input_transcribed")
def on_user_input_transcribed(event):
    if event.is_final:
        asyncio.create_task(handle_transcript(event))
Notes: • Ensure an event loop is running in the process (typical for LiveKit agents). • Use
allow_interruptions=True
if you want the TTS to be interruptible. Would you like a short variant that logs or awaits the
SpeechHandle
result inside the background task?