View source on GitHub |
Instances of this class manage the response of the generate_content
method.
google.generativeai.types.GenerateContentResponse(
done: bool,
iterator: (None | Iterable[protos.GenerateContentResponse] | AsyncIterable[protos.
GenerateContentResponse]),
result: protos.GenerateContentResponse,
chunks: (Iterable[protos.GenerateContentResponse] | None) = None
)
These are returned by GenerativeModel.generate_content
and ChatSession.send_message
.
This object is based on the low level protos.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.
Attributes | |
---|---|
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
@classmethod
from_iterator( iterator: Iterable[protos.GenerateContentResponse] )
from_response
@classmethod
from_response( response: protos.GenerateContentResponse )
resolve
resolve()
to_dict
to_dict()
Returns the result as a JSON-compatible dict.
import json
response = model.generate_content('Hello?')
json.dumps(response.to_dict())
__iter__
__iter__()