freezing-jelly-85538
10/15/2025, 11:13 PMtall-airline-16756
10/15/2025, 11:13 PMtall-airline-16756
10/15/2025, 11:13 PMimport asyncio
# Create an event object to wait on
event_received = asyncio.Event()
@session.on("conversation_item_added")
def on_conversation_item_added(item):
# Process the item and set the event to unblock waiting code
print(f"Received item: {item}")
event_received.set()
# Block until the event is received
await event_received.wait()
# Conversation continues after event is received
This method helps ensure your conversation flow pauses just right until the event happens. If you have any questions or need a hand with anything else, please don’t hesitate to ask—I’m here to help!
Sources: Events and error handling | LiveKit Docs | tests/test_agent_session.py | Agent speech and audio | LiveKit Docs | agent.pyfreezing-jelly-85538
10/15/2025, 11:20 PMtall-airline-16756
10/15/2025, 11:20 PMuser_turn_completed event isn’t supported, you can still guide the agent’s behavior during this event by using the on_user_turn_completed callback. For instance, you might raise a StopResponse exception to stop the agent from replying, like this:
async def on_user_turn_completed(self, turn_ctx, new_message):
if new_message.text_content.strip().lower() == "ignore":
raise StopResponse() # Agent will not respond
Another approach is to adjust the chat context or add some conditional logic to control how the agent responds. If you’d like, I’m here to help with more details or examples—just let me know!
Sources: Pipeline nodes and hooks | LiveKit Docs | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | External data and RAG | LiveKit Docs | Agent speech and audio | LiveKit Docs | Logs, metrics, and telemetry | LiveKit Docsfreezing-jelly-85538
10/16/2025, 3:19 AMAgent signal that it's done with its conversation? Does it make sense to actually make it an AgentTask instead?tall-airline-16756
10/16/2025, 3:20 AMAgent can let you know it's done with its conversation in a few different ways, depending on how your workflow is set up. Let me walk you through them:
• Using `AgentTask`: This is great for task-oriented workflows. You can signal completion by calling the complete(result) method. Here's an example:
class MyTask(AgentTask[str]):
async def on_enter(self):
await self.session.generate_reply("Task completed successfully")
self.complete("Task finished with success")
# Usage
task = MyTask(instructions="Complete the analysis")
result = await task # Receives "Task finished with success"
• Using Function Tools: Another way is to create a function tool that marks natural stopping points, like ending the session or saying goodbye to the user.
@function_tool
async def conversation_finished(self, context: RunContext):
self.session.interrupt()
await self.session.generate_reply("Goodbye!", allow_interruptions=False)
await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx.room.name))
• Session Closure: You can also close the session for the agent or everyone involved by using methods like session.shutdown() or session.aclose().
Using AgentTask is especially helpful if you want a structured workflow with clear result handling. If you'd like, I’m here to help explain any of these options in more detail—just let me know!
Sources: examples/voice_agents/multi_agent.py | Job lifecycle | LiveKit Docs | How to capture real-time agent transcriptions during conversations | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki