Implementation:Googleapis Python genai FunctionDeclaration And Tool
| Knowledge Sources | |
|---|---|
| Domains | Function_Calling, Tool_Use |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for declaring functions and tools that Gemini models can invoke during generation, provided by the google-genai types module.
Description
FunctionDeclaration defines a single function with its name, description, and parameter schema. FunctionDeclaration.from_callable auto-generates a declaration by inspecting a Python function's type annotations and docstring. Tool groups multiple FunctionDeclarations together. For MCP (Model Context Protocol) integration, McpToGenAiToolAdapter converts MCP tool definitions to genai-compatible Tool objects. The SDK supports passing Python callables directly as tools, which are auto-converted.
Usage
Use FunctionDeclaration for manual schema control. Pass Python functions directly in the tools list for automatic declaration generation (requires type annotations). Use McpToGenAiToolAdapter or MCP ClientSession objects in the tools list for external MCP tool servers.
Code Reference
Source Location
- Repository: googleapis/python-genai
- File: google/genai/types.py
- Lines: L3798 (FunctionDeclaration), L4001 (from_callable), L4226 (Tool)
- File: google/genai/_mcp_utils.py
- Lines: L54 (mcp_to_gemini_tools)
- File: google/genai/_adapters.py
- Lines: L26 (McpToGenAiToolAdapter)
Signature
class FunctionDeclaration(_common.BaseModel):
"""Structured representation of a function declaration (OpenAPI 3.0 spec)."""
name: Optional[str] = None
description: Optional[str] = None
parameters: Optional[Schema] = None
response: Optional[Schema] = None
behavior: Optional[Behavior] = None
@classmethod
def from_callable(
cls,
*,
client: 'BaseApiClient',
callable: Callable[..., Any],
behavior: Optional[Behavior] = None,
) -> 'FunctionDeclaration':
"""Auto-generates declaration from a Python function."""
class Tool(_common.BaseModel):
"""Tool containing function declarations."""
function_declarations: Optional[list[FunctionDeclaration]] = None
retrieval: Optional[Retrieval] = None
google_search: Optional[GoogleSearch] = None
code_execution: Optional[ToolCodeExecution] = None
computer_use: Optional[ComputerUse] = None
Import
from google.genai import types
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | Optional[str] | No | Function name (auto-extracted from callable) |
| description | Optional[str] | No | Function description (auto-extracted from docstring) |
| parameters | Optional[Schema] | No | JSON Schema of parameters |
| callable | Callable[..., Any] | Yes (from_callable) | Python function to auto-convert |
| function_declarations | Optional[list[FunctionDeclaration]] | No | List of declarations for Tool |
Outputs
| Name | Type | Description |
|---|---|---|
| FunctionDeclaration | FunctionDeclaration | A function declaration for model tool use |
| Tool | Tool | A tool containing one or more function declarations |
Usage Examples
Auto-Generated from Python Function
from google import genai
from google.genai import types
client = genai.Client(api_key="YOUR_API_KEY")
def get_weather(location: str) -> str:
"""Get the current weather for a given location.
Args:
location: The city name to get weather for.
Returns:
Weather description string.
"""
return f"Sunny, 72°F in {location}"
# Pass function directly - SDK auto-generates FunctionDeclaration
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="What's the weather in San Francisco?",
config=types.GenerateContentConfig(
tools=[get_weather],
),
)
print(response.text)
Manual FunctionDeclaration
from google.genai import types
weather_func = types.FunctionDeclaration(
name="get_weather",
description="Get current weather for a location",
parameters=types.Schema(
type="OBJECT",
properties={
"location": types.Schema(
type="STRING",
description="City name"
)
},
required=["location"]
),
)
tool = types.Tool(function_declarations=[weather_func])