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.5k stars 296 forks source link

encountered a small bug when I tried to use Gemini API #145

Closed zjk000 closed 8 months ago

zjk000 commented 10 months ago

Description of the bug:

I ran the example code on https://ai.google.dev/tutorials/python_quickstart:

import google.generativeai as genai
import os
os.environ["GOOGLE_API_KEY"] = "My API key"

genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

for m in genai.list_models():
  if 'generateContent' in m.supported_generation_methods:
    print(m)

and I got:

Traceback (most recent call last):
  File "C:\Users\zjk\Desktop\Gemini.py", line 10, in <module>
    for m in genai.list_models():
  File "D:\anaconda3\envs\python3.9\lib\site-packages\google\generativeai\models.py", line 165, in list_models
    model = type(model).to_dict(model)
AttributeError: to_dict

Actual vs expected behavior:

I didn't successfully run the code, but I guess I was supposed to see

gemini-pro gemini-pro-vision

as the documentation shows image

Any other information you'd like to share?

google-generativeai version: 0.3.2 python version: 3.9.15

MarkDaoust commented 9 months ago

I'm not able to reproduce this.

Could you paste the output of a pip freeze from that environment?

zjk000 commented 9 months ago

I'm not able to reproduce this.

Could you paste the output of a pip freeze from that environment?

