Skip to content

Commit 472a3e3

Browse files
authored
Add GenerateContentResponse.to_dict() (#337)
* GenerateContentResponse.to_dict() Change-Id: I2042d4387fe216f28b9c48b98eab8a71447fb98f * docstring Change-Id: Icec923e7d782ef5e6c2e36faefe790ea01ed93ad
1 parent 3193c3e commit 472a3e3

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

google/generativeai/types/generation_types.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,18 @@ def __init__(
356356
else:
357357
self._error = None
358358

359+
def to_dict(self):
360+
"""Returns the result as a JSON-compatible dict.
361+
362+
Note: This doesn't capture the iterator state when streaming, it only captures the accumulated
363+
`GenerateContentResponse` fields.
364+
365+
>>> import json
366+
>>> response = model.generate_content('Hello?')
367+
>>> json.dumps(response.to_dict())
368+
"""
369+
return type(self._result).to_dict(self._result)
370+
359371
@property
360372
def candidates(self):
361373
"""The list of candidate responses.
@@ -428,7 +440,7 @@ def __str__(self) -> str:
428440
else:
429441
_iterator = f"<{self._iterator.__class__.__name__}>"
430442

431-
as_dict = type(self._result).to_dict(self._result)
443+
as_dict = self.to_dict()
432444
json_str = json.dumps(as_dict, indent=2)
433445

434446
_result = f"glm.GenerateContentResponse({json_str})"

0 commit comments

Comments
 (0)