google.generativeai.types.GenerateContentResponse

Instances of this class manage the response of the generate_content method.

These are returned by GenerativeModel.generate_content and ChatSession.send_message. This object is based on the low level glm.GenerateContentResponse class which just has prompt_feedback and candidates attributes. This class adds several quick accessors for common use cases.

The same object type is returned for both stream=True/False.

Streaming

When you pass stream=True to GenerativeModel.generate_content or ChatSession.send_message, iterate over this object to receive chunks of the response:

response = model.generate_content(..., stream=True):
for chunk in response:
  print(chunk.text)

GenerateContentResponse.prompt_feedback is available immediately but GenerateContentResponse.candidates, and all the attributes derived from them (.text, .parts), are only available after the iteration is complete.

candidates The list of candidate responses.
parts A quick accessor equivalent to self.candidates[0].content.parts
prompt_feedback

text A quick accessor equivalent to self.candidates[0].content.parts[0].text
usage_metadata

Methods

from_iterator

View source

from_response

View source

resolve

View source

__iter__

View source