enough-match-43171
12/23/2025, 5:27 PMtall-airline-16756
12/23/2025, 5:27 PMtall-airline-16756
12/23/2025, 5:27 PMon_enter and on_exit methods "don't seem to work" in Python. You're right to define them as coroutines; here are the most likely reasons they never run and how to check each one.
1) on_enter only runs when the agent becomes active
• The hook is triggered when the agent actually becomes the active agent in a session (e.g., session starts with this agent or control is handed to it). If the agent never becomes active, on_enter won't be called.
• Confirm you start the session with the agent and await it:
await session.start(agent=MyAgent(), room=room)
2) on_exit only runs on a handoff (not on shutdown)
• on_exit is called when control is transferred to another agent (e.g., via a tool that returns another agent or session.update_agent(...)), not when the session simply ends. If you stop the session without a handoff, on_exit will not be invoked.
3) Missing awaits or session flow issues
• Make sure you await lifecycle actions and any internal async calls inside the hooks. A missing await won’t prevent the hook from being scheduled, but it can cause expected behavior (like replies or TTS) to appear broken.
async def on_enter(self):
print("### Entered the agent session ###")
await self.session.generate_reply("Welcome")
Quick tests to confirm behavior
1. Start session with the agent and watch console for the on_enter print.
2. Trigger a handoff to another agent (or call await session.update_agent(AnotherAgent())) and watch for on_exit.
3. Add debug prints at session start/hand-off points to ensure control reaches the agent.
If you share how you start the session and whether you perform any handoff/update calls, I can point to the exact place your flow needs to change.
Sources: Pipeline nodes and hooks | LiveKit docs | Agents and handoffs | LiveKit docsenough-match-43171
12/23/2025, 5:29 PMtall-airline-16756
12/23/2025, 5:29 PMawait session.start(...), let's narrow this down.
Could you share two things so I can diagnose the lifecycle behavior?
1. The agent class (the code for agent you pass to start), including on_enter / on_exit definitions and any tool methods that might return another agent.
2. How/where the session runs after start — e.g., do you ever call session.update_agent(...), return another agent from a tool, or stop the session? If you never hand off control, on_exit will not be called.
If you prefer, paste a minimal runnable snippet that shows how you create the session, start it, and any handoff/update logic. I’ll point out exactly where the hooks should fire or why they aren’t.enough-match-43171
12/23/2025, 5:29 PMtall-airline-16756
12/23/2025, 5:30 PMon_enter never runs.
Please paste the minimal code for the agent you pass to session.start, including:
• The class definition (base class / imports)
• Your on_enter method body
• Any tool methods on that agent that might be run at start
• How you instantiate agent before calling await session.start(...)
Also tell me:
• Are there any exceptions printed to console when you call await session.start(...)?
• Is the call to await session.start(...) running inside an existing event loop/task (e.g., inside an async command handler), or is it being awaited at the top-level?
With that snippet and answers I can point to the exact reason on_enter isn't firing and a concrete fix.