how to get function tools result in realtimeSessio...
# ask-ai
h
how to get function tools result in realtimeSession?
t
Thanks for reaching out, YUNYI. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To get function tools results in a LiveKit realtimeSession, follow these steps: 1. Define your function tools using the
@function_tool
decorator. For example:
Copy code
@function_tool
async def add_numbers(self, context: RunContext, a: float, b: float):
    print(f"Adding {a} and {b}")
    return str(a + b)
1. Create an instance of the
RealtimeModel
class and update the tools using `update_tools`:
Copy code
realtime_model = RealtimeModel()
tools = [add_numbers]
realtime_model.update_tools(tools)
1. Create a
RealtimeSession
instance and start the session:
Copy code
session = realtime_model.session()
session.start()
1. Handle the tool output content event to retrieve function tool results by implementing
_handle_tool_output_content_event
asynchronously:
Copy code
async def _handle_tool_output_content_event(self, event_data: dict) -> None:
    tool_use_id = event_data["event"]["toolUse"]["toolUseId"]
    tool_name = event_data["event"]["toolUse"]["toolName"]
    args = event_data["event"]["toolUse"]["content"]
    tool_result = await self.tools.function_tools[tool_name](**json.loads(args))
    print(f"Tool {tool_name} result: {tool_result}")
Key points: • Use
update_tools
on
RealtimeModel
to register your function tools. • Start the realtime session. • Implement and await
_handle_tool_output_content_event
on
RealtimeSession
to process results. This way, you get the function tools' output in realtime in your session. Sources: livekit-agents/livekit/agents/voice/agent_activity.py | realtime/openai-realtime-tools.py
h
async def _handle_tool_output_content_event(self, event_data: dict) -> None: tool_use_id = event_data["event"]["toolUse"]["toolUseId"] tool_name = event_data["event"]["toolUse"]["toolName"] args = event_data["event"]["toolUse"]["content"] tool_result = await self.tools.function_tools[tool_name](**json.loads(args)) print(f"Tool {tool_name} result: {tool_result}") is this inside realtimeSession?
t
Yes, the method _handle_tool_output_content_event is inside the RealtimeSession class. Let me break this down in a friendly way – it's basically the method that kicks into gear when a tool's results come back during a realtime session.Think of it like a helpful assistant that jumps in to process the event data, figures out which tool was used and what arguments it had, then runs the corresponding function tool behind the scenes. Pretty neat, right?So, you'll want to roll up your sleeves and implement
_handle_tool_output_content_event
as part of your
RealtimeSession
class. Trust me, it'll make your code flow so much smoother!