Skip to main content
The 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 the contents parameter into list[types.Content]. You can structure your inputs in several ways:
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

The SDK converts non-function-call parts into types.UserContent:

List of Parts

The SDK groups them into a single types.UserContent:

Response Handling

The response object provides several ways to access the generated content:

Image Output Generation

Some models like gemini-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:
The SDK groups consecutive non-function-call parts into 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.Content objects when you need explicit role control
  • Combine text and other modalities (images, files) in the same request
  • Access response.text for simple text responses
  • Iterate over response.parts for multimodal responses
  • Check response.candidates for multiple response options