adamant-gigabyte-17689
12/04/2025, 9:24 AMtall-airline-16756
12/04/2025, 9:24 AMtall-airline-16756
12/04/2025, 9:24 AMllm.tool() including a JSON schema (Zod) for parameters and an execute function:
import { llm, z } from '@livekit/agents';
const getWeather = llm.tool({
description: 'Get the weather for a given location',
parameters: z.object({ location: z.string().describe('The city and country') }),
execute: async ({ location }) => {
// call your weather API
return `The weather in ${location} is sunny and 75°F.`;
},
});
1. Create the RealtimeModel and pass tools via `geminiTools`:
const session = new voice.AgentSession({
llm: new google.realtime.RealtimeModel({
model: 'gemini-2.5-flash-native-audio-preview-09-2025',
geminiTools: [getWeather],
voice: 'Puck',
instructions: 'You are a helpful assistant.',
}),
});
1. When the model chooses a tool, it will call your execute and the returned result is added to the conversation context automatically. Use llm.handoff() if you need to transfer control after tool execution.
Python (Agents SDK)
1. Declare tools as function declarations (JSON schema) and include them in `_gemini_tools`:
from livekit.plugins import google
from google.genai import types
get_weather_decl = types.FunctionDeclaration(
name="getWeather",
description="Get the weather for a given location",
parameters={
"type": "object",
"properties": {
"location": {"type": "string", "description": "The city and country"}
},
"required": ["location"],
},
)
session = AgentSession(
llm=google.realtime.RealtimeModel(
model="gemini-2.5-flash-native-audio-preview-09-2025",
_gemini_tools=[get_weather_decl],
),
)
1. Implement the actual execution handler in your app that receives the function call, runs the tool logic (e.g., calls your weather API), and returns the result. Returning a string or structured data is supported. You can also return an Agent to hand off control.
Key notes and reminders
• Tools must expose a JSON schema for parameters (Zod in Node.js, JSON schema in Python).
• The RealtimeModel natively supports function/tool calling; tool outputs are inserted into the chat context so the model can produce the final spoken response.
• Use llm.handoff() (Node) or return an Agent (Python) if you need to change control flow after a tool runs.
• Ensure your Gemini API credentials (e.g., GOOGLE_API_KEY) are set in your environment.
Would you like a complete runnable example (Node or Python) wired to a real API call and execution handler?
Sources: Gemini Live API plugin | LiveKit docs | Tool definition and use | LiveKit docs