Principle:Googleapis Python genai Manual Function Execution
| Knowledge Sources | |
|---|---|
| Domains | Function_Calling, Tool_Use |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
A pattern for explicitly handling function call requests from a language model, executing them in application code, and returning results for continued generation.
Description
Manual Function Execution gives the application full control over the function calling loop. When the model generates function call requests (instead of text), the application extracts the function name and arguments, executes the function with custom logic (validation, error handling, user confirmation), constructs a function response part, and sends it back to the model for continued generation. This pattern is essential for scenarios requiring human-in-the-loop confirmation, async execution, custom error handling, or result transformation before feeding back to the model.
Usage
Use manual function execution when automatic function calling is insufficient: when functions have side effects requiring user confirmation, when function execution is asynchronous, when results need transformation or filtering before model consumption, or when custom error handling is required. Disable AFC and process response.function_calls directly.
Theoretical Basis
Manual function execution implements an explicit Agent Loop:
# Manual function execution loop (pseudo-code)
contents = [user_query]
while True:
response = model.generate(contents, tools, afc_disabled=True)
if response.function_calls:
for call in response.function_calls:
# Application controls execution
if user_confirms(call):
result = execute(call.name, call.args)
else:
result = {"error": "User declined"}
contents.append(model_turn_with_function_call)
contents.append(function_response(call.name, result))
else:
return response.text