![]() |
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
.
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.
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.
Note: This doesn’t capture the iterator state when streaming, it only captures the accumulated
GenerateContentResponse
fields.
>>> import json
>>> response = model.generate_content('Hello?')
>>> json.dumps(response.to_dict())
__iter__
__iter__()