calm-eye-57311
11/10/2025, 11:55 AMtall-airline-16756
11/10/2025, 11:55 AMtall-airline-16756
11/10/2025, 11:56 AM@session.on("user_state_changed") event in LiveKit! This event is really handy because it triggers whenever a user's state changes during a session. It actually brings together the older user_started_speaking and user_stopped_speaking events into one flexible event that captures all the transitions a user might go through.
When this event fires, it gives you a UserStateChangedEvent object that includes:
• `old_state`: The user's previous state
• `new_state`: The user's current state
Here are the possible user states you might see:
• `speaking`: When the user starts speaking
• `listening`: When the user stops speaking
• `away`: When the user is idle (by default, after 15 seconds, but you can adjust this with user_away_timeout)
About the "away" state — it’s triggered when both the user and agent are in the "listening" state. If either one changes state, the timer resets. You also have the flexibility to customize or even disable this timeout when you initialize the AgentSession.
Here’s a simple example to handle idle users:
@session.on("user_state_changed")
def _user_state_changed(ev: UserStateChangedEvent):
nonlocal inactivity_task
if ev.new_state == "away":
inactivity_task = asyncio.create_task(user_presence_task())
return
if inactivity_task is not None:
inactivity_task.cancel()
This event is really important because it helps you keep track of when users start and stop speaking, and also manage those quiet moments in conversations. If you have any questions or want to dive deeper into how to use this, feel free to ask — I'm here to help!
Sources: Agents v0.x migration guide - Python | LiveKit docs | Events and error handling | LiveKit docs | livekit-agents/livekit/agents/voice/room_io/room_io.pycalm-eye-57311
11/10/2025, 12:00 PMtall-airline-16756
11/10/2025, 12:01 PM@session.on("user_state_changed") event might not be triggering, and that can happen for a few different reasons. Let’s walk through some common causes and helpful debugging steps together:
1. No Audio Input or VAD Not Running: First, please make sure the user is actually publishing audio and that the Voice Activity Detection (VAD) module is up and running. Double-check that the vad parameter is properly passed when you initialize the AgentSession.
2. Incorrect Turn Detection Mode: It’s important to explicitly set the turn_detection mode to "vad" when creating your session. This ensures that VAD is the one driving the state changes: session = AgentSession(vad=vad_instance, turn_detection="vad", ...)
3. Event Listener Not Properly Attached: Make sure your event handler is registered before you start the session. Here’s a quick example: @session.on("user_state_changed")
def on_user_state_changed(ev):
print(f"User state changed to: {ev.new_state}")
await session.start(agent=agent, room=room)
4. User Away Timeout Suppression: Check that the user_away_timeout isn’t set to None unless that’s what you intend. By default, it’s set to 15 seconds.
5. State Not Actually Changing: Remember, the event only fires when there’s a real state change (like switching from listening to speaking). So, verify that VAD is detecting actual speech.
6. Session or Agent Not Fully Initialized: Lastly, confirm that session.start() is called and awaited before you expect any events to come through.
Checking these areas usually helps pinpoint why the event might not be triggering. If you’d like, I’m here to help you dive deeper into any of these steps—just let me know!
Sources: Events and error handling | LiveKit docs | tests/test_agent_session.py