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 the quickstart 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 installed
2.) Uninstall/reinstall google.generativeai
3.) updated google.generativeai ( pip install -U google-generativeai)
4.) Test on different IDEs (JupyterLab desktop and VS Code)
5.) Tested different parts of the module
EDIT
6.) Tried it on another computer. Everything worked perfectly fine on other computer. Not clear to me what differs between them

The full error code is here:


TypeError Traceback (most recent call last)
Cell In[18], line 1
----> 1 import google.generativeai

File ~\AppData\Roaming\jupyterlab-desktop\jlab_server\lib\site-packages\google\generativeai_init_.py:59
1 # -- coding: utf-8 --
2 # Copyright 2023 Google LLC
3 #
(…)
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 “”“A high level client library for generative AI.
16
17 ## Setup
(…)
56
57 “””
—> 59 from google.generativeai import types
60 from google.generativeai import version
62 from google.generativeai.discuss import chat

File ~\AppData\Roaming\jupyterlab-desktop\jlab_server\lib\site-packages\google\generativeai\types_init_.py:19
17 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:34
29 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 by generativeai.generate_text.
36
37 Use GenerateTextResponse.candidates to 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 subscriptable

Versioning of various things is here:
JupyterLab:
Version 3.6.1
Python 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 information
IPython 8.9.0 – An enhanced Interactive Python. Type ‘?’ for help.

VS Code:
Version: 1.79.0 (user setup)
Commit: b380da4ef1ee00e224a15c1d4d9793e27c2b6302
Date: 2023-06-07T14:26:35.552Z
Electron: 22.5.5
Chromium: 108.0.5359.215
Node.js: 16.17.1
V8: 10.8.168.25-electron.0
OS: Windows_NT x64 10.0.22621

Device:

Device name __________
Processor 11th Gen Intel(R) Core™ i7-11390H @ 3.40GHz 3.42 GHz
Installed RAM 16.0 GB (15.8 GB usable)
Device ID 90705941-A75B-47C0-BB50-9CF224D1094F
Product ID 00342-20790-82443-AAOEM
System type 64-bit operating system, x64-based processor
Pen and touch Pen and touch support with 10 touch points

OS:

Edition Windows 11 Home
Version 22H2
Installed on ‎2/‎18/‎2023
OS build 22621.1702
Experience Windows Feature Experience Pack 1000.22641.1000.0

Solved it by manually editing the module with code pulled from Google’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]