diff --git a/google/generativeai/types/generation_types.py b/google/generativeai/types/generation_types.py index b7a342b37..f0c9de4c7 100644 --- a/google/generativeai/types/generation_types.py +++ b/google/generativeai/types/generation_types.py @@ -356,6 +356,18 @@ def __init__( else: self._error = None + def to_dict(self): + """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()) + """ + return type(self._result).to_dict(self._result) + @property def candidates(self): """The list of candidate responses. @@ -428,7 +440,7 @@ def __str__(self) -> str: else: _iterator = f"<{self._iterator.__class__.__name__}>" - as_dict = type(self._result).to_dict(self._result) + as_dict = self.to_dict() json_str = json.dumps(as_dict, indent=2) _result = f"glm.GenerateContentResponse({json_str})"