handsome-easter-5670
07/09/2025, 5:00 PMtall-airline-16756
07/09/2025, 5:00 PMtall-airline-16756
07/09/2025, 5:01 PM@function_tool
decorator. For example:
@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`:
realtime_model = RealtimeModel()
tools = [add_numbers]
realtime_model.update_tools(tools)
1. Create a RealtimeSession
instance and start the session:
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:
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.pyhandsome-easter-5670
07/09/2025, 5:10 PMtall-airline-16756
07/09/2025, 5:10 PM_handle_tool_output_content_event
as part of your RealtimeSession
class. Trust me, it'll make your code flow so much smoother!