Skip to content

Commit 0a9596f

Browse files
Andy963MarkDaoust
andauthored
Fix: List Index out out range with empty parts [] (#138)
* Fix: Index out out range with empty parts [] while question was blocked by api, the parts is an empty list [], so the exp: if len(parts) > 1 or "text" not in parts[0]: will raise list index out of range, but not ValueError * simplify parts length check. * Update error message. --------- Co-authored-by: Mark Daoust <[email protected]>
1 parent 15bc434 commit 0a9596f

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

google/generativeai/types/generation_types.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,13 @@ def text(self):
324324
ValueError: If the candidate list or parts list does not contain exactly one entry.
325325
"""
326326
parts = self.parts
327-
if len(parts) > 1 or "text" not in parts[0]:
327+
if len(parts) != 1 or "text" not in parts[0]:
328328
raise ValueError(
329329
"The `response.text` quick accessor only works for "
330-
"simple (single-`Part`) text responses. This response "
331-
"contains multiple `Parts`. Use the `result.parts` "
332-
"accessor or the full "
330+
"simple (single-`Part`) text responses. This response is not simple text."
331+
"Use the `result.parts` accessor or the full "
333332
"`result.candidates[index].content.parts` lookup "
334-
"instead"
333+
"instead."
335334
)
336335
return parts[0].text
337336

0 commit comments

Comments
 (0)