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)
More Related questions...