when I use the react - useChat hook the user messa...
# ask-ai
a
when I use the react - useChat hook the user message is sent, the agent generates a response but the frontend is not receiving it. however i am getting this in browser console - ignoring incoming text stream due to no handler for topic lk.transcription . Give me a fix and a python code snippet using livekit agents 1.0 to handle send text response.
t
Thanks for reaching out, Satvik. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! The issue you're seeing happens because there’s no handler set up for the
lk.transcription
topic. To fix this, you’ll want to explicitly register a text stream handler in your frontend using
room.registerTextStreamHandler
. Here’s a little example to get you started:
Copy code
room.registerTextStreamHandler("lk.transcription", (reader, participantInfo) => {
  reader.forEach((chunk) => {
    console.log(`Transcription from ${participantInfo.identity}: ${chunk}`);
  });
});
On the Python backend side, here’s a snippet using LiveKit Agents 1.0 to handle and send text responses smoothly:
Copy code
import logging
from dotenv import load_dotenv
from livekit.agents import (
    Agent,
    AgentSession,
    JobContext,
    RoomInputOptions,
    RoomOutputOptions,
    WorkerOptions,
    cli,
)
from livekit.plugins import openai

load_dotenv()

logger = logging.getLogger("text-only-agent")
logger.setLevel(logging.INFO)

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

async def entrypoint(ctx: JobContext):
    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),
    )
    logger.info("Text-only agent session started.")

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
Just make sure your backend is publishing responses to the
lk.transcription
topic, and that your frontend has the handler registered to catch and process those messages. If you need any more help with this, I’m here for you! Sources: packages/livekit-rtc/src/room.ts | src/room/data-stream/incoming/IncomingDataStreamManager.ts | lib/src/core/room.dart | hooks/useDataStreamTranscriptions.ts | Text and transcriptions | LiveKit Docs
a
doesn't the useChat hook supposed to handle the incoming chat messages by itself why is it not working in my case
t
The
useChat
hook is here to make handling incoming chat messages super easy for you. It gives you a
chatMessages
array that updates automatically whenever new messages pop up in the room. Each message comes with handy details like who sent it, what they said, and when. If it’s not working as expected on your end, it might be because of a few things: • There could be some hiccups with the data channel connection in your room. • Messages might be going to a different topic instead of the default chat topic (
lk.chat
). • Or maybe there are some custom settings or filters that are changing how messages are handled. Could you check if the messages are actually being sent to the
lk.chat
topic and if the data channel is up and running? That info would really help us figure out what’s going on. Sources: packages/react/src/prefabs/Chat.tsx | Add chat to your React app | LiveKit Docs