View source on GitHub |
Instances of this class manage the response of the generate_content_async
method.
google.generativeai.types.GenerateContentResponse(
done: bool,
iterator: (None | Iterable[glm.GenerateContentResponse] | AsyncIterable[glm.
GenerateContentResponse]),
result: glm.GenerateContentResponse,
chunks: Iterable[glm.GenerateContentResponse]
)
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.
Attributes | |
---|---|
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
@classmethod
from_iterator( iterator: Iterable[glm.GenerateContentResponse] )
from_response
@classmethod
from_response( response: glm.GenerateContentResponse )
resolve
resolve()
__iter__
__iter__()