How can I use speaker diarization from the STT mod...
# ask-ai
m
How can I use speaker diarization from the STT model? Can I give the LLM access to the diarization information form the STT node?
t
Thanks for reaching out, Alexander. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can — enable diarization in the STT plugin, include speaker labels in the transcription output, and pass that formatted transcription into the LLM so it can use who-said-what. How to do it (Speechmatics example): 1. Install the plugin:
Copy code
uv add "livekit-agents[speechmatics]~=1.2"
1. Set your API key:
Copy code
SPEECHMATICS_API_KEY=<your-api-key>
1. Configure STT with diarization and a speaker format:
Copy code
from livekit.plugins import speechmatics

   stt = speechmatics.STT(
       enable_diarization=True,
       speaker_active_format="<{speaker_id}>{text}<!--{speaker_id}-->",
   )
• Supported formats include
<{speaker_id}>{text}<!--{speaker_id}-->
or
[Speaker {speaker_id}] {text}
. How the LLM gets diarization: • The agent pipeline passes the final formatted transcription (with speaker tags) into the LLM’s chat context. • You should add system instructions explaining the speaker tag format (e.g.,
<s1>
is speaker 1) so the LLM interprets labels correctly. Event-based access (optional): • If you prefer programmatic handling, use the
user_input_transcribed
event to read `speaker_id`:
Copy code
@session.on("user_input_transcribed")
   def on_user_input_transcribed(event: UserInputTranscribedEvent):
       print(f"Speaker {event.speaker_id}: {event.transcript}, Final: {event.is_final}")
•
speaker_id
is present only when the STT provider supports diarization. Notes: • Not all STT providers support diarization; Speechmatics does. • If you want, tell me which STT provider or your desired speaker tag format and I’ll suggest exact system-prompt wording or a sample pipeline that injects diarization into the LLM context. Sources: Speechmatics STT plugin guide | LiveKit Docs | Events and error handling | LiveKit docs
m
What about for Deepgram?
t
Regarding Deepgram: yes — Deepgram supports speaker diarization and you can pass that diarization info to the LLM. How to enable it (example): 1. Set your API key:
Copy code
DEEPGRAM_API_KEY=<your-api-key>
1. Configure the Deepgram STT with diarization:
Copy code
from livekit.plugins import deepgram

stt = deepgram.STT(
    model="nova-2",
    diarize=True
)
1. Read speaker-labeled segments from transcription events and format them into the chat context:
Copy code
@stt_stream.on("recognize")
def on_transcript(event):
    for alt in event.alternatives:
        # alt.speaker contains speaker ID (e.g., "A", "B")
        # alt.text contains the segment
        print(f"[Speaker {alt.speaker}] {alt.text}")
        # append formatted lines to the LLM prompt / conversation history
Make the LLM understand the labels: • Include a short system instruction describing the format, e.g.: "Messages labeled
[Speaker A]
are from participant A,
[Speaker B]
are from participant B. Use these labels to track who said what." Notes: • Deepgram emits speaker IDs in the transcript metadata (e.g.,
A
,
B
) when
diarize=True
. • Unlike Speechmatics, Deepgram does not provide a
speaker_active_format
option; you must format the labeled transcript yourself before sending it to the LLM. • Ensure
DEEPGRAM_API_KEY
is set in your environment. Would you like a ready-to-use agent session example that wires the Deepgram STT output directly into the LLM prompt (with sample system instructions)? Sources: Deepgram STT plugin guide | LiveKit docs | Speechmatics STT plugin guide | LiveKit Docs | Deepgram STT | LiveKit docs
m
Great. Now what about for Gladia?
t
Gladia supports speaker diarization and you can pass its speaker labels to the LLM by formatting the transcript and including a system instruction describing the labels. Quick steps (Python): 1. Install & set API key
Copy code
uv add "livekit-agents[gladia]~=1.2"
# .env
GLADIA_API_KEY=<your-gladia-api-key>
1. Enable diarization in the STT config
Copy code
from livekit.plugins import gladia

stt = gladia.STT(
    speaker_diarization=True,
    # optional: diarization_threshold=0.5,
    # optional: max_speakers=4
)
1. Read/format speaker-labeled output and append to the LLM context
Copy code
# pseudo-handler for transcription segments
def handle_segment(segment):
    # segment.example: {"speaker": 1, "text": "Hello"}
    line = f"[Speaker {segment['speaker']}] {segment['text']}"
    # append `line` to your chat history / prompt sent to the LLM
1. Teach the LLM the label format via system instructions
Copy code
chat_ctx = llm.ChatContext().append(
    role="system",
    text=(
      "You are in a multi-party conversation. Lines labeled "
      "[Speaker X] indicate speaker turns. Use these labels to track who said what."
    )
)
session = AgentSession(stt=stt, llm=openai.LLM(), chat_ctx=chat_ctx)
Notes: • Diarization parameters like
diarization_threshold
and
max_speakers
are available; tune them if needed. • Gladia diarization is available in Python only. • Ensure clear audio for best speaker separation. Would you like a full AgentSession example that wires Gladia’s segments into the LLM prompt (with sample system instructions)? Sources: Gladia STT plugin guide | LiveKit Docs
m
I tried this and it says the diarization parameter doesn’t exist. Are you sure GLadia has diarization support?
t
Thanks — good catch. Gladia does not currently expose a diarization parameter in the LiveKit plugin, and diarization isn’t documented for Gladia. That’s why you see the error. Options you can take now: • Use a diarization-enabled STT provider (Speechmatics, Deepgram, or AssemblyAI) and pass their speaker-labeled output into the LLM. • If you prefer Gladia, check with Gladia for diarization support or request that LiveKit add diarization to the Gladia plugin. Would you like a quick example switching your config to Deepgram or Speechmatics so diarization works end-to-end? Sources: Speechmatics STT plugin guide | LiveKit Docs | Gladia STT plugin guide | LiveKit Docs | AssemblyAI plugin guide | LiveKit Docs