cold-oyster-96332
07/30/2025, 10:45 PMtall-airline-16756
07/30/2025, 10:45 PMtall-airline-16756
07/30/2025, 10:46 PMasync 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:
// JavaScript Example
const text = 'Hello, how are you today?';
const info = await room.localParticipant.sendText(text, {
topic: 'lk.chat',
});
// 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:
// 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.pycold-oyster-96332
07/30/2025, 10:57 PMtall-airline-16756
07/30/2025, 10:59 PM// 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:
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:
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 Docscold-oyster-96332
07/30/2025, 11:02 PMtall-airline-16756
07/30/2025, 11:02 PMsync_transcription setting:
1. Agent Configuration:
Let's configure the agent to work in text-only mode and turn off audio:
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:
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!