Any ways to block an agent conversation until an e...
# ask-ai
f
Any ways to block an agent conversation until an event is received?
t
Thanks for reaching out, Anish. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you block an agent conversation until an event is received, you can use event listeners along with async/await patterns. Here’s a simple example to guide you through it:
Copy code
import 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.py
f
is it possible to just block on user_turn_completed?
t
Thanks for your question! While blocking directly on the
user_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:
Copy code
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 Docs
f
How might an
Agent
signal that it's done with its conversation? Does it make sense to actually make it an
AgentTask
instead?
t
Hi there! An
Agent
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:
Copy code
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.
Copy code
@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