Skip to content
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

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

Open
mitchspano opened this issue Apr 16, 2024 · 13 comments
Assignees
Labels
component:python sdk Issue/PR related to Python SDK type:bug Something isn't working

Comments

@mitchspano
Copy link

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 mitchspano added component:python sdk Issue/PR related to Python SDK type:bug Something isn't working labels Apr 16, 2024
@mitchspano mitchspano changed the title TypeError: argument of type 'Part' is not iterable - response.text is no longer directly accessible response.text is no longer directly accessible - TypeError: argument of type 'Part' is not iterable Apr 16, 2024
@singhniraj08 singhniraj08 added the status:triaged Issue/PR triaged to the corresponding sub-team label Apr 18, 2024
@laxman-munigala
Copy link

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

@MarkDaoust
Copy link
Collaborator

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.

@github-actions github-actions bot removed the status:triaged Issue/PR triaged to the corresponding sub-team label Apr 22, 2024
@laxman-munigala
Copy link

laxman-munigala commented Apr 22, 2024

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

image image

@MarkDaoust MarkDaoust reopened this Apr 22, 2024
@MarkDaoust
Copy link
Collaborator

MarkDaoust commented Apr 22, 2024

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"?

@laxman-munigala
Copy link

image

@MarkDaoust
Copy link
Collaborator

MarkDaoust commented Apr 22, 2024

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?

@laxman-munigala
Copy link

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

@sakher
Copy link

sakher commented Jun 25, 2024

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.

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

@sakher
Copy link

sakher commented Jun 25, 2024

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

@MarkDaoust
Copy link
Collaborator

@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.

@sakher
Copy link

sakher commented Jun 26, 2024

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

@parrt
Copy link

parrt commented Jun 29, 2024

(@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  

@MarkDaoust
Copy link
Collaborator

I think this is the fix we need:
googleapis/proto-plus-python#471

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:python sdk Issue/PR related to Python SDK type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants