generate_content method is the primary way to interact with Gemini models for text generation. This guide covers different ways to structure your inputs and handle responses.
Basic Text Generation
The simplest way to generate content is to pass a string prompt:Understanding the Contents Parameter
The SDK converts all inputs to thecontents parameter into list[types.Content]. You can structure your inputs in several ways:
- String Input
- List of Strings
- Content Object
- List of Content
The simplest format - SDK converts it to a text part:The SDK converts this to:Where
types.UserContent is a subclass of types.Content with role='user'.Working with Parts
Parts are the building blocks of content. You can mix different types:Text Parts
types.UserContent:
List of Parts
types.UserContent:
Response Handling
The response object provides several ways to access the generated content:Image Output Generation
Some models likegemini-2.5-flash-image can generate images:
Using Uploaded Files
You can reference uploaded files in your prompts (Gemini Developer API only):Mixed Content Types
You can mix different content types in a single request:types.UserContent and consecutive function-call parts into types.ModelContent.
Use Cases
Q&A Systems
Generate answers to user questions with context
Content Creation
Generate blog posts, articles, or creative writing
Summarization
Summarize documents, articles, or conversations
Translation
Translate text between languages
Best Practices
- Use simple string inputs for basic prompts
- Use
types.Contentobjects when you need explicit role control - Combine text and other modalities (images, files) in the same request
- Access
response.textfor simple text responses - Iterate over
response.partsfor multimodal responses - Check
response.candidatesfor multiple response options