how do i print in client the `lk.transcription` bu...
# ask-ai
a
how do i print in client the
lk.transcription
but only final and not intermediate? using python client sdk
t
Thanks for reaching out, Sebastián. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to print only final (not interim)
lk.transcription
messages using the Python client SDK — respond to the transcription stream and filter by the
lk.transcription_final
attribute. Example using the Room text-stream handler:
Copy code
from livekit import rtc

room = rtc.Room()  # your connected Room instance

@room.on("connected")
def on_connected():
    room.register_text_stream_handler(
        topic="lk.transcription",
        handler=on_transcription
    )

def on_transcription(stream: rtc.TextStream, participant_identity: str):
    async for text in stream:
        is_final = stream.info.attributes.get("lk.transcription_final") == "true"
        if is_final:
            print(f"[Final] {participant_identity}: {text}")
• Each transcription segment is a separate text stream under
lk.transcription
. • Only print when
stream.info.attributes["lk.transcription_final"] == "true"
. Sources: Text and transcriptions | LiveKit docs
a
i only see TextStreamReader, TextStreamWriter and TextStreamInfo classes in Livekit RTC package
t
You're right — use the classes you have. Check
TextStreamInfo.attributes
on the incoming
TextStreamReader
to detect final transcripts (
lk.transcription_final == "true"
) and read the text only for those streams. Example using the available classes:
Copy code
# register handler (replace with your Room instance registration)
room.register_text_stream_handler("lk.transcription", lambda reader, identity: handle_transcription(reader, identity))

async def handle_transcription(reader: TextStreamReader, participant_identity: str):
    info = reader.info  # TextStreamInfo
    is_final = info.attributes.get("lk.transcription_final") == "true"
    if not is_final:
        return
    # read_all() returns the full text for this final segment
    text = await reader.read_all()
    print(f"[Final] {participant_identity}: {text}")
This uses
TextStreamReader.info.attributes
to filter final segments and
read_all()
on the reader to get the transcript text. Sources: Text and transcriptions | LiveKit docs | Sending text | LiveKit docs
a
im getting:
Copy code
Argument of type "(stream: TextStreamReader, participant_identity: str) -> CoroutineType[Any, Any, None]" cannot be assigned to parameter "handler" of type "TextStreamHandler" in function "register_text_stream_handler"
  Type "(stream: TextStreamReader, participant_identity: str) -> CoroutineType[Any, Any, None]" is not assignable to type "TextStreamHandler"
    Function return type "CoroutineType[Any, Any, None]" is incompatible with type "None"
      "CoroutineType[Any, Any, None]" is not assignable to "None"basedpyright
t
You're seeing a typing error because
register_text_stream_handler
expects a synchronous handler (a
TextStreamHandler
that returns None), but you passed an async function (a coroutine), which mypy/pyright flags. Solution: register a synchronous handler that schedules the async reader coroutine (e.g., with asyncio.create_task). Example:
Copy code
import asyncio
from livekit import rtc
from livekit.rtc import TextStreamReader

def transcription_handler(reader: TextStreamReader, participant_identity: str) -> None:
    # schedule the async worker; handler itself stays synchronous
    asyncio.create_task(_handle_transcription_async(reader, participant_identity))

async def _handle_transcription_async(reader: TextStreamReader, participant_identity: str):
    info = reader.info
    if info.attributes.get("lk.transcription_final") != "true":
        return
    text = await reader.read_all()
    print(f"[Final] {participant_identity}: {text}")

room.register_text_stream_handler("lk.transcription", transcription_handler)
If your app already runs an asyncio loop and you prefer awaiting, you can instead run the registration inside that loop and keep the handler synchronous but synchronously run the coroutine with
asyncio.create_task
as shown.
a
it's not returning agent transcripts, only the same participant transcript is that correct?
t
You're seeing only the speaking participant's transcripts because agent-generated text can look different—it's published to the same
lk.transcription
topic but usually lacks the
lk.transcribed_track_id
attribute. To get agent transcripts as well, stop filtering only on
lk.transcribed_track_id
and accept streams that have no
lk.transcribed_track_id
(and still check
lk.transcription_final == "true"
). Quick checklist and example (uses synchronous handler that schedules async work to satisfy typing):
Copy code
import asyncio
from livekit.rtc import TextStreamReader

