Implementation:Googleapis Python genai GenerateContentResponse Access
| Knowledge Sources | |
|---|---|
| Domains | NLP, Data_Extraction |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for accessing generated text, parts, function calls, and metadata from model responses provided by the google-genai types module.
Description
GenerateContentResponse is the Pydantic model returned by generate_content and generate_content_stream. It provides convenience properties for common access patterns: .text returns concatenated text from the first candidate, .parts returns the raw parts list, .function_calls returns any function call requests from the model, and .usage_metadata provides token counting information. The .candidates field provides access to all response candidates with their safety ratings and finish reasons.
Usage
Access .text for simple text extraction. Use .function_calls in function-calling workflows to determine which functions the model wants to invoke. Use .usage_metadata for cost tracking and monitoring. Check .candidates[0].finish_reason to understand why generation stopped (STOP, MAX_TOKENS, SAFETY, etc.).
Code Reference
Source Location
- Repository: googleapis/python-genai
- File: google/genai/types.py
- Lines: L6781-6920
Signature
class GenerateContentResponse(_common.BaseModel):
"""Response from generate_content."""
candidates: Optional[list[Candidate]] = None
model_version: Optional[str] = None
usage_metadata: Optional[GenerateContentResponseUsageMetadata] = None
automatic_function_calling_history: Optional[list[Content]] = None
@property
def text(self) -> Optional[str]:
"""Concatenated text from all text parts in the first candidate."""
@property
def parts(self) -> Optional[list[Part]]:
"""Parts list from the first candidate's content."""
@property
def function_calls(self) -> Optional[list[FunctionCall]]:
"""Function calls from the first candidate."""
Import
from google.genai import types
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (returned by generate_content) | GenerateContentResponse | N/A | This is a return type, not directly constructed by users |
Outputs
| Name | Type | Description |
|---|---|---|
| .text | Optional[str] | Concatenated text from all text parts in the first candidate |
| .parts | Optional[list[Part]] | Raw parts list from the first candidate |
| .function_calls | Optional[list[FunctionCall]] | Function call requests from the model |
| .candidates | Optional[list[Candidate]] | All response candidates with safety ratings and finish reasons |
| .usage_metadata | Optional[GenerateContentResponseUsageMetadata] | Token counts: prompt_token_count, candidates_token_count, total_token_count |
Usage Examples
Basic Text Extraction
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="What is Python?"
)
# Simple text access
print(response.text)
# Token usage
print(f"Prompt tokens: {response.usage_metadata.prompt_token_count}")
print(f"Response tokens: {response.usage_metadata.candidates_token_count}")
print(f"Total tokens: {response.usage_metadata.total_token_count}")
Checking Finish Reason
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="Tell me a story."
)
# Check why generation stopped
candidate = response.candidates[0]
print(f"Finish reason: {candidate.finish_reason}")
# Possible values: STOP, MAX_TOKENS, SAFETY, RECITATION, OTHER
Streaming Response Processing
full_text = ""
for chunk in client.models.generate_content_stream(
model="gemini-2.0-flash",
contents="Write an essay about AI ethics."
):
if chunk.text:
full_text += chunk.text
print(chunk.text, end="")
print(f"\nTotal length: {len(full_text)}")