How can I get function calls chat items from the c...
# ask-ai
b
How can I get function calls chat items from the chat context?
t
Thanks for reaching out, miguel. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to grab function calls and their outputs from the chat context in LiveKit, you’ve got a couple of easy options. You can either use the
functionCalls
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:
Copy code
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:
Copy code
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!
b
I'm doing
Copy code
chat_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 calls
t
Hey there! Function calls might not show up in the chat context for a few reasons, so let’s walk through some common ones together: 1. Excluding Function Calls: If you’re using
exclude_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.py
b
it seems like tools called during the agent execution are included, but transition tools that handoff to another agent do not
t
Hey there! When you switch between agents in LiveKit, the chat context doesn’t automatically come along unless you specifically pass it during the handoff. So, if you’re using transition tools to move the conversation to another agent, just make sure to carry over the chat context to keep things seamless. Here’s a quick example:
Copy code
from 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 Docs
b
Yes, I'm already copying the chat
t
Hey there! Just a quick heads-up: function calls from transition tools might not show up in the chat context because the
copy
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:
Copy code
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