absl-py @ file:///home/conda/feedstock_root/build_artifacts/absl-py_1695154477682/work
accelerate @ file:///home/conda/feedstock_root/build_artifacts/accelerate_1689437594583/work
addict==2.4.0
aiodns==3.0.0
aiohttp @ file:///D:/bld/aiohttp_1666878661708/work
aiohttp-socks==0.8.0
aiosignal @ file:///home/conda/feedstock_root/build_artifacts/aiosignal_1667935791922/work
aliyun-python-sdk-core==2.13.36
aliyun-python-sdk-kms==2.16.1
annotated-types @ file:///home/conda/feedstock_root/build_artifacts/annotated-types_1696634205638/work
anyio @ file:///home/conda/feedstock_root/build_artifacts/anyio_1685486400275/work/dist
archspec @ file:///home/conda/feedstock_root/build_artifacts/archspec_1699370045702/work
async-timeout @ file:///home/conda/feedstock_root/build_artifacts/async-timeout_1640026696943/work
attrs @ file:///home/conda/feedstock_root/build_artifacts/attrs_1659291887007/work
blinker @ file:///home/conda/feedstock_root/build_artifacts/blinker_1681349778161/work
boltons @ file:///home/conda/feedstock_root/build_artifacts/boltons_1677499911949/work
boto3==1.20.16
botocore==1.23.16
brotlipy @ file:///D:/bld/brotlipy_1666764815687/work
cachetools @ file:///home/conda/feedstock_root/build_artifacts/cachetools_1698197297464/work
cchardet==2.1.7
certifi @ file:///home/conda/feedstock_root/build_artifacts/certifi_1700303426725/work/certifi
cffi @ file:///D:/bld/cffi_1666754845955/work
charset-normalizer @ file:///home/conda/feedstock_root/build_artifacts/charset-normalizer_1661170624537/work
click @ file:///D:/bld/click_1666798499870/work
click-config-file @ file:///home/conda/feedstock_root/build_artifacts/click-config-file_1619600267591/work
click-plugins==1.1.1
colorama==0.4.4
conda @ file:///D:/bld/conda_1689351947684/work
conda-libmamba-solver @ file:///home/conda/feedstock_root/build_artifacts/conda-libmamba-solver_1690880668143/work/src
conda-package-handling @ file:///home/conda/feedstock_root/build_artifacts/conda-package-handling_1691048088238/work
conda_package_streaming @ file:///home/conda/feedstock_root/build_artifacts/conda-package-streaming_1691009212940/work
configobj @ file:///home/conda/feedstock_root/build_artifacts/configobj_1674244895923/work
configparser @ file:///home/conda/feedstock_root/build_artifacts/configparser_1660952164778/work
contourpy @ file:///D:/bld/contourpy_1667248269651/work
crcmod==1.7
cryptography @ file:///D:/bld/cryptography_1669593791496/work
cycler @ file:///home/conda/feedstock_root/build_artifacts/cycler_1635519461629/work
dashscope==1.13.3
dataclasses @ file:///home/conda/feedstock_root/build_artifacts/dataclasses_1628958434797/work
datasets @ file:///home/conda/feedstock_root/build_artifacts/datasets_1669157712528/work
dill @ file:///home/conda/feedstock_root/build_artifacts/dill_1666603105584/work
distro @ file:///home/conda/feedstock_root/build_artifacts/distro_1675116244235/work
einops==0.6.1
elastic-transport==8.4.0
elasticsearch==8.8.0
exceptiongroup @ file:///home/conda/feedstock_root/build_artifacts/exceptiongroup_1678703645500/work
fake-useragent==1.1.3
filelock @ file:///home/conda/feedstock_root/build_artifacts/filelock_1660129891014/work
Flask @ file:///home/conda/feedstock_root/build_artifacts/flask_1676592993069/work
Flask-Cors @ file:///home/conda/feedstock_root/build_artifacts/flask-cors_1622383494577/work
fonttools @ file:///D:/bld/fonttools_1666827219016/work
frozenlist @ file:///D:/bld/frozenlist_1667935593267/work
fsspec @ file:///home/conda/feedstock_root/build_artifacts/fsspec_1702335961905/work
gast==0.5.4
geographiclib==2.0
geopy==2.3.0
google-ai-generativelanguage==0.4.0
google-api-core==2.14.0
google-auth @ file:///home/conda/feedstock_root/build_artifacts/google-auth_1702091584611/work
google-auth-oauthlib @ file:///home/conda/feedstock_root/build_artifacts/google-auth-oauthlib_1702414855226/work
google-generativeai==0.3.2
googleapis-common-protos==1.61.0
googletransx==2.4.2
grpcio==1.59.3
grpcio-status==1.59.3
h11 @ file:///home/conda/feedstock_root/build_artifacts/h11_1664132893548/work
h2 @ file:///home/conda/feedstock_root/build_artifacts/h2_1634280454336/work
hpack==4.0.0
httpcore @ file:///home/conda/feedstock_root/build_artifacts/httpcore_1684845985138/work
httpx @ file:///home/conda/feedstock_root/build_artifacts/httpx_1684534812525/work
huggingface-hub @ file:///home/conda/feedstock_root/build_artifacts/huggingface_hub_1700515386419/work
humanize @ file:///home/conda/feedstock_root/build_artifacts/humanize_1675692848261/work
hyperframe @ file:///home/conda/feedstock_root/build_artifacts/hyperframe_1619110129307/work
idna @ file:///home/conda/feedstock_root/build_artifacts/idna_1663625384323/work
importlib-metadata @ file:///home/conda/feedstock_root/build_artifacts/importlib-metadata_1701632192416/work
importlib-resources==5.12.0
itsdangerous @ file:///home/conda/feedstock_root/build_artifacts/itsdangerous_1648147185463/work
jieba @ file:///home/conda/feedstock_root/build_artifacts/jieba_1622403007736/work
Jinja2 @ file:///home/conda/feedstock_root/build_artifacts/jinja2_1654302431367/work
jmespath==0.10.0
joblib==1.1.0
jsonpatch @ file:///home/conda/feedstock_root/build_artifacts/jsonpatch_1632759296524/work
jsonpointer==2.0
kiwisolver @ file:///D:/bld/kiwisolver_1666805897768/work
libmambapy @ file:///D:/bld/mamba-split_1697043028982/work/libmambapy
lxml==4.9.2
mamba @ file:///D:/bld/mamba-split_1697043028982/work/mamba
Markdown @ file:///home/conda/feedstock_root/build_artifacts/markdown_1698797478597/work
MarkupSafe @ file:///D:/bld/markupsafe_1674136063379/work
matplotlib @ file:///D:/bld/matplotlib-suite_1667505027923/work
menuinst @ file:///D:/bld/menuinst_1666840019111/work
modelscope==1.7.1
multidict @ file:///D:/bld/multidict_1666830237137/work
multiprocess @ file:///D:/bld/multiprocess_1666933024820/work
munkres==1.1.4
nltk @ file:///home/conda/feedstock_root/build_artifacts/nltk_1672696305909/work
numpy @ file:///D:/bld/numpy_1688887186903/work
oauthlib @ file:///home/conda/feedstock_root/build_artifacts/oauthlib_1666056362788/work
openai @ file:///home/conda/feedstock_root/build_artifacts/openai_1701459275756/work
openprompt==1.0.1
oss2==2.18.0
packaging @ file:///home/conda/feedstock_root/build_artifacts/packaging_1681337016113/work
pandas @ file:///D:/bld/pandas_1702057215848/work
patsy @ file:///home/conda/feedstock_root/build_artifacts/patsy_1665356157073/work
Pillow @ file:///D:/bld/pillow_1666920708409/work
platformdirs @ file:///home/conda/feedstock_root/build_artifacts/platformdirs_1701708255999/work
pluggy @ file:///home/conda/feedstock_root/build_artifacts/pluggy_1667232663820/work
ply==3.11
pooch @ file:///home/conda/feedstock_root/build_artifacts/pooch_1679580333621/work
proto-plus==1.22.3
protobuf==4.25.1
psutil @ file:///D:/bld/psutil_1681775298734/work
pyarrow==10.0.1
pyasn1 @ file:///home/conda/feedstock_root/build_artifacts/pyasn1_1701287008248/work
pyasn1-modules @ file:///home/conda/feedstock_root/build_artifacts/pyasn1-modules_1695107857548/work
pycares==4.3.0
pycosat @ file:///D:/bld/pycosat_1666836850580/work
pycparser @ file:///home/conda/feedstock_root/build_artifacts/pycparser_1636257122734/work
pycryptodome==3.18.0
pydantic @ file:///home/conda/feedstock_root/build_artifacts/pydantic_1700669197061/work
pydantic-settings==2.1.0
pydantic_core @ file:///D:/bld/pydantic-core_1700664143617/work
PyJWT @ file:///home/conda/feedstock_root/build_artifacts/pyjwt_1683676063469/work
pyOpenSSL @ file:///home/conda/feedstock_root/build_artifacts/pyopenssl_1665350324128/work
pyparsing @ file:///home/conda/feedstock_root/build_artifacts/pyparsing_1652235407899/work
PyQt5==5.15.7
PyQt5-sip @ file:///D:/bld/pyqt-split_1666830111760/work/pyqt_sip
PySocks @ file:///D:/bld/pysocks_1661604991356/work
python-dateutil==2.8.2
python-dotenv==1.0.0
python-socks==2.3.0
pytz @ file:///home/conda/feedstock_root/build_artifacts/pytz_1667391478166/work
pyu2f @ file:///home/conda/feedstock_root/build_artifacts/pyu2f_1604248910016/work
PyYAML @ file:///D:/bld/pyyaml_1666772623553/work
regex==2021.11.10
requests @ file:///home/conda/feedstock_root/build_artifacts/requests_1661872987712/work
requests-oauthlib @ file:///home/conda/feedstock_root/build_artifacts/requests-oauthlib_1643557462909/work
responses @ file:///home/conda/feedstock_root/build_artifacts/responses_1643839609465/work
rjieba==0.1.11
rouge==1.0.0
rsa @ file:///home/conda/feedstock_root/build_artifacts/rsa_1658328885051/work
ruamel-yaml-conda @ file:///D:/bld/ruamel_yaml_1666819853770/work
ruamel.yaml @ file:///D:/bld/ruamel.yaml_1678273159970/work
ruamel.yaml.clib @ file:///D:/bld/ruamel.yaml.clib_1670412967785/work
s3transfer==0.5.0
sacremoses @ file:///home/conda/feedstock_root/build_artifacts/sacremoses_1651557636210/work
safetensors @ file:///D:/bld/safetensors_1682431613026/work
schedule==1.2.0
scikit-learn==1.0.1
scipy==1.10.1
seaborn @ file:///home/conda/feedstock_root/build_artifacts/seaborn-split_1672497695270/work
sentence-transformers @ file:///home/conda/feedstock_root/build_artifacts/sentence-transformers_1656297135090/work
sentencepiece @ file:///D:/bld/sentencepiece-split_1674349076575/work/python
simplejson==3.19.1
sip @ file:///D:/bld/sip_1669040051908/work
six @ file:///home/conda/feedstock_root/build_artifacts/six_1620240208055/work
sklearn==0.0
sniffio @ file:///home/conda/feedstock_root/build_artifacts/sniffio_1662051266223/work
snscrape @ git+https://github.com/JustAnotherArchivist/snscrape.git@b76f485208016f337ce36a3c6c0c6573502693a7
sortedcontainers==2.4.0
soupsieve==2.4.1
sparkapi-python==1.0.3
statsmodels @ file:///D:/bld/statsmodels_1667586210685/work
tensorboard @ file:///home/conda/feedstock_root/build_artifacts/tensorboard_1699025239851/work/tensorboard-2.15.1-py3-none-any.whl#sha256=c46c1d1cf13a458c429868a78b2531d8ff5f682058d69ec0840b0bc7a38f1c0f
tensorboard-data-server @ file:///D:/bld/tensorboard-data-server_1695425544157/work/tensorboard_data_server-0.7.0-py3-none-any.whl#sha256=16250db016726523c76d2af77dbfd82881a4c011f2b62e2fe7b508e3e0722450
tensorboardX==2.6
threadpoolctl==3.0.0
tokenizers @ file:///D:/bld/tokenizers_1702395354819/work/bindings/python
toml @ file:///home/conda/feedstock_root/build_artifacts/toml_1604308577558/work
toolz @ file:///home/conda/feedstock_root/build_artifacts/toolz_1657485559105/work
torch==1.10.2
torchvision==0.11.3
tornado @ file:///D:/bld/tornado_1666788767305/work
tqdm @ file:///home/conda/feedstock_root/build_artifacts/tqdm_1662214488106/work
transformers @ file:///home/conda/feedstock_root/build_artifacts/transformers_1702595340577/work
twarc @ file:///home/conda/feedstock_root/build_artifacts/twarc_1680726614347/work
tweepy @ file:///home/conda/feedstock_root/build_artifacts/tweepy_1682424282752/work
twint==2.1.20
typing_extensions @ file:///home/conda/feedstock_root/build_artifacts/typing_extensions_1695040754690/work
tzdata @ file:///home/conda/feedstock_root/build_artifacts/python-tzdata_1680081134351/work
unicodedata2 @ file:///D:/bld/unicodedata2_1667240049903/work
urllib3 @ file:///home/conda/feedstock_root/build_artifacts/urllib3_1669259737463/work
websockets==12.0
Werkzeug @ file:///home/conda/feedstock_root/build_artifacts/werkzeug_1676411946679/work
win-inet-pton @ file:///D:/bld/win_inet_pton_1667051142467/work
wincertstore==0.2
wobert @ git+https://github.com/JunnYu/WoBERT_pytorch.git@f19c7e0301b941140dd55fd6dd46e5e8c157e73f
xxhash @ file:///D:/bld/python-xxhash_1666826613636/work
yacs==0.1.8
yapf==0.40.1
yarl @ file:///D:/bld/yarl_1666861847371/work
zipp @ file:///home/conda/feedstock_root/build_artifacts/zipp_1669453021653/work
zstandard==0.22.0
Ellen-Gu commented 9 months ago

