AI / Google Antigravity Gemini Fundamentals Interview Questions
How does function calling (tool use) work in the Gemini API?
Function calling allows you to declare external functions to the Gemini model. The model then decides when to call them, returning structured arguments you execute in your code. The result is sent back, and the model continues its response using the function output.
from google import genai from google.genai import types import json client = genai.Client() # 1. Declare your functions as tools get_weather = types.FunctionDeclaration( name="get_weather", description="Get current weather for a city", parameters={ "type": "object", "properties": { "city": {"type": "string", "description": "City name"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["city"] } ) # 2. First call: model may request a function call response = client.models.generate_content( model="gemini-3.5-flash", contents="What's the weather in Tokyo right now?", config=types.GenerateContentConfig( tools=[types.Tool(function_declarations=[get_weather])] ) ) # 3. Check if model requested a function call for part in response.candidates[0].content.parts: if hasattr(part, "function_call"): fc = part.function_call print(f"Model wants to call: {fc.name}({dict(fc.args)})") result = your_weather_api(fc.args["city"]) # execute! # 4. Return result and continue response2 = client.models.generate_content( model="gemini-3.5-flash", contents=[ types.Content(role="user", parts=[types.Part(text="What's the weather in Tokyo?")]), response.candidates[0].content, # model turn with function_call types.Content( role="user", parts=[types.Part( function_response=types.FunctionResponse( name=fc.name, response={"result": result} ) )] ) ], ) print(response2.text)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
