ancient-honey-53923
10/06/2025, 11:03 AMtall-airline-16756
10/06/2025, 11:03 AMtall-airline-16756
10/06/2025, 11:04 AMancient-honey-53923
10/06/2025, 11:59 AMtall-airline-16756
10/06/2025, 12:01 PMimport asyncio
import logging
from livekit import rtc
from livekit.agents.vad import VADEventType
from livekit.plugins import silero
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("vad-example")
async def main():
# Load the Silero VAD plugin
vad = silero.VAD.load(
min_speech_duration=0.2, # Minimum duration of speech to trigger VAD
min_silence_duration=0.6 # Minimum silence duration to end speech
)
vad_stream = vad.stream()
async def process_audio():
# Simulate audio input (replace with actual audio frames in real use)
for _ in range(100):
frame = rtc.AudioFrame(data=b"\x00" * 480, sample_rate=48000, num_channels=1)
vad_stream.push_frame(frame)
await asyncio.sleep(0.01) # Simulate real-time audio input
async def process_vad():
async for vad_event in vad_stream:
if vad_event.type == VADEventType.START_OF_SPEECH:
logger.info("VAD: Start of speech detected")
elif vad_event.type == VADEventType.END_OF_SPEECH:
logger.info("VAD: End of speech detected")
# Run audio processing and VAD detection concurrently
await asyncio.gather(process_audio(), process_vad())
if __name__ == "__main__":
asyncio.run(main())
Here are some key things to keep in mind:
• VAD Events: This example listens for START_OF_SPEECH and END_OF_SPEECH events to help you track speech activity.
• Plugin: It uses the Silero VAD plugin, which is a reliable tool for detecting speech.
• Configuration: You can tweak min_speech_duration and min_silence_duration to adjust how sensitive the detection should be, depending on your needs.
If you have any questions or would like me to guide you through a more advanced example, please don’t hesitate to ask. I’m here to help!
Sources: livekit-agents/livekit/agents/voice/audio_recognition.py | Improving voice AI's turn detection with transformers | Silero VAD plugin | LiveKit Docs | metrics/metrics_vad.pyancient-honey-53923
10/06/2025, 12:19 PMtall-airline-16756
10/06/2025, 12:19 PMRecognitionHooks protocol. Here’s a friendly example to guide you through defining and using these handlers:
from livekit.agents.vad import VADEvent
from livekit.agents.voice import RecognitionHooks
class MySpeechHandler(RecognitionHooks):
def on_start_of_speech(self, ev: VADEvent | None) -> None:
print("Speech started")
if ev:
print(f"Detected at {ev.timestamp}, speech duration: {ev.speech_duration}s")
def on_end_of_speech(self, ev: VADEvent | None) -> None:
print("Speech ended")
if ev:
print(f"Ended at {ev.timestamp}, silence duration: {ev.silence_duration}s")
# Example integration with a VoicePipelineAgent
from livekit.agents.pipeline import VoicePipelineAgent
from livekit.plugins import silero, deepgram, openai
vad_instance = silero.VAD.load()
stt_instance = deepgram.STT()
llm_instance = openai.LLM()
tts_instance = openai.TTS()
speech_handler = MySpeechHandler()
agent = VoicePipelineAgent(
vad=vad_instance,
stt=stt_instance,
llm=llm_instance,
tts=tts_instance,
hooks=speech_handler # Register your custom hooks here
)
# Start the agent (example context setup required)
# await agent.start(ctx.room, participant)
Here are some key things to keep in mind:
• on_start_of_speech: This triggers right when speech begins, giving you useful info like timestamp and how long the speech has lasted.
• on_end_of_speech: This kicks in when speech ends, letting you know the timestamp and the duration of silence afterward.
• By integrating your handler into a VoicePipelineAgent, you ensure these VAD events are routed smoothly to your custom methods.
If you have any questions or want help customizing this further, please don’t hesitate to ask—I’m here to support you!
Sources: Events and error handling | LiveKit Docs | livekit-agents/livekit/agents/voice/audio_recognition.py | Improving voice AI's turn detection with transformers | Improving voice AI's turn detection with transformersancient-honey-53923
10/06/2025, 1:56 PMtall-airline-16756
10/06/2025, 1:56 PMancient-honey-53923
10/06/2025, 1:57 PMtall-airline-16756
10/06/2025, 1:57 PM# Force interruption even if allow_interruptions=False
session.interrupt()
Here are some key parameters to keep in mind:
• allow_interruptions: This controls whether user speech can interrupt the agent’s speech.
• min_interruption_duration: The minimum length of user speech needed before an interruption happens.
• false_interruption_timeout: How long to wait before resuming playback if there’s a brief pause without more speech.
If you’re working with ElevenLabs TTS, just double-check that your integration supports pausing and matches LiveKit’s interruption approach. And please feel free to reach out if you’d like a hand verifying or customizing your ElevenLabs setup—I’m here to help!
Sources: Turn detection and interruptions | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.pyancient-honey-53923
10/06/2025, 1:59 PMtall-airline-16756
10/06/2025, 2:00 PMsession = AgentSession(
allow_interruptions=True,
min_interruption_duration=0.5,
min_interruption_words=1
)
Manual Interruption: And if you ever need to force an interruption right away, you can do so with:
# Force interruption regardless of allow_interruptions setting
await session.interrupt()
Monitoring Speech State: To keep an eye on whether a speech task is active, just check the current_speech property like this:
current_speech = session.current_speech
if current_speech:
print(f"Current speech ID: {current_speech.id}")
print(f"Allow interruptions: {current_speech.allow_interruptions}")
If you have any questions or want a hand setting this up or tweaking the interruption logic to fit your needs, I’m here to help! Just let me know.
Sources: Turn detection and interruptions | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Agent speech and audio | LiveKit Docs | Bringing AI avatars to voice agentsancient-honey-53923
10/06/2025, 2:18 PMtall-airline-16756
10/06/2025, 2:19 PMagent_session._vad.on to handle start_of_speech and end_of_speech events! The VAD system in LiveKit does emit these events and supports attaching listeners with the .on() method. Just a small note—the underscore prefix (_vad) usually means it might be an internal API.
To keep things smooth and future-proof, I’d suggest using the public agent_session.vad property instead, or you might find it helpful to process VAD events using the VAD stream pattern like this:
async def process_vad():
async for vad_event in agent_session.vad.stream():
if vad_event.type == VADEventType.START_OF_SPEECH:
on_start_of_speech(vad_event)
elif vad_event.type == VADEventType.END_OF_SPEECH:
on_end_of_speech(vad_event)
This way aligns nicely with LiveKit’s recommended usage and helps keep everything compatible with future updates. If you have any questions or want me to walk you through it, just let me know—I’m here to help!
Sources: basics/echo_transcriber_agent.py | livekit-agents/livekit/agents/voice/agent_session.pyancient-honey-53923
10/06/2025, 2:49 PMtall-airline-16756
10/06/2025, 2:50 PMstart_of_speech and end_of_speech events using the session by implementing the RecognitionHooks protocol. These hooks work hand-in-hand with session state management, so they automatically update the user state whenever speech starts or ends—making things much smoother for you.
Here’s a friendly example of how you can implement it:
class MyRecognitionHooks:
def on_start_of_speech(self, ev):
print("User started speaking")
if ev:
print(f"Speech started at {ev.timestamp}")
def on_end_of_speech(self, ev):
print("User finished speaking")
if ev:
print(f"Speech duration: {ev.speech_duration}s")
# Other required methods for RecognitionHooks
def retrieve_chat_ctx(self):
pass
def on_end_of_turn(self, info):
return True
def on_interim_transcript(self, ev, speaking):
pass
def on_final_transcript(self, ev):
pass
Once you pass these hooks to an AgentSession, the framework will:
• Automatically connect VAD or STT streams to your hook methods.
• Update the session state to "speaking" or "listening" based on these events.
• Coordinate agent responses in a seamless way.
If you'd like, I’m here to walk you through integrating this into your session or help customize it to fit your needs perfectly. Just let me know!
Sources: plugins/deepgram/src/stt.ts | livekit-agents/livekit/agents/inference/stt.py | Agent Session & Activity Management | livekit/livekit_composite | DeepWikiancient-honey-53923
10/06/2025, 2:57 PMtall-airline-16756
10/06/2025, 2:58 PMancient-honey-53923
10/06/2025, 2:58 PM