Implementation:Googleapis Python genai Part From Function Response
| Knowledge Sources | |
|---|---|
| Domains | Function_Calling, Tool_Use |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for constructing function response parts to feed execution results back to the model provided by the google-genai types module.
Description
Part.from_function_response creates a Part containing the result of a function execution. It takes the function name (matching the name from the model's function call request) and a response dictionary with the result data. This part is appended to the conversation contents and sent back to the model in the next generate_content call. The model uses the function result to produce its final text response.
Usage
Use Part.from_function_response after manually executing a function requested by the model. The name must match the function name from response.function_calls. The response dict should contain the function's return value as a structured dictionary.
Code Reference
Source Location
- Repository: googleapis/python-genai
- File: google/genai/types.py
- Lines: L1789-1799
Signature
class Part(_common.BaseModel):
@classmethod
def from_function_response(
cls,
*,
name: str,
response: dict[str, Any],
parts: Optional[list[FunctionResponsePart]] = None,
) -> 'Part':
"""Creates a Part from a function execution result.
Args:
name: Function name (must match the FunctionCall.name).
response: Function result as a dictionary.
parts: Optional multimodal response parts.
"""
Import
from google.genai import types
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | str | Yes | Function name matching the model's FunctionCall.name |
| response | dict[str, Any] | Yes | Function execution result as a dictionary |
| parts | Optional[list[FunctionResponsePart]] | No | Optional multimodal response parts |
Outputs
| Name | Type | Description |
|---|---|---|
| Part | Part | A Part containing the function response, ready to append to contents |
Usage Examples
Manual Function Calling Loop
from google import genai
from google.genai import types
client = genai.Client(api_key="YOUR_API_KEY")
# Define tools but disable AFC
def get_weather(location: str) -> dict:
return {"temperature": "72°F", "condition": "Sunny"}
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="What's the weather in London?",
config=types.GenerateContentConfig(
tools=[get_weather],
automatic_function_calling_config=types.AutomaticFunctionCallingConfig(
disable=True
),
),
)
# Check for function calls
if response.function_calls:
# Build the conversation for the next call
contents = [
types.Content(
parts=[types.Part.from_text(text="What's the weather in London?")],
role="user"
),
response.candidates[0].content, # Model's function call turn
]
# Execute each function and create response parts
function_response_parts = []
for fc in response.function_calls:
result = get_weather(**fc.args) # Execute manually
function_response_parts.append(
types.Part.from_function_response(
name=fc.name,
response=result
)
)
# Send results back to model
contents.append(
types.Content(parts=function_response_parts, role="user")
)
final_response = client.models.generate_content(
model="gemini-2.0-flash",
contents=contents,
config=types.GenerateContentConfig(tools=[get_weather]),
)
print(final_response.text)