Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gemini AI error - "AttributeError: module 'google.generativeai' has no attribute 'GenerativeModel'"

I am trying to create a script in Jupyter for testing Google Gemini AI model.

import google.generativeai as genai
import os

# genai.configure(api_key="API Key")

model = genai.GenerativeModel('gemini-pro')
response = model.generate_content('Please summarise this document: ...')

print(response.text)

I'm getting this error:

AttributeError
Traceback (most recent call last)
Input In [2], in <cell line: 6>()
      2 import os
      4 # genai.configure(api_key=os.environ['API_KEY'])
----> 6 model = genai.GenerativeModel('gemini-pro')
      7 response = model.generate_content('Please summarise this document: ...')
      9 print(response.text)

AttributeError: module 'google.generativeai' has no attribute 'GenerativeModel'

I tried:

  • Re-installing package - python3.11 -m pip install google-generativeai
  • Checked whether the class named GenerativeModel existed in my installed package source code but it didn't help.

Python and package versions:

  • python: 3.11.7
  • google-generativeai=0.5.1
like image 301
Learner_77 Avatar asked Aug 31 '25 16:08

Learner_77


2 Answers

The class should exist since v0.3.0 (https://github.com/google-gemini/generative-ai-python/commit/098854379496247617ef7fea882694022fca171d)

You must be using an older version of the SDK.

There are two likely causes:

  1. You've installed the package to a different environment than the one you're actually using. If you use Jupyter, please use the %pip install magic to install packages.
  2. You have updated the package, but have not restarted the Jupyter session.
like image 75
Ark-kun Avatar answered Sep 02 '25 06:09

Ark-kun


I'm having the same issue as you, a fix that you may or maynot be able to implement is downgrading your python version back to 3.10 in which gemini seems to be working without any issues.

like image 37
Guest Avatar answered Sep 02 '25 07:09

Guest