miniature-morning-62153
08/07/2025, 9:36 AMtall-airline-16756
08/07/2025, 9:36 AMtall-airline-16756
08/07/2025, 9:37 AMagent.py
does not have a callback registered for the topic "mainTopic". To resolve this:
1. In agent.py
, register a handler for the topic "mainTopic" using room.register_text_stream_handler()
. For example:
room.register_text_stream_handler('mainTopic', handle_text_stream)
1. Define the handler function to process the incoming text:
async def handle_text_stream(reader, participant_identity):
while True:
chunk = await reader.read_chunk()
if chunk.is_end_of_stream:
break
print(f"Received text from {participant_identity}: {chunk.text}")
1. Ensure the topic names match exactly between the Unity SDK and agent.py
. If you prefer a different topic, update both sides accordingly.
Let me know if you need further assistance!miniature-morning-62153
08/07/2025, 9:39 AMtall-airline-16756
08/07/2025, 9:39 AMagent.py
that shows how to register a handler for the topic "mainTopic" and handle incoming text streams:
import logging
from livekit import agents
logging.basicConfig(level=<http://logging.INFO|logging.INFO>)
# This function handles the text stream from participants
async def handle_text_stream(reader, participant_identity):
<http://logging.info|logging.info>(f"Text stream received from {participant_identity}")
while True:
chunk = await reader.read_chunk()
if chunk.is_end_of_stream:
break
<http://logging.info|logging.info>(f"Text: {chunk.text}")
# This is where the agent starts running
async def entrypoint(job_context: agents.JobContext):
room = job_context.room
# Here we register the handler for the "mainTopic" text stream
room.register_text_stream_handler('mainTopic', handle_text_stream)
# You can add more logic here if needed
<http://logging.info|logging.info>("Agent is up and running, ready to process text streams.")
if __name__ == "__main__":
agents.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint))
To sum it up:
• We register a handler for the topic "mainTopic" using room.register_text_stream_handler()
.
• It processes incoming text streams and logs the text it receives.
• The agent is set up to run smoothly with the agents.run_app
function.
If you’d like me to help customize this or add any extra features, just let me know—I’m here to help!miniature-morning-62153
08/07/2025, 9:51 AMtall-airline-16756
08/07/2025, 9:52 AMRuntimeWarning: coroutine 'handle_text_stream' was never awaited
pops up because the coroutine handle_text_stream
is being called without using the await
keyword. In Python, async functions (coroutines) need to be awaited so they can run properly.
No worries though! To fix this, just make sure you use await
when calling handle_text_stream
. Like this:
# Correct usage
await handle_text_stream(reader, participant_identity)
Also, if you're using room.register_text_stream_handler
, double-check that the handler is registered correctly and awaited inside. And if you're calling the handler yourself, remember to always use await
.
If you want, feel free to share the part of your code where this error shows up—I’d be happy to help you troubleshoot it more closely!
Sources: livekit-agents/livekit/agents/voice/agent.py | examples/data-streams/data_streams.pyminiature-morning-62153
08/07/2025, 9:53 AMtall-airline-16756
08/07/2025, 9:53 AMSyntaxError: invalid syntax
pops up because you can’t use the keyword await
with def
. Instead, you’ll want to use async def
when defining an asynchronous function—this little change will do the trick!
Here’s the corrected version of your code:
async def handle_text_stream(reader, participant_identity):
while True:
chunk = await reader.read_chunk()
if chunk.is_end_of_stream:
break
print(f"Received text from {participant_identity}: {chunk.text}")
Just remember to swap out await def
for async def
whenever you’re working with async functions.
If you have any more questions or want me to walk you through anything else, just let me know—I’m here to help!miniature-morning-62153
08/08/2025, 2:09 AMtall-airline-16756
08/08/2025, 2:10 AMsendText
method in the LiveKit SDK. Here’s a simple way to set it up:
# Send a reply to the topic "mainTopic"
await room.local_participant.send_text(
"This is the agent's reply",
topic="mainTopic"
)
And if your message is a bit long and you'd like to send it in parts, the stream_text
method is perfect for that:
# Stream a reply in chunks
stream_writer = await room.local_participant.stream_text(
topic="mainTopic"
)
await stream_writer.write("This is the first part of the reply.")
await stream_writer.write(" This is the second part.")
await stream_writer.aclose()
Just a quick reminder to make sure your agent is properly authenticated and registered to handle the "mainTopic" topic. If you hit any bumps or want to chat more about it, I’m here to help!miniature-morning-62153
08/08/2025, 2:16 AMtall-airline-16756
08/08/2025, 2:16 AMagent.py
to send a reply: # Send a reply to the Unity client
await room.local_participant.send_text(
"This is the agent's reply",
topic="mainTopic"
)
3. Unity Client Configuration: Double-check that your Unity client is set up to receive text streams properly. For example: IEnumerator HandleTextStream(TextStreamReader reader, string participantIdentity)
{
Debug.Log($"Text stream received from {participantIdentity}");
while (true)
{
var chunk = await reader.ReadAsync();
if (chunk.isComplete)
{
break;
}
Debug.Log($"Received text chunk: {chunk.text}");
}
}
4. Stream Text (Optional): If you’d like to send the reply in chunks, you can use the stream_text
method like this: # Stream a reply in chunks
stream_writer = await room.local_participant.stream_text(
topic="mainTopic"
)
await stream_writer.write("This is the first part of the reply.")
await stream_writer.write(" This is the second part.")
await stream_writer.aclose()
If you hit any bumps or want me to take a look at specific errors in your logs, just let me know—I’m here to help!
Sources: Agents v0.x migration guide | LiveKit Docs | Runtime/Scripts/Internal/FFIClient.csminiature-morning-62153
08/08/2025, 2:19 AM# Send a reply to the Unity client
await room.local_participant.send_text(
"This is the agent's reply",
topic="mainTopic"
)
tall-airline-16756
08/08/2025, 2:19 AMhandle_text_stream
, that’s the perfect spot to include the script to send a reply whenever a message comes in.
Here’s a simple example to give you an idea:
async def handle_text_stream(reader, participant_identity):
while True:
chunk = await reader.read_chunk()
if chunk.is_end_of_stream:
break
print(f"Received text from {participant_identity}: {chunk.text}")
# Send a reply to the Unity client
await room.local_participant.send_text(
"This is the agent's reply",
topic="mainTopic"
)
Just a quick heads-up: make sure the room
object is available inside your handler function. If it’s not, you might need to pass it in as an argument or use a global variable to access it.
If anything’s unclear or you want me to walk you through it step-by-step, just let me know—I’m here to help!miniature-morning-62153
08/08/2025, 2:22 AMtall-airline-16756
08/08/2025, 2:22 AMTextStreamReader
object doesn’t have a read_chunk
method. No worries though—you can use the asynchronous iterator that TextStreamReader
provides to handle the text stream smoothly.
Here’s a friendly tweak you can make to your code:
async def handle_text_stream(reader, participant_identity):
async for chunk in reader:
print(f"Received text from {participant_identity}: {chunk.text}")
# Send a reply to the Unity client
await room.local_participant.send_text(
"This is the agent's reply",
topic="mainTopic"
)
If you’d rather read the whole stream at once, you can use the read_all
method like this:
async def handle_text_stream(reader, participant_identity):
full_text = await reader.read_all()
print(f"Received full text from {participant_identity}: {full_text}")
# Send a reply to the Unity client
await room.local_participant.send_text(
"This is the agent's reply",
topic="mainTopic"
)
Feel free to reach out if you want me to help with anything else!
Sources: Tests/LiveKitTests/DataStream/TextStreamReaderTests.swift | src/room/data-stream/incoming/IncomingDataStreamManager.tsminiature-morning-62153
08/08/2025, 2:31 AMtall-airline-16756
08/08/2025, 2:32 AMawait room.local_participant.send_text(
"This is the agent's reply",
topic="mainTopic"
)
2. Look at Unity Client Setup: Confirm that the Unity client has a handler registered for that topic. Something like this: room.RegisterTextStreamHandler("mainTopic", HandleTextStream);
And here’s a simple way to implement that handler:
IEnumerator HandleTextStream(TextStreamReader reader, string participantIdentity)
{
while (true)
{
var chunk = await reader.ReadAsync();
if (chunk.isComplete)
{
break;
}
Debug.Log($"Received text chunk: {chunk.text}");
}
}
3. Check the Agent’s Reply Logic: Make sure the agent is actually sending the reply inside the text stream handler. For example: async def handle_text_stream(reader, participant_identity):
async for chunk in reader:
print(f"Received text from {participant_identity}: {chunk.text}")
# Send a reply to the Unity client
await room.local_participant.send_text(
"This is the agent's reply",
topic="mainTopic"
)
4. Peek at the Logs: Take a look at the Unity client console and agent logs for any error messages that might give clues about why the reply isn’t showing up.
5. Check Network Connections: Make sure both your Unity client and agent are connected properly to the LiveKit room and can talk to each other without issues.
If you hit any snags or spot any errors in your logs, just let me know—I’m here to help you get this sorted out!
Sources: RPC | LiveKit Docs | README.mdminiature-morning-62153
08/08/2025, 2:37 AMtall-airline-16756
08/08/2025, 2:37 AMNameError: name 'room' is not defined
pops up because the room
object isn’t available where you’re trying to use it. To fix this, just make sure the room
object is properly passed or set up in your code.
Here’s a simple way to sort it out:
1. Pass the Room Object to the Handler: If you’re working with a handler function like handle_text_stream
, just pass the room
object as an argument: async def handle_text_stream(reader, participant_identity, room):
async for chunk in reader:
print(f"Received text from {participant_identity}: {chunk.text}")
# Send a reply to the Unity client
await room.local_participant.send_text(
"This is the agent's reply",
topic="mainTopic"
)
2. Ensure Room Initialization: Double-check that the room
object is initialized in your agent’s entry point and passed along to the handler. For example: async def entrypoint(job_context):
room = job_context.room
# Register the text stream handler
room.register_text_stream_handler('mainTopic', lambda reader, identity: handle_text_stream(reader, identity, room))
If you have any questions or want me to walk you through it, just let me know—I’m happy to help!miniature-morning-62153
08/12/2025, 7:45 AMtall-airline-16756
08/12/2025, 7:46 AM