How to fetch meta data from lk.transcription
# ask-ai
c
How to fetch meta data from lk.transcription
t
Thanks for reaching out, Ashish. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To fetch metadata from
lk.transcription
, you need to register a text stream handler for the transcription topic and access the attributes from the stream info. Below are examples for different SDKs: JavaScript/TypeScript:
Copy code
room.registerTextStreamHandler('lk.transcription', async (reader, participantInfo) => {
  const message = await reader.readAll();

  // Access transcription metadata
  const segmentId = reader.info.attributes['lk.segment_id'];
  const transcribedTrackId = reader.info.attributes['lk.transcribed_track_id'];
  const isFinal = reader.info.attributes['lk.transcription_final'] === 'true';

  if (transcribedTrackId) {
    console.log(`Transcription from ${participantInfo.identity}: ${message}`);
    console.log(`Segment ID: ${segmentId}`);
    console.log(`Track ID: ${transcribedTrackId}`);
    console.log(`Is Final: ${isFinal}`);
  }
});
Android (Kotlin):
Copy code
private fun createTranscriptionSegment(streamInfo: TextStreamInfo): TranscriptionSegment {
    return TranscriptionSegment(
        id = streamInfo.attributes["lk.segment_id"] ?: "",
        text = "",
        language = "",
        final = streamInfo.attributes["lk.transcription.final"]?.toBoolean() ?: false,
        firstReceivedTime = Date().time,
        lastReceivedTime = Date().time
    )
}
Python:
Copy code
ATTRIBUTE_TRANSCRIPTION_SEGMENT_ID = "lk.segment_id"
ATTRIBUTE_TRANSCRIPTION_TRACK_ID = "lk.transcribed_track_id"
ATTRIBUTE_TRANSCRIPTION_FINAL = "lk.transcription_final"
Available Metadata Attributes: •
lk.segment_id
- The segment ID of the transcription •
lk.transcribed_track_id
- The associated track ID of the transcription •
lk.transcription_final
- Whether the transcription is final Let me know if you need further clarification or assistance with a specific SDK! Sources: app/src/main/java/io/livekit/android/example/voiceassistant/datastreams/Rem... | lib/src/core/engine.dart | livekit-agents/livekit/agents/types.py | Text and transcriptions | LiveKit Docs
c
I want to know from reader is it possible
t
Absolutely! You can fetch metadata directly from the reader in
lk.transcription
. The reader object gives you access to metadata through its
info.attributes
property. When you register a text stream handler for the
lk.transcription
topic, the reader object includes an
info
property that holds attributes and other metadata. Here’s a simple example in JavaScript:
Copy code
room.registerTextStreamHandler('lk.transcription', async (reader, participantInfo) => {
  const message = await reader.readAll();
  if (reader.info.attributes['lk.transcribed_track_id']) {
    console.log(`New transcription from ${participantInfo.identity}: ${message}`);
  } else {
    console.log(`New message from ${participantInfo.identity}: ${message}`);
  }
});
Here’s what metadata you can get from the Reader: •
reader.info.attributes
- Holds transcription-specific attributes •
reader.info.id
- The stream ID •
reader.info.timestamp
- When the message was sent Some common transcription attributes include: •
lk.segment_id
- The segment ID of the transcription •
lk.transcribed_track_id
- The track ID associated with the transcription •
lk.transcription_final
- Indicates if the transcription is final And if you’re working in Swift, here’s how you might access these attributes:
Copy code
let segmentID = reader.info.attributes[TranscriptionAttributes.segment.rawValue] ?? reader.info.id
let isFinal = reader.info.attributes[TranscriptionAttributes.final.rawValue] == "true"
This way, you can easily tell different types of transcription messages apart, keep track of their final status, and link them to specific audio tracks or segments. If you have any questions or want to dive deeper, just let me know! Sources: Text and transcriptions | LiveKit Docs | VoiceAgent/Chat/Receive/TranscriptionStreamReceiver.swift | src/room/attribute-typings.ts