The genai.GenerativeModel class wraps default parameters for calls to GenerativeModel.generate_content, GenerativeModel.count_tokens, and GenerativeModel.start_chat.

This family of functionality is designed to support multi-turn conversations, and multimodal requests. What media-types are supported for input and output is model-dependant.

import google.generativeai as genai
import PIL.Image
model = genai.GenerativeModel('models/gemini-pro')
result = model.generate_content('Tell me a story about a magic backpack')
"In the quaint little town of Lakeside, there lived a young girl named Lily..."

Multimodal input:

model = genai.GenerativeModel('models/gemini-pro')
result = model.generate_content([
    "Give me a recipe for these:",'scones.jpeg')])
"**Blueberry Scones** ..."

Multi-turn conversation:

chat = model.start_chat()
response = chat.send_message("Hi, I have some questions for you.")
"Sure, I'll do my best to answer your questions..."

To list the compatible model names use:

for m in genai.list_models():
    if 'generateContent' in m.supported_generation_methods:

model_name The name of the model to query. To list compatible models use
safety_settings Sets the default safety filters. This controls which content is blocked by the api before being returned.
generation_config A genai.GenerationConfig setting the default generation parameters to use.





View source


View source


View source

Creates a model with cached_content as model's context.

cached_content context for the model.
generation_config Overrides for the model's generation config.
safety_settings Overrides for the model's safety settings.

GenerativeModel object with cached_content as its context.


View source

A multipurpose function to generate responses from the model.

This GenerativeModel.generate_content method can handle multimodal input, and multi-turn conversations.

model = genai.GenerativeModel('models/gemini-pro')
response = model.generate_content('Tell me a story about a magic backpack')


This method supports streaming with the stream=True. The result has the same type as the non streaming case, but you can iterate over the response chunks as they become available:

response = model.generate_content('Tell me a story about a magic backpack', stream=True)
for chunk in response:


This method supports multi-turn chats but is stateless: the entire conversation history needs to be sent with each request. This takes some manual management but gives you complete control:

messages = [{'role':'user', 'parts': ['hello']}]
response = model.generate_content(messages) # "Hello, how can I help"
messages.append({'role':'user', 'parts': ['How does quantum physics work?']})
response = model.generate_content(messages)

For a simpler multi-turn interface see GenerativeModel.start_chat.

Input type flexibility

While the underlying API strictly expects a list[protos.Content] objects, this method will convert the user input into the correct type. The hierarchy of types that can be converted is below. Any of these objects can be passed as an equivalent dict.

In an Iterable[protos.Content] each content is a separate message. But note that an Iterable[protos.Part] is taken as the parts of a single message.

contents The contents serving as the model's prompt.
generation_config Overrides for the model's generation config.
safety_settings Overrides for the model's safety settings.
stream If True, yield response chunks as they are generated.
tools protos.Tools more info coming soon.
request_options Options for the request.


View source

The async version of GenerativeModel.generate_content.


View source

Returns a genai.ChatSession attached to this model.

model = genai.GenerativeModel()
chat = model.start_chat(history=[...])
response = chat.send_message("Hello?")

history An iterable of protos.Content objects, or equivalents to initialize the session.