The code examples from Google generally work well when run directly in google Colab environment. However, I've encountered some minor issues when trying to execute the same code on my local laptop. I encounter similar errors as described in this post:

for m in genai.list_models():
    print(m)

AttributeError Traceback (most recent call last) Cell In[176], line 1 ----> 1 for m in genai.list_models(): 2 print(m)

File [~/anaconda3/lib/python3.10/site-packages/google/generativeai/models.py:165](https://vscode-remote+attached- . . . . . .

      162     client = get_default_model_client()
      164 for model in client.list_models(page_size=page_size):
--> 165     model = type(model).to_dict(model)
      166     yield model_types.Model(**model)

AttributeError: to_dict

anuj0405 commented 9 months ago

The code examples from Google generally work well when run directly in google Colab environment. However, I've encountered some minor issues when trying to execute the same code on my local laptop. I encounter similar errors as described in this post:

for m in genai.list_models():
    print(m)

AttributeError Traceback (most recent call last) Cell In[176], line 1 ----> 1 for m in genai.list_models(): 2 print(m)

File [~/anaconda3/lib/python3.10/site-packages/google/generativeai/models.py:165](https://vscode-remote+attached- . . . . . .

      162     client = get_default_model_client()
      164 for model in client.list_models(page_size=page_size):
--> 165     model = type(model).to_dict(model)
      166     yield model_types.Model(**model)

AttributeError: to_dict

The problem was solved by using python==3.11 and google-generativeai==0.3.2

github-actions[bot] commented 9 months ago

Marking this issue as stale since it has been open for 14 days with no activity. This issue will be closed if no further activity occurs.

github-actions[bot] commented 8 months ago

This issue was closed because it has been inactive for 28 days. Please post a new issue if you need further assistance. Thanks!