-
Notifications
You must be signed in to change notification settings - Fork 475
Description
Description of the bug:
Issue: Persistent ImportError for Content/Part from google.generativeai.types (Python 3.12.3, google-generativeai 0.8.5)
Summary:
Despite google-generativeai being reported as 0.8.5 by pip show, attempts to import Content or Part directly from google.generativeai.types consistently result in an ImportError. This occurs on a clean virtual environment using Python 3.12.3. Prior to this, a TypeError: Could not create Blob was encountered with standard dictionary-based message formats, also persisting through various troubleshooting steps. All standard debugging approaches, including aggressive environment cleanups and explicit API type usage, have failed to resolve either issue.
Environment Details:
- Operating System: Windows 11**
- Python Version: 3.12.3
google-generativeaiVersion: 0.8.5- Key Dependency Versions (from
pip showoutputs):google-ai-generativelanguage: 0.6.15protobuf: 4.25.8grpcio: 1.72.1Pillow: 11.2.1
Problem Description:
The core issue is a recurring ImportError when attempting to import Content or Part from google.generativeai.types. This is unexpected for google-generativeai version 0.8.5, where these types are documented and expected to be directly importable.
Prior to this ImportError, the primary blocking issue was a persistent TypeError: Could not create Blob, expected Blob, dictor anImage type... Got a: <class 'google.ai.generativelanguage_v1beta.types.content.Content'>. This TypeError consistently occurred when calling chat_session.send_message(), even when the messages list was correctly structured as [{"role": "user", "parts": [{"text": "Your prompt"}]}]. The TypeError indicated an internal type conversion error within the library, where an already-processed Content object was being passed to an internal function expecting raw data for conversion.
The ImportError then surfaced when attempting to explicitly use Content and Part types (from google.generativeai.types import Content, Part) to bypass the TypeError. This suggests a deeper problem with the actual installed library files or Python's ability to load them correctly, rather than just runtime behavior.
Steps to Reproduce:
- Create a clean virtual environment:
python -m venv flaky_detector_venv
- Activate the virtual environment:
.\flaky_detector_venv\Scripts\activate
- Create a
requirements.txtfile with the following content:google-generativeai~=0.8.0 pymysql~=1.1.0 pandas~=2.2.2 numpy~=1.26.4 pillow~=11.2.1 - Install dependencies from
requirements.txt:pip install -r requirements.txt
- Verify
google-generativeaiversion:(Expected output confirms Version 0.8.5)pip show google-generativeai
- Create a minimal Python script named
test_gemini.pywith the following content:import os import google.generativeai as genai import logging from google.generativeai.types import Content, Part # This line causes the ImportError logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') def run_test(): try: api_key = os.getenv("GEMINI_API_KEY") if not api_key: logging.error("GEMINI_API_KEY environment variable not set. Please set it.") return genai.configure(api_key=api_key) model = genai.GenerativeModel(model_name="gemini-1.5-flash-latest") chat_session = model.start_chat() prompt = "Hello, what is your purpose?" # Attempting with explicit types after prior TypeError issues: messages = [Content(role="user", parts=[Part(text=prompt)])] logging.info("Attempting to send message to Gemini...") response = chat_session.send_message(messages) logging.info(f"Received response from Gemini: {response.text}") except Exception as e: logging.error(f"An error occurred: {e}", exc_info=True) if __name__ == "__main__": run_test()
- Run the
test_gemini.pyscript:python .\test_gemini.py
Full Traceback:
Traceback (most recent call last):
File "D:\agent\v3\test_gemini.py", line 4, in <module>
from google.generativeai.types import Content, Part # Import Content and Part
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: cannot import name 'Content' from 'google.generativeai.types' (D:\agent\flaky_detector_venv\Lib\site-packages\google\generativeai\types_init_.py)
(Note: Prior to this ImportError, when attempting chat_session.send_message([{"role": "user", "parts": [{"text": prompt}]}]), the script consistently failed with a TypeError traceback similar to this example:)
TypeError: Could not create Blob, expected Blob, dict or an Image type(PIL.Image.Image or IPython.display.Image).
Got a: <class 'google.ai.generativelanguage_v1beta.types.content.Content'>
Value: parts {
text: "Hello, what is your purpose?"
}
role: "user"
Traceback (most recent call last):
File "D:\agent\v3\test_gemini.py", line 23, in run_test
response = chat_session.send_message(messages)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\agent\v3\flaky_detector_venv\Lib\site-packages\google\generativeai\generative_models.py", line 564, in send_message
content = content_types.to_content(content)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\agent\v3\flaky_detector_venv\Lib\site-packages\google\generativeai\types\content_types.py", line 296, in to_content
return protos.Content(parts=[to_part(part) for part in content])
^^^^^^^^^^^^^
File "D:\agent\v3\flaky_detector_venv\Lib\site-packages\google\generativeai\types\content_types.py", line 264, in to_part
return protos.Part(inline_data=to_blob(part))
^^^^^^^^^^^^^
File "D:\agent\v3\flaky_detector_venv\Lib\site-packages\google\generativeai\types\content_types.py", line 210, in to_blob
raise TypeError(
Actual vs expected behavior:
Actual Behavior:
The script consistently terminates with an ImportError on the line where Content and Part are imported.
Expected Behavior:
The test_gemini.py script should successfully import Content and Part, send a message to the Gemini API, and print the received response.
Any other information you'd like to share?
Troubleshooting Performed:
-
Variations in
send_messageinput format:- Attempted
chat_session.send_message([prompt]). - Attempted
chat_session.send_message([{"role": "user", "parts": [{"text": prompt}]}])(consistently failed withTypeError: Could not create Blob...). - Attempted
from google.generativeai.types import text_part(failed withImportErrorastext_partwas not found/available in earlier version). - Attempted
from google.generativeai.types import Content, Part(currently failing withImportErrorfor Python 3.12.3).
- Attempted
-
Aggressive Virtual Environment Management:
- Multiple instances of complete deletion of the
flaky_detector_venvdirectory. - Used
pip cache purgeto clear pip's package cache before reinstallations. - Recreated new virtual environments from scratch for each test.
- Manually checked for and deleted
__pycache__folders in relevant directories.
- Multiple instances of complete deletion of the
-
Dependency Installation and Verification:
- Confirmed
Pillowwas installed and updated (initially identified as missing). - Ensured all required packages (
pandas,numpy,pymysql) were installed viarequirements.txt. - Upgraded
google-generativeaithroughpip install --upgrade(from an initial0.5.xto0.8.5) and by updatingrequirements.txttogoogle-generativeai~=0.8.0. - Confirmed
pip show google-generativeaiconsistently reports0.8.5in the active environment after each installation. - Confirmed other core dependencies (
google-ai-generativelanguage,protobuf,grpcio) are at recent, compatible versions.
- Confirmed
-
Python Version Consideration:
- The issue is observed with Python 3.12.3. It has been suggested to try Python 3.11.x to rule out compatibility issues specific to Python 3.12.3, but this step has not been fully completed yet by the user.
Additional Notes/Hypothesis:
The core contradiction between pip show google-generativeai (showing 0.8.5) and Python's inabilitty to import expected components (Content, Part) from google.generativeai.types is highly unusual. This points towards a potential issue with the package's distribution for Python 3.12.3 on Windows, or a deeper, less common environmental problem affecting how Python loads installed modules.