brave-island-45242
08/14/2025, 9:08 PMtall-airline-16756
08/14/2025, 9:08 PMtall-airline-16756
08/14/2025, 9:09 PMfunctionCalls
property of the LLMStream
or filter through the chatCtx
items. Here’s how you can do it:
Using LLMStream's functionCalls:
1. First, access the functionCalls
property:
const functionCalls = stream.functionCalls;
2. Then, loop through each function call to see the details and outputs:
functionCalls.forEach((callInfo) => {
console.log('Function Name:', callInfo.function.name);
console.log('Call ID:', callInfo.callId);
console.log('Arguments:', callInfo.arguments);
console.log('Output:', callInfo.output);
});
Using chatCtx to Filter Function Calls:
1. Start by getting the chat context:
const chatContext = stream.chatCtx;
2. Then, filter the items to find those function calls and their outputs:
const functionCallItems = chatContext.items.filter((item) =>
item.type === 'function_call' || item.type === 'function_call_output'
);
Both ways work well! The functionCalls
property is a quick and straightforward route, while filtering chatCtx
gives you a bit more context if you need it. Hope this helps you out!brave-island-45242
08/14/2025, 9:10 PMchat_history = session.current_agent.chat_ctx.copy()
chat_items: list[dict[str, Any]] = []
for item in chat_history.items:
item_dict: dict[str, Any] = {
"id": item.id,
"type": item.type,
"created_at": datetime.fromtimestamp(item.created_at, UTC).isoformat()
}
if item.type == "message":
# assert isinstance(item, ChatMessage)
item_dict["role"] = item.role
item_dict["interrupted"] = item.interrupted
item_dict["transcript_confidence"] = item.transcript_confidence
item_dict["text_content"] = item.text_content or str(item.content)
elif item.type == "function_call":
# assert isinstance(item, FunctionCall)
item_dict["call_id"] = item.call_id
item_dict["arguments"] = item.arguments
item_dict["name"] = item.name
elif item.type == "function_call_output":
# assert isinstance(item, FunctionCallOutput)
item_dict["name"] = item.name
item_dict["call_id"] = item.call_id
item_dict["output"] = item.output
item_dict["is_error"] = item.is_error
else:
raise ValueError(f"Unknown item type: {item.type}")
chat_items.append(item_dict)
But I'm not seeing any function callstall-airline-16756
08/14/2025, 9:11 PMexclude_function_call=True
when copying or trimming the chat context, it’ll leave out function calls and their results.
2. Real-time Context Updates: Make sure the chat context updates properly after function calls by double-checking that you’re using update_chat_ctx
as needed.
3. Function Execution Errors: Sometimes errors during function calls mean they don’t get added to the chat context. It’s a good idea to look out for error handling in your function outputs.
4. Context Truncation: If you’re trimming the context, recent items like function calls might get cut off. Take a peek at your truncation logic to make sure it’s keeping what you need.
5. Initialization Issues: Lastly, confirm that your chat context starts off right and that function calls are properly added after they run.
Give these a look and see if any fit your situation, especially if you’re working with the copy()
method or trimming the context. And hey, if you want me to help you dig deeper, just let me know—I’m here for you!
Sources: livekit-agents/livekit/agents/voice/generation.pybrave-island-45242
08/14/2025, 9:13 PMtall-airline-16756
08/14/2025, 9:13 PMfrom livekit.agents import Agent, function_tool
class SourceAgent(Agent):
@function_tool()
async def transfer_to_target_agent(self):
# Hand off to the target agent, preserving the chat context
return TargetAgent(chat_ctx=self.session.chat_ctx)
If you don’t pass the chat context explicitly, the new agent will start fresh without any previous conversation history. Feel free to reach out if you want me to walk you through it or if you have any other questions!
Sources: Workflows | LiveKit Docs | Workflows | LiveKit Docs | Agents v0.x migration guide | LiveKit Docsbrave-island-45242
08/14/2025, 9:48 PMtall-airline-16756
08/14/2025, 9:48 PMcopy
method of the ChatContext
class actually skips function calls by default. So, when you're copying the chat context during an agent handoff, make sure to set exclude_function_call=False
to include those function calls and their outputs.
Here’s a little example:
new_agent = HelpfulAssistant(
chat_ctx=self.session.chat_ctx.copy(exclude_function_call=False)
)
This way, the full conversation history—including all those function calls—gets passed along to the new agent. If you run into any issues or want me to walk you through it, just let me know—I’m here to help!
Sources: livekit-agents/livekit/agents/voice/run_result.py | Workflows | LiveKit Docs | agents/src/pipeline/pipeline_agent.ts