Intro to Function Calling with the Gemini API & Python SDK
Source notebook
Repo path:
gemini/function-calling/intro_function_calling.ipynb· Open on GitHub · intermediate
Introduces Gemini function calling in Vertex AI with chat tools, mock API responses, and live geocoding.
Summary
This notebook teaches how to define callable functions for Gemini using the Google Gen AI SDK on Vertex AI. It walks through a Google Store chat flow with function declarations, tool configuration, multi-turn function responses, and multiple function calls in one turn. It also demonstrates automatic function calling by extracting address fields and calling the OpenStreetMap Nominatim API for coordinates.
Key code patterns
Create Vertex AI GenAI client
from google import genai
LOCATION = "global"
client = genai.Client(
enterprise=True,
project=PROJECT_ID,
location=LOCATION,
)
MODEL_ID = "gemini-3.5-flash"Initializes the Gemini API in Vertex AI for the selected Google Cloud project and global location.
Declare functions as tools
get_product_info = FunctionDeclaration(
name="get_product_info",
description="Get the stock amount and identifier for a given product",
parameters={"type": "object", "properties": {"product_name": {"type": "string"}}},
)
retail_tool = Tool(function_declarations=[
get_product_info,
get_store_location,
place_order,
])Shows how function schemas guide Gemini to produce structured function names and arguments.
Start a tool-enabled chat
chat = client.chats.create(
model=MODEL_ID,
config=GenerateContentConfig(
temperature=0,
tools=[retail_tool],
),
)Attaches tools once to a multi-turn chat session and uses low temperature for more deterministic parameters.
Return external API payloads
response = chat.send_message(prompt)
api_response = {"sku": "GA04834-US", "in_stock": "yes"}
response = chat.send_message(
Part.from_function_response(
name="get_product_info",
response={"content": api_response},
)
)Demonstrates the loop of receiving a function call, executing or simulating it, and sending results back to Gemini.
Automatic function calling
response = client.models.generate_content(
model=MODEL_ID,
contents=prompt,
config=GenerateContentConfig(
tools=[get_location],
temperature=0,
),
)Uses a Python function directly as a tool so Gemini extracts address fields and invokes it automatically.
Models & APIs used
- Models: gemini-3.5-flash
- APIs / services: Vertex AI, OpenStreetMap Nominatim API
- SDKs / libraries:
google-genai,requests
When to use this
Use this pattern when a Gemini app must turn natural language into structured calls to APIs, databases, or business functions.
Gotchas & caveats
- Requires an existing Google Cloud project with the Vertex AI API enabled.
- Colab requires authenticate_user(); Vertex AI Workbench does not require that Colab-only step.
- Vertex AI usage is billable.
- Function parameters are specified using OpenAPI JSON schema format.
- The model may return multiple back-to-back or parallel function calls in one conversation turn.
- Temperature 0 is mostly deterministic, but the notebook notes a small amount of variation is still possible.
- The live geocoding example depends on an external Nominatim HTTP request and catches requests.RequestException by returning an empty list.
Best practices
- Define clear functions with specific parameters and data types instead of parsing freeform text.
- Set temperature=0 for functions that require deterministic parameter values.
- Attach tools when creating the chat session to avoid sending them with every request.
- Send external system results back with Part.from_function_response before asking Gemini for the final user-facing answer.
- Handle multiple function calls in one turn by returning a list of function responses.
- Use synthetic API payloads while focusing on function-call extraction and orchestration.
- Filter None values from API parameters and handle request errors in live tool functions.
Related
- Concepts: Getting Started · Function Calling & Tools · Applied Use Cases
- Entities: Vertex AI · Google GenAI SDK · Function Calling · Gemini
- Area: Gemini Notebooks
- Best practices: Getting Started - Best Practices · Function Calling & Tools - Best Practices · Applied Use Cases - Best Practices