Skip to content

Add GenerateContentResponse.to_dict() #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion google/generativeai/types/generation_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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})"
Expand Down
Loading