in the livekit agents sdk, how could I log to stdo...
# ask-ai
c
in the livekit agents sdk, how could I log to stdout the exact stt returned by the stt node?
t
Thanks for reaching out, Guillaume. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're looking to log the exact transcription returned by the STT node in the LiveKit Agents SDK! You can achieve this by overriding the
stt_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:
Copy code
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