| name | function-calling |
| description | Registering custom Python tools, handling parallel function calls, and executing callback routines. |
| allowed-tools | Read Write Edit Bash |
| license | MIT license |
| metadata | {"skill-author":"Lord1Egypt"} |
Gemini Function Calling & Tool Integration Skill
Overview
This skill describes how to connect Gemini models to external tool APIs. By declaring Python functions as tools, the model returns structured tool call instructions when it needs to retrieve live data, execute calculations, or write data. The calling application executes the actual code and returns the output to Gemini to compile a final answer.
When to Use This Skill
- Fetching live info from databases, web endpoints, or hardware sensors.
- Performing strict computations (e.g. currency conversion, SQL executions).
- Connecting agent workflows to system commands or external systems (like sending slack alerts or Telegram updates).
Quick Start (with runnable code examples)
import json
from google import genai
client = genai.Client()
def fetch_weather(location: str) -> str:
"""Retrieve the current weather status for a given city location.
Args:
location: The name of the city (e.g., Cairo, London, Tokyo)
"""
db = {
"cairo": "Sunny and hot, 38°C",
"london": "Overcast and rainy, 14°C",
"tokyo": "Mild and humid, 22°C"
}
loc = location.strip().lower()
return db.get(loc, f"Moderate and clear, 20°C in {location}")
def calculate_tax(amount: float, rate: float = 0.14) -> float:
"""Calculate the tax amount for a transaction.
Args:
amount: The base transaction money amount
rate: The tax rate percentage decimal (default is 0.14 for 14%)
"""
return round(amount * rate, 2)
def run_tool_use_agent(user_query: str):
print(f"User Request: '{user_query}'")
response = client.models.generate_content(
model='gemini-2.5-flash',
contents=user_query,
config=dict(
tools=[fetch_weather, calculate_tax]
)
)
function_calls = response.function_calls
if function_calls:
print("\n--- Gemini Requested Function Calls ---")
history = [response.candidates[0].content]
for call in function_calls:
name = call.name
args = call.args
print(f"Executing local function '{name}' with arguments: {args}")
if name == "fetch_weather":
result = fetch_weather(**args)
elif name == "calculate_tax":
result = calculate_tax(**args)
else:
result = "Error: Tool not found"
print(f"Result: {result}")
history.append({
"role": "tool",
"parts": [{
"function_response": {
"name": name,
"response": {"result": result}
}
}]
})
final_response = client.models.generate_content(
model='gemini-2.5-flash',
contents=history,
config=dict(
tools=[fetch_weather, calculate_tax]
)
)
print("\n--- Final Grounded Output ---")
print(final_response.text)
else:
print("\nGemini did not require any function calls.")
print(response.text)
if __name__ == "__main__":
run_tool_use_agent("I need the weather in Cairo, and please calculate the 14% tax on a $250 invoice.")
Advanced Usage
Forcing Specific Modes (Function Calling Constraints)
You can force Gemini to call a specific function, or run purely in text mode without using tools:
response = client.models.generate_content(
model='gemini-2.5-flash',
contents="Analyze cairo",
config=dict(
tools=[fetch_weather],
tool_config={
"function_calling_config": {
"mode": "ANY"
}
}
)
)
Key References
Dependencies