For client-sdk-unity, I'm sending text with topic ...
# ask-ai
m
For client-sdk-unity, I'm sending text with topic "mainTopic" and yet agent.py is showing "INFO root - ignoring text stream with topic 'mainTopic', no callback attached"
t
Thanks for reaching out, Leon. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The issue occurs because
agent.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:
Copy code
room.register_text_stream_handler('mainTopic', handle_text_stream)
1. Define the handler function to process the incoming text:
Copy 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}")
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!
m
Could you make an example agent.py?
t
Here’s a friendly example of
agent.py
that shows how to register a handler for the topic "mainTopic" and handle incoming text streams:
Copy code
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!
m
What is this, is this error? RuntimeWarning: coroutine 'handle_text_stream' was never awaited text_stream_handler(text_reader, participant_identity) RuntimeWarning: Enable tracemalloc to get the object allocation traceback
t
Hey there! That error
RuntimeWarning: 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:
Copy code
# 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.py
m
await def handle_text_stream(reader, participant_identity): ^^^ SyntaxError: invalid syntax
t
Hey there! The error
SyntaxError: 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:
Copy 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!
m
How do I make my agent reply to my text message with topic "mainTopic"
t
If you want your agent to reply to a text message with the topic "mainTopic," you can easily do this using the
sendText
method in the LiveKit SDK. Here’s a simple way to set it up:
Copy code
# 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:
Copy code
# 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!
m
"INFO root - Text stream received from identity" but no message from agent.py was sent to my Unity client.
t
Hey there! If your agent is getting the text stream but not sending a message back to the Unity client, let's double-check a few things together: 1. Correct Topic: Make sure both your agent and Unity client are on the same page with the topic, like "mainTopic" or "lk.chat". If they're not matching, just update the topic on both sides. 2. Send Text from Agent: Here’s a handy snippet for
agent.py
to send a reply:
Copy code
# 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:
Copy code
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:
Copy code
# 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.cs
m
Where should I put the following script in my agent.py?
# Send a reply to the Unity client
await room.local_participant.send_text(
"This is the agent's reply",
topic="mainTopic"
)
t
Hey there! You’ll want to place the script that sends a reply inside the handler function that deals with incoming text streams. So, if you have a handler function like
handle_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:
Copy 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}")

        # 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!
m
AttributeError: 'TextStreamReader' object has no attribute 'read_chunk'
t
Hey there! The error happens because the
TextStreamReader
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:
Copy 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:
Copy code
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.ts
m
Message from Unity is sent to the agent but no reply from agent is received.
t
Hey there! If your Unity client is sending a message to the agent but not getting a reply, no worries—let’s walk through some easy steps to figure out what’s going on and get it fixed: 1. Double-check the Topic Matching: Make sure the agent is replying on the exact same topic the Unity client is listening to. For example, if your Unity client listens on "mainTopic", the agent should send the reply like this:
Copy code
await 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:
Copy code
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:
Copy 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"
        )
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.md
m
"await room.local_participant.send_text" NameError: name 'room' is not defined.
t
Hey there! The error
NameError: 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:
Copy code
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:
Copy code
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!
m
livekit\rtc\room.py867 RuntimeWarning: coroutine 'handle_text_stream' was never awaited text_stream_handler(text_reader, participant_identity)
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!