many-forest-60185
11/27/2025, 9:41 PMtall-airline-16756
11/27/2025, 9:41 PMtall-airline-16756
11/27/2025, 9:42 PMuv add "livekit-agents[speechmatics]~=1.2"
1. Set your API key:
SPEECHMATICS_API_KEY=<your-api-key>
1. Configure STT with diarization and a speaker format:
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`:
@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 docsmany-forest-60185
11/27/2025, 9:43 PMtall-airline-16756
11/27/2025, 9:43 PMDEEPGRAM_API_KEY=<your-api-key>
1. Configure the Deepgram STT with diarization:
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:
@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 docsmany-forest-60185
11/29/2025, 5:24 AMtall-airline-16756
11/29/2025, 5:24 AMuv add "livekit-agents[gladia]~=1.2"
# .env
GLADIA_API_KEY=<your-gladia-api-key>
1. Enable diarization in the STT config
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
# 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
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 Docsmany-forest-60185
11/29/2025, 5:50 AMtall-airline-16756
11/29/2025, 5:50 AM