Is there a way to retrieve a transcript from a roo...
# ask-ai
a
Is there a way to retrieve a transcript from a room?
t
Thanks for reaching out, Anne. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can retrieve a transcript from a room in LiveKit using two methods: 1. Real-Time Transcription: You can listen to the
RoomEvent.TranscriptionReceived
event in the Frontend SDK to capture transcription segments as they are generated. Here's an example:
Copy code
import { useEffect, useState } from "react";
import { useMaybeRoomContext } from "@livekit/components-react";
import { RoomEvent, TranscriptionSegment } from "livekit-client";

export default function Transcriptions() {
    const room = useMaybeRoomContext();
    const [transcriptions, setTranscriptions] = useState<{ [id: string]: TranscriptionSegment }>({});

    useEffect(() => {
        if (!room) return;

        const updateTranscriptions = (segments: TranscriptionSegment[]) => {
            setTranscriptions(prev => {
                const newTranscriptions = { ...prev };
                for (const segment of segments) {
                    newTranscriptions[segment.id] = segment;
                }
                return newTranscriptions;
            });
        };

        room.on(RoomEvent.TranscriptionReceived, updateTranscriptions);
        return () => room.off(RoomEvent.TranscriptionReceived, updateTranscriptions);
    }, [room]);

    return (
        
    );
}
Note: Transcription events are being moved to text streams on the
lk.chat
topic, so you may need to update your implementation accordingly. 2. Post-Session Transcript Retrieval: You can use the
session.history
property to access and save the session's history upon room shutdown. Here's an example in Python:
Copy code
from datetime import datetime
import json
from livekit.agents import stt, transcription
from livekit.plugins.deepgram import STT

async def write_transcript():
    current_date = datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = f"/tmp/transcript_{ctx.room.name}_{current_date}.json"
    with open(filename, 'w') as f:
        json.dump(session.history.to_dict(), f, indent=2)
    print(f"Transcript saved to {filename}")

async def entrypoint(ctx: JobContext):
    ctx.add_shutdown_callback(write_transcript)
Let me know if you need further assistance with implementing these methods! Sources: Session recording and transcripts | LiveKit Docs | Transcriptions | LiveKit Docs