python-aiplatform: `TypeError: argument of type 'Part' is not iterable`
When we execute the code from the test playground for Gemini in Vertex AI Studio, there is a type error. Actually, _raw_part is not a dict. It is of type Part, as an extension of proto.Message. So, the text property of the class Part is not available as keyword…
I have found no way to extract text from a streaming response other than using repr function and trimming it.
Environment details
- OS type and version:
MacOS Sonoma 14.2 - Python version:
3.11.5 - pip version:
23.2.1 google-cloud-aiplatformversion:
Name: google-cloud-aiplatform
Version: 1.38.1
Summary: Vertex AI API client library
Home-page: https://github.com/googleapis/python-aiplatform
Author: Google LLC
Author-email: googleapis-packages@google.com
License: Apache 2.0
Location: /Users/xxxxxxx/anaconda3/lib/python3.11/site-packages
Requires: google-api-core, google-cloud-bigquery, google-cloud-resource-manager, google-cloud-storage, packaging, proto-plus, protobuf, shapely
Required-by:
Steps to reproduce
- Just execute the example code below, after logging with
gcloud
Code example
import vertexai
from vertexai.preview.generative_models import GenerativeModel # , Part
def generate():
"""Generates text using the Generative Model."""
# Initialize Vertex AI
vertexai.init(project="<project_id>", location="us-central1")
model = GenerativeModel("gemini-pro")
responses = model.generate_content(
"""hi. I would like to know the 10 first items of the fibonacci
sequence.""",
generation_config={
"max_output_tokens": 2048,
"temperature": 0.9,
"top_p": 1
},
stream=True,
)
for i, response in enumerate(responses):
print("Response:", i)
print(response.candidates[0].content.parts)
# print(repr(response.candidates[0].content.parts[0]).split("\"")[1])
print(response.candidates[0].content.parts[0].text)
generate()
Stack trace
Response: 0
[text: "1. 0\n2. 1\n3. 1\n4"
]
Traceback (most recent call last):
File "/Users/xxxxxxxxx/Documents/002-git/dolibarr-invoice/services.py", line 57, in <module>
generate()
File "/Users/xxxxxxxxx/Documents/002-git/dolibarr-invoice/services.py", line 54, in generate
print(response.candidates[0].content.parts[0].text)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxxxxxxx/anaconda3/lib/python3.11/site-packages/vertexai/generative_models/_generative_models.py", line 1508, in text
if "text" not in self._raw_part:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument of type 'Part' is not iterable
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 25 (14 by maintainers)
check the link https://github.com/google-gemini/generative-ai-python/issues/196#issuecomment-2081481592 , i was able to solve that issue. You cannot directly access the text from the response.
I had to do
response.candidates[0].content.parts[0]._raw_part.textto get the response.I get the same error, by just executing the python code from the playground in my google cloud shell.