Implementation:Googleapis Python genai Models Generate Content AFC
| Knowledge Sources | |
|---|---|
| Domains | Function_Calling, Generative_AI |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for generating content with automatic function calling (AFC) support provided by the google-genai models module.
Description
When tools are provided in the GenerateContentConfig, Models.generate_content enters the Automatic Function Calling (AFC) loop. The SDK builds a function map from the declared tools (Python callables, FunctionDeclarations, or MCP sessions), invokes functions when the model requests them via get_function_response_parts, and continues the generation loop until the model returns a text response or the maximum call limit is reached. The AFC history is available in response.automatic_function_calling_history. The core AFC logic lives in _extra_utils.py.
Usage
Pass tools in the config.tools parameter of generate_content. By default, AFC is enabled and will automatically execute Python callables. To handle function execution manually, set automatic_function_calling_config.disable=True and process response.function_calls yourself.
Code Reference
Source Location
- Repository: googleapis/python-genai
- File: google/genai/models.py
- Lines: L5507-5666 (generate_content with AFC loop)
- File: google/genai/_extra_utils.py
- Lines: L150 (get_function_map), L305 (invoke_function_from_dict_args), L333 (get_function_response_parts), L417 (should_disable_afc)
Signature
class Models:
def generate_content(
self,
*,
model: str,
contents: types.ContentListUnionDict,
config: Optional[types.GenerateContentConfigOrDict] = None,
) -> types.GenerateContentResponse:
"""Generates content, with optional AFC loop for tool use.
When config.tools contains callables and AFC is not disabled,
the method automatically executes function calls and continues
generation until a text response is produced.
"""
Import
from google import genai
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | str | Yes | Model resource ID |
| contents | ContentListUnionDict | Yes | Input content |
| config.tools | Optional[list[ToolUnion]] | No | Tool definitions (functions, FunctionDeclarations, MCP sessions) |
| config.automatic_function_calling_config | Optional[AutomaticFunctionCallingConfig] | No | AFC behavior settings |
| config.tool_config | Optional[ToolConfig] | No | Function calling mode (AUTO/ANY/NONE) |
Outputs
| Name | Type | Description |
|---|---|---|
| GenerateContentResponse | GenerateContentResponse | Final response after AFC loop completes; .text has the answer, .automatic_function_calling_history has the full loop history |
Usage Examples
Automatic Function Calling
from google import genai
from google.genai import types
client = genai.Client(api_key="YOUR_API_KEY")
def get_weather(location: str) -> str:
"""Get current weather for a location."""
return f"Sunny, 72°F in {location}"
def get_time(timezone: str) -> str:
"""Get current time in a timezone."""
return f"2:30 PM in {timezone}"
# AFC automatically calls functions as needed
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="What's the weather and time in New York?",
config=types.GenerateContentConfig(
tools=[get_weather, get_time],
),
)
print(response.text)
# The model called both functions and synthesized the results
Check AFC History
# After AFC completes, inspect the full interaction history
if response.automatic_function_calling_history:
for turn in response.automatic_function_calling_history:
print(f"Role: {turn.role}")
for part in turn.parts:
if part.function_call:
print(f" Called: {part.function_call.name}({part.function_call.args})")
if part.function_response:
print(f" Result: {part.function_response.response}")