Method Signature
Description
Makes an API request to generate content using a Gemini model. Supports text-only and multimodal input/output, including images, audio, and video. The method includes built-in support for automatic function calling (AFC) when tools are provided. MCP (Model Context Protocol) support is available as an experimental feature.Parameters
str
required
The model to use for generation.Vertex AI formats:
- Model ID:
'gemini-2.0-flash' - Full resource name:
'projects/my-project/locations/us-central1/publishers/google/models/gemini-2.0-flash' - Partial resource name:
'publishers/google/models/gemini-2.0-flash' - Publisher/model:
'google/gemini-2.0-flash'
- Model ID:
'gemini-2.0-flash' - Model name:
'models/gemini-2.0-flash' - Tuned model:
'tunedModels/1234567890123456789'
ContentListUnionDict
required
The conversation history or input prompt to generate content from.Can be:
- A string:
'What is your name?' - A list of Content objects
- A list of Part objects
- Mixed content with text, images, video, and audio
GenerateContentConfig
Configuration for content generation.
Response
list[Candidate]
List of generated response candidates
UsageMetadata
Token usage information
PromptFeedback
Feedback about the prompt (e.g., safety blocks)
str
The model version used
str
Convenience property: text from the first candidate
Code Examples
Basic Text Generation
Multimodal Generation (Image Input)
Structured JSON Output
Function Calling (Automatic)
Async Usage
Notes
- The method automatically handles function calling when Python callables are provided in
tools - MCP (Model Context Protocol) sessions are only supported in async methods
- Use
generate_content_streamfor streaming responses - Multimodal input is supported for Gemini 2.0 and later models
- Some configuration options are only available on Vertex AI or Gemini API
Related Methods
- generate_content_stream - Streaming version
- count_tokens - Count tokens before generating
- embed_content - Generate embeddings