google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.64k stars 324 forks source link

fix: correct the Part argument is not iterable error #453

Closed DaHui-BT closed 4 months ago

DaHui-BT commented 4 months ago

Description of the change

I have changed the generation_types.py file. The changed line at [443, 446, 454]. Turn the part into dir(part)

Motivation

When using this library like:

response = genai.GenerativeModel('gemini-pro').generate_content("test",)
print(response.text)

the following error has occur:

File google\generativeai\types\generation_types.py:443, in BaseGenerateContentResponse.text(self)
    [441](google/generativeai/types/generation_types.py:441) texts = []
    [442](google/generativeai/types/generation_types.py:442) for part in parts:
--> [443](google/generativeai/types/generation_types.py:443)     if "text" in part:
    [444](google/generativeai/types/generation_types.py:444)         texts.append(part.text)
    [445](google/generativeai/types/generation_types.py:445)         continue

TypeError: argument of type 'Part' is not iterable

Type of change

Bug fix

Checklist

google-cla[bot] commented 4 months ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

MarkDaoust commented 4 months ago

https://github.com/google-gemini/generative-ai-python/issues/288

Thanks, but I don't think this is the right fix.

AFAIKT Something in your setup is breaking the proto-plus library, and is returning the proto instances without their proto-plus wrappers.

Your change fixes the first immediate symptom, but the code is littered with isinstance checks that won't see the expected proto-plus classes, and will do the wrong thing.

The last coment on https://github.com/google-gemini/generative-ai-python/issues/288 says they can reproduce this in anaconda.

Are you using anaconda?

DaHui-BT commented 4 months ago

Yes, I'm using miniconda. I make a new commit with changes for turning the dir(part, attr) into hasattr(part, attr) which can just focus on using attribute checks.

MarkDaoust commented 4 months ago

I think this is the fix we need: https://github.com/googleapis/proto-plus-python/pull/471