Skip to content

LiveKit: speech to text

Transcribe user audio in a LiveKit Agents session with Sprag models.

Overview

Sprag provides an OpenAI compatible speech-to-text API allowing you to use LiveKit's OpenAI plugin out of the box. For STT on LiveKit that means openai.STT is usable by simply setting Sprag's base URL and invoking one of our models.

Basic usage

Use openai.STT in an AgentSession, or as a standalone transcription service:

import os

from livekit.agents import AgentSession
from livekit.plugins import openai

session = AgentSession(
    stt=openai.STT(
        model="rhythm",
        use_realtime=True,
        base_url="https://api.sprag.ai/v1",
        api_key=os.environ["SPRAG_API_KEY"],
    ),
    # ... llm, tts, etc.
)

Configuration

Constructor arguments

ArgumentTypeDefaultDescription
modelstr--Required parameter when used with Sprag
use_realtimeboolFalseStreams over a realtime WebSocket.
base_urlstr--https://api.sprag.ai/v1.
api_keystrOPENAI_API_KEY env varYour Sprag API key.
turn_detectiondictserver VADEndpointing on the realtime transport.

See speech-to-text models for a list of available models and supported languages for the languages each transcribes.

Transports

In most cases Sprag hosted models are available as REST and realtime connections over a websocket. With use_realtime set to True you will receive a persistent websocket for the duration of the request. With False, each turn is committed as a separate POST.

VAD

For realtime connections we offer an optional server configured VAD for automatic detection of turn boundaries, enabled through turn_detection. It applies only with use_realtime=True; LiveKit ignores it on the REST transport.

import os

from livekit.agents import AgentSession
from livekit.plugins import openai, silero

session = AgentSession(
    stt=openai.STT(
        model="rhythm",
        use_realtime=True,
        base_url="https://api.sprag.ai/v1",
        api_key=os.environ["SPRAG_API_KEY"],
        turn_detection={"silence_duration_ms": 200},
    ),
)

You are still free to use VAD configured through LiveKit as well.

turn_detection takes the same fields as the realtime API, and we fill in any you leave out.

Events

A realtime session reports the edges of each turn alongside its transcript.

EventMeaning
START_OF_SPEECHThe turn started.
INTERIM_TRANSCRIPTA fragment of the turn's transcript.
FINAL_TRANSCRIPTThe settled transcript for that turn.
END_OF_SPEECHThe turn ended.
RECOGNITION_USAGEMeasured duration for that turn.

Fragments concatenate into the final transcript, and a turn's fragments arrive together once it closes. Over REST you receive one FINAL_TRANSCRIPT per utterance.

Speaker labels and word timings

Over REST we return word and segment times under response_format=verbose_json, and speaker_labels=true attributes each word to a speaker.

You can find more information about timestamps and diarization in the docs.

Reference