def transcription_handler(reader: TextStreamReader, participant_identity: str) -> None:
    asyncio.create_task(_handle_transcription_async(reader, participant_identity))

async def _handle_transcription_async(reader: TextStreamReader, participant_identity: str):
    info = reader.info
    # only final segments
    if info.attributes.get("lk.transcription_final") != "true":
        return

    text = await reader.read_all()
    # user transcription if track id present
    if info.attributes.get("lk.transcribed_track_id") is not None:
        print(f"[User Final] {participant_identity}: {text}")
    else:
        # agent-generated text output (no transcribed_track_id)
        print(f"[Agent Final] {participant_identity}: {text}")

room.register_text_stream_handler("lk.transcription", transcription_handler)
• Keep the
lk.transcription_final == "true"
check to avoid interim results. • Do not require `lk.transcribed_track_id`; treat absence as likely agent text output. • The sender identity will be the participant identity included with the stream—use that to distinguish the agent participant if needed. Sources: Text and transcriptions | LiveKit docs | How to Set Up Speech-to-Text Input with Text-Only Output in LiveKit Agents
a
im not filtering yet i don't see agent transcriptions
t
You're not seeing agent transcriptions because the agent may not be publishing them. Agent text output is sent on the same
lk.transcription
topic but usually has no
lk.transcribed_track_id
. Quick checks and a handler that covers both: 1) Confirm the agent is actually producing speech/text • The agent must call
session.say()
/ generate a reply or have text output enabled. • Ensure agent session transcription/text output isn't disabled. 2) Use a handler that accepts both streams (with and without
lk.transcribed_track_id
) and only prints finals:
Copy code
import asyncio
from livekit.rtc import TextStreamReader

def transcription_handler(reader: TextStreamReader, participant_identity: str) -> None:
    asyncio.create_task(_handle(reader, participant_identity))

async def _handle(reader: TextStreamReader, participant_identity: str):
    info = reader.info
    if info.attributes.get("lk.transcription_final") != "true":
        return
    text = await reader.read_all()
    if info.attributes.get("lk.transcribed_track_id") is not None:
        print(f"[User Final] {participant_identity}: {text}")
    else:
        print(f"[Agent Final] {participant_identity}: {text}")
If you still see only participant transcripts after this: • Confirm the agent is configured to output transcripts (text/audio enabled). • Confirm the agent actually produced speech/text during the session. • Check agent logs for generation/TTS activity. Tell me whether the agent was invoked to speak (e.g., you called
session.say()
or triggered a reply) and I’ll help narrow the cause. Sources: Text and transcriptions | LiveKit docs | How to Set Up Speech-to-Text Input with Text-Only Output in LiveKit Agents
a
how to check if transcription is enabled or not in the agent?
t
To check whether transcription is enabled for your agent, inspect the agent session / room output options used when starting the AgentSession. Key things to check: 1. Python (AgentSession / RoomOptions) • Look at
text_output
or
TextOutputOptions
passed into
RoomOptions
when starting the session. • If
text_output
is
False
, transcription/text output is disabled. Default is enabled. • Example:
Copy code
from livekit import room_io
room_options = room_io.RoomOptions(
    text_output=True  # must be True or a TextOutputOptions instance to enable transcription
)
session.start(room_options=room_options)
1. Node.js (if applicable) • Check
outputOptions.transcriptionEnabled
(boolean). Must be
true
to enable transcription. 1. Sync settings • If
sync_transcription
is configured (in
TextOutputOptions
), it affects delivery timing but not whether transcription is enabled.
sync_transcription=True
emits in sync with audio. 1. Runtime checks • Ensure the agent actually produced speech/text (e.g.,
session.say()
or generated a reply). • Listen to the
lk.transcription
text stream; agent transcripts appear on that topic but typically lack
lk.transcribed_track_id
. • Also watch conversation events (e.g.,
conversation_item_added
) which appear when the agent adds text. If you want, tell me: • How you start the session (show the
RoomOptions
/
outputOptions
you use), and • Whether you call
session.say()
or generate replies, and I’ll point out exactly which setting to change. Sources: Agent session | LiveKit docs | Agents (Node.js) | livekit/livekit_composite | DeepWiki | Text and transcriptions | LiveKit docs
a
im not seeing it, can we make so that agent output transcription is streamed to the client on the same topic?
t
You can stream agent-generated transcription to clients on the same
lk.transcription
topic — that’s how agent text output is delivered. To make sure it appears for your clients, confirm two things and use the handler pattern below. 1) Agent configuration • Ensure agent text output is enabled when starting the agent session: ◦ Python:
RoomOptions(text_output=True)
or provide a
TextOutputOptions
instance. ◦ Do not set
text_output=False
. • Ensure the agent actually emits text (e.g.,
session.say()
or generate a reply). 2) Client subscription & handler • Listen to the
lk.transcription
text stream and accept streams that may lack
lk.transcribed_track_id
(those are agent outputs). • Only print final segments (
lk.transcription_final == "true"
). Example (satisfies synchronous handler typing and handles both participant and agent output):
Copy code
import asyncio
from livekit.rtc import TextStreamReader

