generative-ai-python icon indicating copy to clipboard operation
generative-ai-python copied to clipboard

`response.text` is no longer directly accessible - `TypeError: argument of type 'Part' is not iterable`

Open mitchspano opened this issue 1 year ago • 13 comments

Description of the bug:

Accessing the property response.text no longer works.

genai.configure(api_key = gcp_key)
model = genai.GenerativeModel("gemini-pro")
response = model.generate_content(prompt)

print(str(response.text)
# TypeError: argument of type 'Part' is not iterable
# google/generativeai/types/generation_types.py
# line 327, in text
#    if len(parts) != 1 or "text" not in parts[0]:

As a workaround, I can get the detail with response.candidates[0].content.parts[0].text, but this seems like it might be a breaking change?

Actual vs expected behavior:

I should be able to reference response.text directly.

Any other information you'd like to share?

NOTE: I am using the internal Google3 version of this SDK - see go/generative-ai-python-sdk-response-type-issue for the exact line internally. There seems to be a discrepancy between Google3 and GitHub - this is the line in the GitHub repo.

Feel free to reach out to me internally - mitchspano@

mitchspano avatar Apr 16 '24 01:04 mitchspano

Any update on this pls. Does this break all the python examples?

laxman-munigala avatar Apr 22 '24 19:04 laxman-munigala

The public python examples are okay, this is a google-internal problem.

@mitchspano I've added you to an email thread titled "Checking for oneof field presence in UPB proto messages", where we're discussing the cause of this.

MarkDaoust avatar Apr 22 '24 19:04 MarkDaoust

Thanks @MarkDaoust .. I am getting below error using langchain aswell as google-cloud-aiplatform. Am i missing something.

image image

laxman-munigala avatar Apr 22 '24 19:04 laxman-munigala

Interesting. Thanks for the update. Reopened.

Internally it has something to do with switching to upb protos, IDK if it's the same problem here.

https://github.com/protocolbuffers/upb?tab=readme-ov-file https://github.com/protocolbuffers/protobuf/tree/main/upb

proto-plus (used by these libraries)depends on an older version of protobuf (<5, when latest is 5.26.1), but upgrading to 5.26 doesn't cause this error.

Can ou post a "pip freeze | grep proto"?

MarkDaoust avatar Apr 22 '24 20:04 MarkDaoust

image

laxman-munigala avatar Apr 22 '24 21:04 laxman-munigala

Just with the python versions, using this SDK I can't reproduce that.

Can you reproduce the error in Colab, for example, so we can replicate it and share with the owners?

MarkDaoust avatar Apr 22 '24 22:04 MarkDaoust

Used below to create my environment where i see the issue.

name: python-test channels:

  • defaults dependencies:
  • python
  • pip=23.3
  • requests=2.31
  • pandas=2.2
  • plotly=5.19
  • sqlalchemy=2.0
  • streamlit=1.32
  • fastapi=0.103
  • uvicorn=0.20
  • ipykernel=6.28
  • pip:
    • duckdb == 0.10
    • google-cloud-aiplatform
    • langchain
    • langchain-google-vertexai

laxman-munigala avatar Apr 23 '24 16:04 laxman-munigala

The issue happens when parts is a protobuf object and not a dict - the check needs to be replaced with:

        if len(parts) != 1 or not hasattr(parts[0], "text"):

Instead of:

        if len(parts) != 1 or "text" not in parts[0]:

I am not sure if it will resolve all issues or if it needs to be a more comprehensive check, but it was causing issues for me and I ended up using parts[0].text directly to avoid similar issues.

https://github.com/google-gemini/generative-ai-python/blob/419a7cef81188a2d715b3853113a3213516d6a4a/google/generativeai/types/generation_types.py#L416

sakher avatar Jun 25 '24 18:06 sakher

For some reason - I was getting this error consistently yesterday, but can't reproduce today!!!

sakher avatar Jun 25 '24 18:06 sakher

@sakher, you do have a point, hasattr(parts[0], "text") would be more robust here. But the problem is that "text" in parts[0] is supposed to work for the proto-plus based proto classes. This library is only setup to work with those. Switching to hasattr will just mask this error and move the problem somewhere else. There are a lot of isinstance checks that will go the wrong way.

MarkDaoust avatar Jun 25 '24 23:06 MarkDaoust

You guys know better, I haven't dug deep into it - but when the error happens (sporadically) from some debugging, the type of parts was: google.protobuf.pyext._message.RepeatedCompositeContainer

However, in some cases, the error doesn't happen, and the type of parts seems to be different in such cases: proto.marshal.collections.repeated.RepeatedComposite

sakher avatar Jun 26 '24 06:06 sakher

(@MarkDaoust see me inside corp as parrt)

I'm also seeing this with anaconda and mac os (Sonoma 14.5) on apple silicon via jupyter lab.

TypeError: argument of type 'Part' is not iterable

Snippet:

import google.generativeai as genai
genai.configure(api_key='SECRET API KEY')

code = """
int i = 34;
"""
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
response = model.generate_content([
"""You are an expert Java programmer.
Here is some Java code that I would like you to analyze.""", code])
print(response.text)

Oddly however, the response object does appear to have the appropriate elements:

GenerateContentResponse(
    done=True,
    iterator=None,
    result=protos.GenerateContentResponse({
      "candidates": [
        {
          "content": {
            "parts": [
              {
                "text": "This code snippet is a simple declaration
...
)

For example the following evaluates fine: response.candidates[0].content.parts[0].text:

"This code snippet is a simple declaration ...

Info:

$ conda list python
# packages in environment at /Users/parrt/opt/anaconda3:
#
# Name                    Version                   Build  Channel
antlr4-python3-runtime    4.12.0                   pypi_0    pypi
brotli-python             1.0.9            py39hcec6c5f_8  
google-api-python-client  2.133.0                  pypi_0    pypi
ipython                   8.15.0           py39hecd8cb5_0  
ipython_genutils          0.2.0              pyhd3eb1b0_1  
msgpack-python            1.0.3            py39haf03e11_0  
python                    3.9.19               h5ee71fb_1  
python-dateutil           2.9.0post0       py39hecd8cb5_2  
python-fastjsonschema     2.16.2           py39hecd8cb5_0  
python-graphviz           0.20.1                   pypi_0    pypi
python-libarchive-c       2.9                pyhd3eb1b0_1  
python-lmdb               1.4.1            py39hcec6c5f_0  
python-lsp-black          2.0.0            py39hecd8cb5_0  
python-lsp-jsonrpc        1.1.2              pyhd3eb1b0_0  
python-lsp-server         1.10.0           py39hecd8cb5_0  
python-slugify            5.0.2              pyhd3eb1b0_0  
python-snappy             0.6.1            py39hcec6c5f_0  
python.app                3                py39hca72f7f_0  
python_abi                3.9                      2_cp39    conda-forge
$ conda list anaconda
# packages in environment at /Users/parrt/opt/anaconda3:
#
# Name                    Version                   Build  Channel
_anaconda_depends         2022.05                  py39_0  
anaconda                  custom                   py39_2  
anaconda-anon-usage       0.4.4            py39hfd8fc91_0  
anaconda-client           1.12.3           py39hecd8cb5_0  
anaconda-navigator        2.1.4            py39hecd8cb5_0  
anaconda-project          0.11.1           py39hecd8cb5_0  

My environment was old so I whacked anaconda and reinstalled everything. I get the same problem still.

$ conda list python
# packages in environment at /Users/parrt/anaconda3:
#
# Name                    Version                   Build  Channel
brotli-python             1.0.9           py312h313beb8_8  
gitpython                 3.1.37          py312hca03da5_0  
google-api-python-client  2.135.0                  pypi_0    pypi
ipython                   8.25.0          py312hca03da5_0  
ipython_genutils          0.2.0              pyhd3eb1b0_1  
msgpack-python            1.0.3           py312h48ca7d4_0  
python                    3.12.4               h99e199e_1  
python-dateutil           2.9.0post0      py312hca03da5_2  
python-dotenv             0.21.0          py312hca03da5_0  
python-fastjsonschema     2.16.2          py312hca03da5_0  
python-json-logger        2.0.7           py312hca03da5_0  
python-libarchive-c       2.9                pyhd3eb1b0_1  
python-lmdb               1.4.1           py312h313beb8_0  
python-lsp-black          2.0.0           py312hca03da5_0  
python-lsp-jsonrpc        1.1.2              pyhd3eb1b0_0  
python-lsp-server         1.10.0          py312hca03da5_0  
python-slugify            5.0.2              pyhd3eb1b0_0  
python-snappy             0.6.1           py312h313beb8_0  
python-tzdata             2023.3             pyhd3eb1b0_0  
python.app                3               py312h80987f9_0  
$ conda list anaconda
# packages in environment at /Users/parrt/anaconda3:
#
# Name                    Version                   Build  Channel
_anaconda_depends         2024.06         py312_openblas_2  
anaconda-anon-usage       0.4.4           py312hd6b623d_100  
anaconda-catalogs         0.2.0           py312hca03da5_1  
anaconda-client           1.12.3          py312hca03da5_0  
anaconda-cloud-auth       0.5.1           py312hca03da5_0  
anaconda-navigator        2.6.0           py312hca03da5_0  
anaconda-project          0.11.1          py312hca03da5_0  

parrt avatar Jun 29 '24 19:06 parrt

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

MarkDaoust avatar Jul 15 '24 03:07 MarkDaoust