google.generativeai.types.ChatResponse

A chat response from the model.

  • Use response.last (settable) for easy access to the text of the last response. (messages[-1]['content'])
  • Use response.messages to access the message history (including .last).
  • Use response.candidates to access all the responses generated by the model.

Other attributes are just saved from the arguments to genai.chat, so you can easily continue a conversation:

import google.generativeai as genai

genai.configure(api_key=os.environ['GOOGLE_API_KEY'])

response = genai.chat(messages=["Hello."])
print(response.last) #  'Hello! What can I help you with?'
response.reply("Can you tell me a joke?")

See genai.chat for more details.

candidates A list of candidate responses from the model.

The top candidate is appended to the messages field.

This list will contain a maximum of candidate_count candidates. It may contain fewer (duplicates are dropped), it will contain at least one.

filters This indicates which types.SafetyCategory(s) blocked a candidate from this response, the lowest types.HarmProbability that triggered a block, and the types.HarmThreshold setting for that category. This indicates the smallest change to the types.SafetySettings that would be necessary to unblock at least 1 response.

The blocking is configured by the types.SafetySettings in the request (or the default types.SafetySettings of the API).

messages A snapshot of the conversation history sorted chronologically.
model The model name.
context Text that should be provided to the model first, to ground the response.
examples Examples of what the model should generate.
temperature Controls the randomness of the output. Must be positive.
candidate_count The maximum number of generated response messages to return.
top_k The maximum number of tokens to consider when sampling.
top_p The maximum cumulative probability of tokens to consider when sampling.
last A settable property that provides simple access to the last response string

A shortcut for response.messages[0]['content'].

Methods

reply

View source

Add a message to the conversation, and get the model's response.

to_dict

View source

__eq__

top_k None
top_p None