This message was deleted.
# ask-ai
s
This message was deleted.
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.
f
I asked below anyway
t
I'm glad you asked about the
stt.SpeechEventType.PREFLIGHT_TRANSCRIPT
event in LiveKit! This event is really helpful because it lets agents start generating responses even before the user has finished speaking. Let me walk you through how you can work with this event: • First, check if the event type is
stt.SpeechEventType.PREFLIGHT_TRANSCRIPT
. • Then, you can extract the transcript data using
event.alternatives[0].text
,
event.alternatives[0].confidence
, and so on. • Feel free to use this preflight transcript for early processing, keeping in mind it might still be updated. • Make sure to coordinate this with the final transcripts, so your system smoothly handles any updates when the final transcript arrives. This event is especially great for cutting down latency in conversational AI applications, as it allows response generation to start before speech recognition is fully complete. If you have any questions or want to dive deeper, I'm here to help! Sources: livekit-plugins/livekit-plugins-deepgram/livekit/plugins/deepgram/stt_v2.py | Agent speech and audio | LiveKit Docs
f
How do I handle it?
t
I'd love to help you handle the
stt.SpeechEventType.PREFLIGHT_TRANSCRIPT
event! Here's a friendly step-by-step guide you can follow: 1. First, try creating a custom
RecognitionHooks
class where you can define exactly how you'd like to process this event. For example:
Copy code
class CustomRecognitionHooks(RecognitionHooks):
    def on_interim_transcript(self, ev: stt.SpeechEvent, *, speaking: bool | None) -> None:
        if ev.type == stt.SpeechEventType.PREFLIGHT_TRANSCRIPT:
            print(f"Preflight transcript: {ev.alternatives[0].text}")
            # Feel free to add your own custom logic here
1. Next, you’ll want to integrate this custom hooks class with your
AudioRecognition
system like this:
Copy code
audio_recognition = AudioRecognition(
    hooks=CustomRecognitionHooks(),
    stt=your_stt_node,  # Your STT implementation
    vad=your_vad,       # Your VAD implementation
    turn_detector=None,  # Optional turn detector
    min_endpointing_delay=0.5,
    max_endpointing_delay=2.0,
    turn_detection_mode="vad"
)
With this setup, the
on_interim_transcript
method will be called whenever a
PREFLIGHT_TRANSCRIPT
event comes through. This way, you can easily process the transcript data and add any preemptive generation or other custom behavior you need. If you have any questions or want to explore this further, I’m here to help!
f
OK but I can't override
AgentActivity
with is a subclass of
RecognitionHooks
t
I understand you might not have overridden
AgentActivity
yet, which is a subclass of
RecognitionHooks
. No worries! You can still handle the
stt.SpeechEventType.PREFLIGHT_TRANSCRIPT
event by implementing the necessary methods in your own custom agent class. Let me guide you through the steps: 1. First, create a custom subclass of
AgentActivity
and override the
on_interim_transcript
method like this:
Copy code
from livekit.agents.voice.audio_recognition import AgentActivity

class CustomAgentActivity(AgentActivity):
    def on_interim_transcript(self, ev: stt.SpeechEvent, *, speaking: bool | None) -> None:
        if ev.type == stt.SpeechEventType.PREFLIGHT_TRANSCRIPT:
            print(f"Preflight transcript: {ev.alternatives[0].text}")
            # Feel free to add your custom logic here
1. Then, use this custom class in your application to handle the event. The
on_interim_transcript
method will be triggered automatically whenever a
PREFLIGHT_TRANSCRIPT
event comes in. This way, you can manage the event smoothly without needing to change the base
RecognitionHooks
class. If you have any questions or need further help, I’m here for you!
f
but where would I hook this custom activity into the system?
t
I'd love to help you get your custom
AgentActivity
integrated smoothly with the system! To do this, you'll want to connect it with the LiveKit agent session or the audio recognition system. Here's a simple way to approach it: 1. First, create an instance of your custom
AgentActivity
class:
Copy code
custom_activity = CustomAgentActivity()
1. Next, pass this instance to the
AudioRecognition
system or the agent session where the recognition hooks are used. For example:
Copy code
audio_recognition = AudioRecognition(
    hooks=custom_activity,  # This is where your custom activity goes
    stt=your_stt_node,      # Your speech-to-text setup
    vad=your_vad,           # Your voice activity detection
    turn_detector=None,     # Optional, if you have one
    min_endpointing_delay=0.5,
    max_endpointing_delay=2.0,
    turn_detection_mode="vad"
)
1. Finally, make sure the
AudioRecognition
instance is properly initialized and used within your application. This way, your custom activity’s overridden methods will be called automatically when events like
PREFLIGHT_TRANSCRIPT
happen. With this setup, your custom logic inside
CustomAgentActivity
will seamlessly work as part of the LiveKit audio recognition flow. If you have any questions or want to walk through this together, I’m here to help!
f
Is there a way to do this via
AgentSession
and/or
Agent
?
t
I'm glad you're interested in integrating your custom logic using
AgentSession
or
Agent
! You can do this by extending the
Agent
class and then using it within your session. Let me walk you through the steps: 1. First, create a custom agent class by extending `Agent`:
Copy code
from livekit.agents.voice import Agent

class CustomAgent(Agent):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    async def on_enter(self):
        # Here you can add your custom logic when the agent starts
        pass

    async def on_exit(self):
        # And here, add your custom logic when the agent stops
        pass
1. Next, integrate this custom agent with the `AgentSession`:
Copy code
from livekit.agents.voice import AgentSession

# Create a session with your custom agent
session = AgentSession(
    stt=your_stt,
    llm=your_llm,
    tts=your_tts,
    # other configuration
)

# Start the session with the custom agent
await session.start(agent=CustomAgent(...))
This way, your custom logic will be smoothly executed as part of the agent's lifecycle. Plus, the
AgentSession
will take care of managing the underlying
AgentActivity
instances for you, so you can focus on what matters most. If you have any questions or need further help, feel free to ask! Sources: Agent Session & Client Integration | livekit/livekit_composite | DeepWiki | agents/src/voice/agent_activity.ts | livekit-agents/livekit/agents/voice/agent_session.py
f
OK but to the end of handling that original event
t
I'm glad you're looking into handling the
stt.SpeechEventType.PREFLIGHT_TRANSCRIPT
event within an
AgentSession
or
Agent
. You can easily listen for transcription events using the session's event system. Here's a friendly guide to help you through it: 1. First, use the
on
method of
AgentSession
to listen for transcription events:
Copy code
@session.on("user_input_transcribed")
def on_user_transcribed(event):
    if not event.is_final:
        print(f"Preflight transcript: {event.transcript}")
        # Feel free to add your own logic here to handle preflight transcripts
1. Inside this event handler, you can check the
is_final
property to tell apart interim (preflight) transcripts from the final ones. This method lets you manage the
PREFLIGHT_TRANSCRIPT
event smoothly without needing to change the underlying hooks, making great use of the session’s built-in event system for a seamless experience.