How do you send and receive messages from the live...
# ask-ai
c
How do you send and receive messages from the livekit agent when text-only mode is enabled?
t
Thanks for reaching out, Matin. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To send and receive messages from a LiveKit agent in text-only mode, follow these steps: 1. Configure the Agent for Text-Only Mode: Set up the agent to operate in text-only mode by disabling audio and enabling text input/output:
Copy code
async def entrypoint(ctx: JobContext):
    session = AgentSession(
        llm=openai.LLM(model="gpt-4o-mini"),
        # No TTS or STT needed here
    )
    await session.start(
        agent=MyAgent(),
        room=ctx.room,
        room_input_options=RoomInputOptions(text_enabled=True, audio_enabled=False),
        room_output_options=RoomOutputOptions(transcription_enabled=True, audio_enabled=False),
    )
2. Sending Messages to the Agent: Send text messages to the agent using the
lk.chat
topic:
Copy code
// JavaScript Example
const text = 'Hello, how are you today?';
const info = await room.localParticipant.sendText(text, {
    topic: 'lk.chat',
});
Copy code
// Swift Example
try await room.localParticipant.sendText(text, for: "lk.chat")
3. Receiving Messages from the Agent: The agent sends responses through the
lk.transcription
text stream topic. Register a text stream handler to receive these responses:
Copy code
// JavaScript Example
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}`);
    }
});
Key Points: • Send messages to the
lk.chat
topic. • Receive responses from the
lk.transcription
topic. • Ensure
audio_enabled
is set to
False
in both input and output options for text-only mode. • No TTS or STT components are required for text-only agents. Let me know if you need further clarification or assistance! Sources: Text and transcriptions | LiveKit Docs | Text and transcriptions | LiveKit Docs | VoiceAgent/Chat/Send/LocalMessageSender.swift | examples/other/text_only.py
c
Why does it seem like I still have to wait for the agent to finish thinking? Is there any way to stream the chunks? Or is it generating it at a conversational pace? Also should I set sync_transcript to false, or just not specify it at all?
t
Hey there! I totally get your concerns about waiting for the agent to finish thinking and streaming chunks. Let me walk you through some helpful details and settings that can make things smoother for you: 1. Streaming Chunks: You can actually stream text chunks from the agent by setting up a text stream handler. This way, you can process and show responses in real-time as they come in:
Copy code
// JavaScript Example
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}`);
    }
});
And on the backend, you can do something similar to handle chunks as they arrive:
Copy code
async def on_text_received(reader, participant_identity):
    async for chunk in reader:
        print(f"Received chunk: {chunk}")
2. sync_transcription Setting: This
sync_transcription
option controls whether transcriptions sync up with the audio playback: • By default: Transcriptions match the audio, showing text word by word as it’s spoken. • Want it faster? Set
sync_transcription=False
to get transcriptions right away, without waiting for sync. Here’s a quick example:
Copy code
await session.start(
    agent=MyAgent(),
    room=ctx.room,
    room_output_options=RoomOutputOptions(sync_transcription=False),
)
3. Key Takeaways: • Make sure you have a text stream handler registered for the
lk.transcription
topic to stream responses live. • Set
sync_transcription=False
if you want to skip delays caused by syncing with audio playback. • If you don’t set
sync_transcription
, it defaults to synchronized transcription. I hope this helps you get faster response streaming and cuts down on waiting! If you have any more questions or want me to clarify anything, just let me know—I'm here to help! Sources: Text and transcriptions | LiveKit Docs | examples/other/transcription/translator.py | Text and transcriptions | LiveKit Docs
c
Can you please create a python example please?
t
Here's a friendly Python example to help you set up a LiveKit agent in text-only mode, stream text chunks as they come in real-time, and manage the
sync_transcription
setting: 1. Agent Configuration: Let's configure the agent to work in text-only mode and turn off audio:
Copy code
import asyncio
from livekit.agents import (
    AgentSession,
    RoomInputOptions,
    RoomOutputOptions,
)
from livekit.plugins import openai

class MyAgent:
    def __init__(self):
        self.instructions = "You are a helpful assistant."

async def entrypoint(ctx):
    session = AgentSession(
        llm=openai.LLM(model="gpt-4o-mini"),
    )
    await session.start(
        agent=MyAgent(),
        room=ctx.room,
        room_input_options=RoomInputOptions(text_enabled=True, audio_enabled=False),
        room_output_options=RoomOutputOptions(transcription_enabled=True, audio_enabled=False, sync_transcription=False),
    )
2. Streaming Text Chunks: Here’s how you can register a text stream handler to catch and display text chunks as they arrive:
Copy code
from livekit import rtc
from livekit.agents.types import (
    ATTRIBUTE_TRANSCRIPTION_FINAL,
    ATTRIBUTE_TRANSCRIPTION_SEGMENT_ID,
    ATTRIBUTE_TRANSCRIPTION_TRACK_ID,
    TOPIC_TRANSCRIPTION,
)

class TextStreamPrinter:
    def __init__(self):
        self._text_chunk_queue = asyncio.Queue()

    async def on_text_received(self, reader: rtc.TextStreamReader, participant_identity: str):
        async for chunk in reader:
            print(f"Received chunk from {participant_identity}: {chunk}")

        # Check the final flag
        final = reader.info.attributes.get(ATTRIBUTE_TRANSCRIPTION_FINAL, "null")
        print(f"Final flag: {final}")

# Register the handler
text_printer = TextStreamPrinter()
room.register_text_stream_handler(
    topic=TOPIC_TRANSCRIPTION, 
    handler=text_printer.on_text_received
)
3. Key Points to Keep in Mind: • Setting
sync_transcription=False
lets you get transcriptions right away, without waiting for synchronization. • Use the
lk.transcription
topic to catch text responses from the agent. • Process those text chunks as they come in by using the handler you registered. This example walks you through setting up and using a LiveKit agent for real-time text streaming in Python. If you want me to explain anything further or share more examples, just let me know — I’m here to help!