google.generativeai.types.GenerateContentResponse

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

These are returned by GenerativeModel.generate_content_async and ChatSession.send_message_async. 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_async or ChatSession.send_message_async, iterate over this object to receive chunks of the response:

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

AsyncGenerateContentResponse.prompt_feedback is available immediately but AsyncGenerateContentResponse.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].parts
prompt_feedback

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

Methods

from_iterator

View source

from_response

View source

resolve

View source

__iter__

View source