def transcription_handler(reader: TextStreamReader, participant_identity: str) -> None:
    asyncio.create_task(_handle(reader, participant_identity))

async def _handle(reader: TextStreamReader, participant_identity: str):
    info = reader.info
    if info.attributes.get("lk.transcription_final") != "true":
        return
    text = await reader.read_all()
    if info.attributes.get("lk.transcribed_track_id") is not None:
        print(f"[User Final] {participant_identity}: {text}")
    else:
        print(f"[Agent Final] {participant_identity}: {text}")

room.register_text_stream_handler("lk.transcription", transcription_handler)
If you still don’t see agent transcripts after: • Confirm the agent was invoked to speak (call used
session.say()
/ generated a reply). • Confirm the session started with
text_output
enabled. • Confirm your client successfully subscribed to text streams in the room. Tell me how you start the agent session (show
RoomOptions
/
text_output
usage) and whether you called
session.say()
— I’ll point out the exact setting to change.
a
the agent doesnt use session.say manually
t
Agent transcriptions can be produced and streamed without calling
session.say()
manually — as long as the agent is configured to produce text output (and actually generates a reply), its text output will be published to the
lk.transcription
topic. Steps to verify and enable this: 1) Confirm agent text output is enabled when the session starts • Python: ensure
RoomOptions(text_output=True)
or a
TextOutputOptions
instance is passed to the agent session. If
text_output=False
, agent text/transcription is disabled. 2) Confirm agent is generating replies • Even without
session.say()
, the agent must generate text (e.g., via auto-replies, webhook-driven responses, or the agent's reply generation flow). If the agent never produces a reply, no agent transcript will be published. 3) Client-side: listen to
lk.transcription
and accept streams without
lk.transcribed_track_id
• Agent output is on the same
lk.transcription
topic but typically lacks
lk.transcribed_track_id
. • Only print when
lk.transcription_final == "true"
. Minimal client example (synchronous handler that schedules async work):
Copy code
import asyncio
from livekit.rtc import TextStreamReader

def transcription_handler(reader: TextStreamReader, participant_identity: str) -> None:
    asyncio.create_task(_handle(reader, participant_identity))

async def _handle(reader: TextStreamReader, participant_identity: str):
    if reader.info.attributes.get("lk.transcription_final") != "true":
        return
    text = await reader.read_all()
    if reader.info.attributes.get("lk.transcribed_track_id") is not None:
        print(f"[User Final] {participant_identity}: {text}")
    else:
        print(f"[Agent Final] {participant_identity}: {text}")
If you still see nothing for agent output: • Share how you start the agent (show
RoomOptions
/
text_output
), and • Confirm whether the agent generation flow is invoked (automatic replies, webhook, or other trigger). Send those two bits and I’ll pinpoint the setting to change.
a
the agent is producing response but im only receiving user transcriptions
t
Thanks — I hear that the agent is producing responses but you only receive user transcriptions. To diagnose, please share two things so I can pinpoint the issue: 1. The code you use to start the agent session (the
RoomOptions
/
text_output
or
outputOptions
you pass when calling
session.start()
). 2. The client-side handler registration code you use to listen to
lk.transcription
(the exact
register_text_stream_handler
call and handler). If you prefer, paste the minimal snippets for both (server/agent start + client handler). I’ll check for the common causes (text output disabled, sync settings, handler typing/awaiting, or accidental filtering) and give the exact fix.
a
Copy code
await session.start(
        agent=assistant,
        room=room,
        room_options=RoomOptions(
            audio_input=AudioInputOptions(
                noise_cancellation=noise_cancellation.BVC(),
                pre_connect_audio=True,
                pre_connect_audio_timeout=20,
            ),
        ),
    )
Copy code
def transcription_handler(
        self, reader: rtc.TextStreamReader, participant_identity: str
    ) -> None:
        asyncio.create_task(
            self._handle_transcription_async(reader, participant_identity)
        )

    async def _handle_transcription_async(
        self, reader: rtc.TextStreamReader, participant_identity: str
    ):
        info = reader.info
        if info.attributes and info.attributes.get("lk.transcription_final") != "true":
            return
        text = await reader.read_all()
        logger.info(f"User message [{participant_identity}]: {text}")
t
Thanks — I reviewed your snippets. You didn't enable text output when starting the agent, so agent text/transcription isn't being published. Your client handler is fine for receiving streams. What to change (server/agent): • Pass
text_output=True
(or a
TextOutputOptions
instance) in
RoomOptions
when calling
session.start()
. Example:
Copy code
from livekit import room_io

await session.start(
    agent=assistant,
    room=room,
    room_options=room_io.RoomOptions(
        audio_input=AudioInputOptions(...),
        text_output=True,  # enable agent text/transcription output
    ),
)
Client-side: your handler is fine; agent transcripts will appear on
lk.transcription
but typically lack
lk.transcribed_track_id
. To verify, add a distinct log for agent streams:
Copy code
info = reader.info
if info.attributes and info.attributes.get("lk.transcription_final") != "true":
    return
text = await reader.read_all()
if info.attributes and info.attributes.get("lk.transcribed_track_id") is None:
    logger.info(f"Agent message [{participant_identity}]: {text}")
else:
    logger.info(f"User message [{participant_identity}]: {text}")
Apply the server change and test. If you still only see user transcripts after that, tell me the agent reply flow (how it generates responses) and I’ll help further.
a
no, still the same behaviour
t
Thanks — you're receiving only user transcripts despite enabling
text_output
. Let's narrow this down with three quick checks and one small experiment. 1) Confirm agent actually emits text replies • Does your agent call
session.say()
or otherwise generate replies (e.g.,
session.generate_reply()
or built-in auto-reply flow)? If the agent produces no reply, no agent transcription will be published. 2) Verify RoomOptions used at start • You showed RoomOptions without
text_output
. Please confirm you restarted the session with:
Copy code
room_io.RoomOptions(
    audio_input=...,
    text_output=True,   # must be present
)
• If you changed it and still see nothing, paste the exact
session.start(...)
call you now run. 3) Ensure handler runs long enough and logs agent streams • Your handler currently logs only "User message" — add a log for streams missing
lk.transcribed_track_id
so agent messages don't get mis-classified or ignored:
Copy code
info = reader.info
if info.attributes and info.attributes.get("lk.transcription_final") != "true":
    return
text = await reader.read_all()
if info.attributes and info.attributes.get("lk.transcribed_track_id") is None:
    logger.info(f"Agent message [{participant_identity}]: {text}")
else:
    logger.info(f"User message [{participant_identity}]: {text}")
Quick experiment to confirm publishing path • While your client is connected and handler registered, trigger the agent to speak once explicitly (if possible). For example, call
session.say("testing 1 2 3")
from your agent logic or invoke whatever action causes the agent to generate a reply. Then watch logs for an
Agent message
entry. If after these checks you still see only user transcripts, please paste: • The exact
session.start(...)
call you currently run (with RoomOptions), • How the agent generates replies (code snippet or description), • Any logs from the agent around the time it generates a response. I’ll use those to pinpoint why agent transcripts aren’t being published. Sources: Text and transcriptions | LiveKit docs | Voice AI quickstart | LiveKit docs | How to Set Up Speech-to-Text Input with Text-Only Output in LiveKit Agents