Function Calling in the Gemini API
Pass functions as tools, enable automatic_function_calling in ChatSession, inspect FunctionCall/FunctionResponse history, and implement a manual calling loop.
Build a chatbot with three tools: get_weather(city), convert_currency(amount, from_currency, to_currency), and calculate_tax(income, rate). Test a multi-tool scenario with a single user message.
Copy and adapt to your context. Text in angle brackets should be replaced.
from google import genai
client = genai.Client(api_key=GOOGLE_API_KEY)
def get_stock_price(ticker: str) -> dict:
"""Get the current stock price for a given ticker symbol."""
prices = {"GOOGL": 175.0, "AAPL": 190.5, "MSFT": 415.2}
return {"ticker": ticker, "price": prices.get(ticker, 0), "currency": "USD"}
chat = client.chats.create(
model="gemini-2.5-flash",
config={"tools": [get_stock_price]}
)
response = chat.send_message("What is the current price of Apple and Google stocks?")
print(response.text)Using unsupported parameter types (only int, float, bool, str, list, dict are allowed); missing docstrings (the model won't understand the function's purpose); forgetting to send FunctionResponse in manual mode.