TypeError when importing google.generativeai to use PaLM API
[EDIT – SOLVED. See comment below]I’m relatively new to Python (~4 months), so forgive any obvious oversights in my question.I’ve been using LangChain with OpenAI to make several simple chains, and everything has been working out. I got access to Google’s PaLM API and wanted to start converting some of the chain to that. Following the instructions from thequickstart guide, I installed the PaLM API library (pip install -q google-generativeai). When I try to import it, following the quickstart instructions (import google.generativeai as palm), I get the following error: “TypeError: ‘type’ object is not subscriptable”.It appears to be tied to this: “filters: Optional[list[safety_types.ContentFilterDict]]”. When I import something from the module at the same level as safety_types (including itself), I get no error (e.g., “from google.generativeai.types.discuss_types import ChatResponse”). Any other module I import works without issue (e.g. “from google import ai”)Things I have tried to fix the issue:1.) Confirmed the module is installed2.) Uninstall/reinstall google.generativeai3.) updated google.generativeai (pip install -U google-generativeai)4.) Test on different IDEs (JupyterLab desktop and VS Code)5.) Tested different parts of the moduleEDIT6.) Tried it on another computer. Everything worked perfectly fine on other computer. Not clear to me what differs between themThe full error code is here:TypeError                                 Traceback (most recent call last)Cell In[18], line 1----> 1 import google.generativeaiFile ~\AppData\Roaming\jupyterlab-desktop\jlab_server\lib\site-packages\google\generativeai_init_.py:591 # -- coding: utf-8 --2 # Copyright 2023 Google LLC3 #(…)13 # See the License for the specific language governing permissions and14 # limitations under the License.15 “”“A high level client library for generative AI.1617 ## Setup(…)5657 “””—> 59 from google.generativeai import types60 from google.generativeai import version62 from google.generativeai.discuss import chatFile ~\AppData\Roaming\jupyterlab-desktop\jlab_server\lib\site-packages\google\generativeai\types_init_.py:1917 from google.generativeai.types.discuss_types import *18 from google.generativeai.types.model_types import *—> 19 from google.generativeai.types.text_types import *20 from google.generativeai.types.citation_types import *21 from google.generativeai.types.safety_types import *File ~\AppData\Roaming\jupyterlab-desktop\jlab_server\lib\site-packages\google\generativeai\types\text_types.py:3429     safety_ratings: Optional[List[safety_types.SafetyRatingDict]]30     citation_metadata: Optional[citation_types.CitationMetadataDict]33@dataclasses.dataclass(init=False)—> 34 class Completion(abc.ABC):35     “”“The result returned bygenerativeai.generate_text.3637     UseGenerateTextResponse.candidatesto access all the completions generated by the model.(…)44         safety_feedback: Indicates which safety settings blocked content in this result.45     “””47     candidates: List[TextCompletion]File ~\AppData\Roaming\jupyterlab-desktop\jlab_server\lib\site-packages\google\generativeai\types\text_types.py:49, in Completion()47 candidates: List[TextCompletion]48 result: Optional[str]—> 49 filters: Optional[list[safety_types.ContentFilterDict]]50 safety_feedback: Optional[list[safety_types.SafetyFeedbackDict]]52 def to_dict(self) → Dict[str, Any]:TypeError: ‘type’ object is not subscriptableVersioning of various things is here:JupyterLab:Version 3.6.1Python 3.8.16 | packaged by conda-forge | (default, Feb  1 2023, 15:53:35) [MSC v.1929 64 bit (AMD64)]Type ‘copyright’, ‘credits’ or ‘license’ for more informationIPython 8.9.0 – An enhanced Interactive Python. Type ‘?’ for help.VS Code:Version: 1.79.0 (user setup)Commit: b380da4ef1ee00e224a15c1d4d9793e27c2b6302Date: 2023-06-07T14:26:35.552ZElectron: 22.5.5Chromium: 108.0.5359.215Node.js: 16.17.1V8: 10.8.168.25-electron.0OS: Windows_NT x64 10.0.22621Device:Device name__________Processor11th Gen Intel(R) Core™ i7-11390H @ 3.40GHz   3.42 GHzInstalled RAM16.0 GB (15.8 GB usable)Device ID90705941-A75B-47C0-BB50-9CF224D1094FProduct ID00342-20790-82443-AAOEMSystem type64-bit operating system, x64-based processorPen and touchPen and touch support with 10 touch pointsOS:EditionWindows 11 HomeVersion22H2Installed on‎2/‎18/‎2023OS build22621.1702ExperienceWindows Feature Experience Pack 1000.22641.1000.0
2023-06-10T23:16:03Z
Solved it by manually editing the module with code pulled fromGoogle’s Github. In the code installed through pip, the following block did not have OR None condition.candidates: List[TextCompletion]
result: str | None
filters: List[safety_types.ContentFilterDict | None]
safety_feedback: List[safety_types.SafetyFeedbackDict | None]