Skip to content

Fix: List Index out out range with empty parts [] #138

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 3 commits into from
Dec 19, 2023
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
9 changes: 4 additions & 5 deletions google/generativeai/types/generation_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,13 @@ def text(self):
ValueError: If the candidate list or parts list does not contain exactly one entry.
"""
parts = self.parts
if len(parts) > 1 or "text" not in parts[0]:
if len(parts) != 1 or "text" not in parts[0]:
raise ValueError(
"The `response.text` quick accessor only works for "
"simple (single-`Part`) text responses. This response "
"contains multiple `Parts`. Use the `result.parts` "
"accessor or the full "
"simple (single-`Part`) text responses. This response is not simple text."
"Use the `result.parts` accessor or the full "
"`result.candidates[index].content.parts` lookup "
"instead"
"instead."
)
return parts[0].text

Expand Down