crooked-dawn-90821
11/01/2025, 12:01 PMtall-airline-16756
11/01/2025, 12:01 PMtall-airline-16756
11/01/2025, 12:02 PMstt_node method in your custom Agent class. This way, you can easily intercept and handle the SpeechEvent objects as they come from the STT system.
Here’s a friendly example in Python to guide you through it:
import logging
import asyncio
from livekit.agents import Agent, JobContext, WorkerOptions, cli
from livekit import rtc
from livekit.agents.voice import AgentSession
from livekit.plugins import deepgram, openai, silero
from livekit.agents import stt
logger = logging.getLogger("stt-logger")
class MyLoggingAgent(Agent):
async def stt_node(
self, audio: AsyncIterable[rtc.AudioFrame], model_settings: ModelSettings
) -> AsyncIterable[stt.SpeechEvent | str]:
# Get the default STT node generator (from the parent class)
default_stt_generator = Agent.default.stt_node(self, audio, model_settings)
# Iterate over every SpeechEvent produced by the STT
async for event in default_stt_generator:
# Check if it’s a final transcription event
if isinstance(event, stt.SpeechEvent) and event.type == stt.SpeechEventType.FINAL_TRANSCRIPT:
# The transcribed text is in the first alternative
transcribed_text = event.alternatives[0].text
# Log the exact transcription to stdout
logger.info(f"STT Returned Transcription: {transcribed_text}")
# Or use print()
# print(f"STT Returned Transcription: {transcribed_text}")
# Always yield the event so the pipeline can continue
yield event
async def entrypoint(ctx: JobContext):
session = AgentSession(
stt=deepgram.STT(), # Your chosen STT provider
llm=openai.LLM(),
tts=openai.TTS(),
vad=silero.VAD.load(),
)
await session.start(agent=MyLoggingAgent(), room=ctx.room)
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
This snippet overrides the stt_node hook by first calling the default implementation to receive the transcription stream. Think of it as a helpful middle step: it reads each event, checks if it contains the final transcription text, logs that to stdout, and then passes the event along so the rest of your agent’s pipeline can keep working smoothly.
If you have any questions or want to dive deeper, feel free to ask—I'm here to help!
Sources: Pipeline nodes and hooks | LiveKit docs | livekit-agents/livekit/agents/voice/agent.py