i want to connect custom STT for live kit
# ask-ai
r
i want to connect custom STT for live kit
t
Thanks for reaching out, Av. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you're looking to connect a custom Speech-to-Text (STT) service to LiveKit, you can do this by integrating it through the LiveKit Agents framework using the
stt.STT
interface. Here’s a quick rundown of what that involves: • Creating a class that inherits from
stt.STT
and implementing the methods needed for transcription tasks. • Defining the STT capabilities, like whether it supports streaming and interim results. • Handling audio input and managing WebSocket connections, if that applies. • Emitting transcription events such as
START_OF_SPEECH
,
INTERIM_TRANSCRIPT
, and
FINAL_TRANSCRIPT
. • Checking out existing plugins (like Soniox, Deepgram, Google) which can serve as great templates for your implementation. If you want to dive deeper, the LiveKit Agents STT interface source is a fantastic resource. And of course, feel free to reach out if you have any questions or need a hand along the way! Sources: examples/other/transcription/README.md | Transcription Applications | livekit/livekit_composite | DeepWiki | Speechmatics STT integration guide | LiveKit Docs | Speech-to-text (STT) integrations | LiveKit Docs
r
give me sample code
t
Here’s a friendly little Python code template to help you get started with a custom Speech-to-Text (STT) service in LiveKit:
Copy code
from livekit.agents import stt, APIConnectOptions, AudioBuffer
from livekit.agents.stt import SpeechEvent, SpeechEventType, STTCapabilities

class CustomSTT(stt.STT):
    def __init__(self, *, api_key: str, sample_rate: int = 16000, language: str = "en"):
        capabilities = STTCapabilities(streaming=True, interim_results=True)
        super().__init__(capabilities=capabilities)
        self._api_key = api_key
        self._sample_rate = sample_rate
        self._language = language

    async def _recognize_impl(self, buffer: AudioBuffer, *, language: str | None = None, conn_options: APIConnectOptions):
        audio_data = b"".join(frame.data.tobytes() for frame in buffer)
        recognized_text = "This is a sample transcription."
        return SpeechEvent(
            type=SpeechEventType.FINAL_TRANSCRIPT,
            alternatives=[
                stt.SpeechData(
                    text=recognized_text,
                    language=language or self._language,
                    confidence=0.9,
                )
            ],
        )

    def stream(self, *, language: str | None = None, conn_options: APIConnectOptions):
        return CustomSpeechStream(
            stt=self,
            conn_options=conn_options,
            sample_rate=self._sample_rate,
            language=language or self._language,
        )

class CustomSpeechStream(stt.SpeechStream):
    def __init__(self, *, stt: CustomSTT, conn_options: APIConnectOptions, sample_rate: int, language: str):
        super().__init__(stt=stt, conn_options=conn_options, sample_rate=sample_rate)
        self._language = language

    async def _run(self):
        async for frame in self._input_ch:
            if isinstance(frame, AudioFrame):
                pass
            self._event_ch.send_nowait(
                SpeechEvent(
                    type=SpeechEventType.INTERIM_TRANSCRIPT,
                    alternatives=[stt.SpeechData(text="interim text", language=self._language)]
                )
            )
This example walks you through how to: • Implement the
_recognize_impl
method for handling non-streaming transcription. • Set up a streaming transcription class using
SpeechStream
. • Send transcription events like
FINAL_TRANSCRIPT
and
INTERIM_TRANSCRIPT
to keep things flowing. If you’d like me to explain any part in more detail or want a tailored example, just let me know—I’m here to help! Sources: livekit-plugins/livekit-plugins-azure/livekit/plugins/azure/stt.py | plugins/deepgram/src/stt.ts