nice-room-70150
09/14/2025, 10:14 PMtall-airline-16756
09/14/2025, 10:14 PMtall-airline-16756
09/14/2025, 10:14 PMconversation_item_added
event. This event gives you a ChatMessage
object that includes helpful metadata to identify where the message came from.
Here are the key fields you’ll want to check:
• role
: This tells you who sent the message. If role == "user"
, it means the message was typed by the user. If role == "assistant"
, then it was generated by the agent.
• generated
: This is a boolean that shows if the message was created by an agent from user transcription.
Here’s a quick example to illustrate:
from livekit.agents import ConversationItemAddedEvent
@session.on("conversation_item_added")
def on_conversation_item_added(event: ConversationItemAddedEvent):
msg = event.item
if msg.role == "user":
print("Message is user-typed.")
elif msg.role == "assistant":
print("Message is agent-generated.")
print(f"Generated from transcription: {msg.generated}")
print(f"Message ID: {msg.id}, Timestamp: {msg.timestamp}")
print(f"Content: {msg.message}")
Using this method, you can confidently distinguish between messages from users and agents. If you have any questions or need further help, feel free to ask—I’m here to support you!
Sources: Events and error handling | LiveKit Docs | livekit-plugins/livekit-plugins-ultravox/livekit/plugins/ultravox/realtime/... | livekit-protocol/livekit/protocol/models.pyi | livekit-protocol/src/livekit.rs | Turn detection and interruptions | LiveKit Docsnice-room-70150
09/14/2025, 10:30 PMtall-airline-16756
09/14/2025, 10:31 PMadd_message()
function inside a ChatContext
. This lets you specify roles like "user"
, "assistant"
, "system"
, or "tool"
.
Here's a quick example to guide you:
from livekit.agents import ChatContext
async def on_user_turn_completed(self, turn_ctx: ChatContext):
turn_ctx.add_message(
role="assistant",
content="Here is the response from the assistant."
)
await self.update_chat_ctx(turn_ctx) # Persist the message in chat history
Some important things to keep in mind:
• Use add_message()
whenever you want to add messages with specific roles.
• Don't forget to call update_chat_ctx()
so your message gets saved in the chat history.
• Roles like "user"
, "assistant"
, and "system"
are usually supported, but just a heads-up—this might vary depending on your LLM integration.
If you'd like me to walk you through more examples or if anything feels unclear, please feel free to ask. I'm here to help!
Sources: Pipeline nodes and hooks | LiveKit Docs | models/templates/CohereForAI-c4ai-command-r-plus-tool_use.jinja | Docs::Agents | Testing and evaluation | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docsnice-room-70150
09/14/2025, 11:05 PMtall-airline-16756
09/14/2025, 11:06 PM