red-television-3815
07/13/2025, 7:54 AMtall-airline-16756
07/13/2025, 7:54 AMtall-airline-16756
07/13/2025, 7:55 AMChatRole.TOOL
role. The recommended approach is to use the ChatMessage.createToolFromFunctionResult
method, which formats the tool/function call output properly for the chat context.
Example:
import { ChatMessage, ChatRole } from '@livekit/agents';
// Assume you have a CallableFunctionResult object named functionResult
const toolResultMessage = ChatMessage.createToolFromFunctionResult(functionResult);
// Add the tool result to the chat context
callContext.addExtraChatMessage(toolResultMessage);
This will create a message with the TOOL
role, carrying your tool call result or error. This role is specifically designed for representing tool/function call outputs and ensures correct integration with the LLM system.
Sources: examples/src/outbound.ts | AgentCallContext | LiveKit Agentsred-television-3815
07/13/2025, 7:56 AMtall-airline-16756
07/13/2025, 7:57 AMFunctionCallOutput
object. I'll show you a quick example that should make this crystal clear:
from livekit.agents.llm import ChatContext, FunctionCallOutput
# Create a chat context
chat_ctx = ChatContext()
# Add a tool call result
tool_result = FunctionCallOutput(
call_id="tool_call_123", # Must match the original function call ID
name="search_calendar", # Name of the function that was called
output="Found 3 meetings scheduled for today: 9am standup, 2pm review, 4pm planning",
is_error=False # True if failed
)
chat_ctx.items.append(tool_result)
Pro tip: The "tool"
role is always the way to go for tool results. If you need to insert multiple results at once, you can use chat_ctx.insert()
. The best part? The SDK takes care of formatting for your LLM provider, so you don't have to worry about the technical details. Easy peasy!
Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime...red-television-3815
07/13/2025, 8:30 AMtall-airline-16756
07/13/2025, 8:30 AMfrom livekit.agents.llm import ChatContext, FunctionCallOutput
# Create or use your existing chat context
chat_ctx = ChatContext()
# Create the tool output
output = FunctionCallOutput(
call_id="tool_call_456", # Must match function call id
name="my_function",
output="Result of the function call.",
is_error=False
)
# Append to the context
chat_ctx.items.append(output)
Pro tip: If you're juggling multiple outputs and want to keep things in order, you can use chat_ctx.insert([output1, output2, ...])
. This helps make sure everything lands in the right sequence. Pretty cool, right?red-television-3815
07/13/2025, 8:31 AMtall-airline-16756
07/13/2025, 8:32 AMred-television-3815
07/13/2025, 10:48 AM