`STTMetrics` is emitted after the STT model has pr...
# ask-ai
b
STTMetrics
is emitted after the STT model has processed the audio input. This metrics is only available when an STT component is used, which does not apply to Realtime APIs. MetricDescription`audio_duration`The duration (seconds) of the audio input received by the STT model.`duration`For non-streaming STT, the amount of time (seconds) it took to create the transcript. Always
0
for streaming STT.
streamedTrue
if the STT is in streaming mode. How do I print the
STTMetrics
with my logger ?
t
Thanks for reaching out, aile. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To print the
STTMetrics
with your logger in LiveKit, you can use one of the following approaches: 1. Using the Built-in
log_metrics
Helper FunctionThe simplest way is to use the built-in
log_metrics
utility function, which automatically formats and logs `STTMetrics`:
Copy code
from livekit.agents import metrics, MetricsCollectedEvent

@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
    metrics.log_metrics(ev.metrics)
This will log the message "STT metrics" and include fields like
audio_duration
. You can also pass a custom logger:
Copy code
metrics.log_metrics(ev.metrics, logger=your_custom_logger)
2. Setting Up Custom STT Metrics Event HandlersFor more control, you can set up event handlers directly on the STT component:
Copy code
import logging
from livekit.agents.metrics import STTMetrics

logger = logging.getLogger("stt-metrics")
logger.setLevel(<http://logging.INFO|logging.INFO>)

class STTMetricsAgent(Agent):
    def __init__(self):
        super().__init__(
            stt=deepgram.STT(),
            # ... other components
        )
        
        def stt_wrapper(metrics: STTMetrics):
            asyncio.create_task(self.on_stt_metrics_collected(metrics))
            
        self.stt.on("metrics_collected", stt_wrapper)

    async def on_stt_metrics_collected(self, metrics: STTMetrics):
        <http://logger.info|logger.info>(
            f"STT Metrics - Duration: {metrics.duration:.4f}s, "
            f"Audio Duration: {metrics.audio_duration:.4f}s, "
            f"Streamed: {metrics.streamed}, "
            f"Speech ID: {metrics.speech_id}"
        )
Available Fields in `STTMetrics`When logging
STTMetrics
, you can access the following fields: • `type`: The metrics type • `label`: The metrics label • `request_id`: The request identifier • `timestamp`: When the metrics were collected • `duration`: Processing duration in seconds • `speech_id`: The speech identifier • `error`: Any error that occurred • `streamed`: Whether STT is in streaming mode • `audio_duration`: Duration of the audio input in seconds The built-in
log_metrics
function is ideal for standardized logging, while custom event handlers allow for more tailored logging formats. Sources: livekit-agents/livekit/agents/metrics/utils.py | Capturing metrics | LiveKit Docs | metrics/metrics_llm.py