If you've tried installing the Gemini API SDK only to run into ModuleNotFoundError, ImportError, or Cannot find module errors, you're not alone. These issues are among the most common friction points for developers getting started with the Gemini API — and they almost always come down to package naming changes or environment mismatches.
Common Symptoms
Before diving into solutions, here's a quick reference for recognizing these errors:
Python SDK (google-generativeai / google-genai)
ModuleNotFoundError: No module named 'google.generativeai'
ImportError: cannot import name 'GenerativeModel' from 'google.generativeai'
AttributeError: module 'google.generativeai' has no attribute 'GenerativeModel'
TypeError: configure() got an unexpected keyword argument 'api_key'
Node.js SDK (@google/generative-ai / @google/genai)
Error: Cannot find module '@google/generative-ai'
TypeError: GoogleGenerativeAI is not a constructor
SyntaxError: The requested module '@google/generative-ai' does not provide an export named 'GoogleGenerativeAI'
If any of these look familiar, read on.
Root Cause Analysis
Pattern 1: Using the old package name
In late 2024, Google renamed the SDK packages:
- Python:
google-generativeai→google-genai - Node.js:
@google/generative-ai→@google/genai
Many tutorials and blog posts still reference the old names. Since the old and new SDKs have different class names and APIs, mixing them in the same project guarantees import errors.
Pattern 2: Multiple Python environments in conflict
When pip install succeeds but import still fails, the most likely cause is that the Python interpreter running your code is different from the one where you installed the package. This is especially common when python and python3 point to different binaries, or when multiple virtual environments are active at the same time.
Pattern 3: Stale cache serving an outdated version
Both pip and npm maintain local caches. If an old, broken version is cached, re-running install may silently skip updating. Clearing the cache forces a fresh download.
Pattern 4: Runtime version too old
The latest Gemini API SDKs require:
- Python: 3.9 or above (3.10+ recommended)
- Node.js: 18 or above (20 LTS recommended)
Older runtimes may either fail during install or raise cryptic errors at runtime.
Pattern 5: Dependency conflict with binary packages
Packages like protobuf and grpcio ship as compiled binaries and are version-sensitive. If another library pins an incompatible version of either, pip may refuse to install or silently downgrade a dependency, causing runtime failures.
Fix: Python SDK
Step 1: Audit your currently installed packages
pip list | grep -i googleSample output:
google-generativeai 0.8.3
google-genai 1.3.0
If both appear, you must remove one. The old package (google-generativeai) should go.
Step 2: Remove the old package and do a clean install
# Remove the legacy package
pip uninstall google-generativeai -y
# Install the current package, bypassing cache
pip install --no-cache-dir google-genai
# Verify
pip show google-genaiExpected output:
Name: google-genai
Version: 1.10.0
Step 3: Update your import statements
The new google-genai package uses a different import style:
# ✅ Correct import for the new SDK
from google import genai
client = genai.Client(api_key="YOUR_GEMINI_API_KEY")
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Hello, Gemini!"
)
print(response.text)
# → Hello! How can I help you today?Make sure every file in your project uses this new import style — do not mix old and new patterns.
Step 4: Isolate your environment with a virtual env (recommended)
# Create a fresh virtual environment
python3 -m venv gemini_env
# Activate it (Mac/Linux)
source gemini_env/bin/activate
# Activate it (Windows)
gemini_env\Scripts\activate
# Install inside the venv
pip install google-genai
# Confirm the install
pip list | grep googleVirtual environments eliminate cross-project dependency conflicts entirely.
Step 5: Confirm your Python version
python3 --version
# Python 3.12.3If you're on 3.8 or below, consider upgrading before proceeding.
Fix: Node.js SDK
Step 1: Inspect your installed packages
# Check package.json
cat package.json | grep google
# Check installed versions
npm list @google/genai 2>/dev/null
npm list @google/generative-ai 2>/dev/nullStep 2: Remove the old package and install the new one
# Uninstall the legacy package
npm uninstall @google/generative-ai
# Clear npm cache and install fresh
npm cache clean --force
npm install @google/genai
# Confirm
npm list @google/genaiStep 3: Use the correct import syntax
// ✅ Correct import for @google/genai (ES Modules)
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Hello, Gemini!",
});
console.log(response.text);
// → Hello! How can I help you today?
}
main();Step 4: Check your Node.js version
node --version
# v20.11.0If you're below v18, upgrade via nvm:
nvm install 20
nvm use 20How to Verify the Fix
After making changes, run this minimal test to confirm the SDK is working:
Python:
from google import genai
client = genai.Client(api_key="YOUR_GEMINI_API_KEY")
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Say 'SDK is working' in one sentence."
)
print(response.text)
# → SDK is working correctly!Node.js:
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const res = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Say 'SDK is working' in one sentence.",
});
console.log(res.text);
// → SDK is working correctly!If a response comes back without errors, you're good to go.
Preventive Best Practices
Pin your dependency versions
Specify exact SDK versions in requirements.txt (Python) or package.json (Node.js):
# requirements.txt
google-genai==1.10.0
Check for updates regularly
# Python
pip index versions google-genai
# Node.js
npm view @google/genai versionRun pip check in CI
pip check
# No broken requirements found.Consult the official migration guide
For major version jumps, review the official migration guide to understand breaking changes before upgrading.
Looking back
SDK installation and version mismatch issues are frustrating, but entirely solvable once you know where to look:
- Remove the old package (
google-generativeai/@google/generative-ai) and switch to the current one (google-genai/@google/genai) - Use virtual environments (Python) or clean
node_modulesinstalls (Node.js) to prevent dependency conflicts - Verify your runtime versions — Python 3.9+ and Node.js 18+ are minimum requirements
- Run
pip checkornpm lsregularly to catch dependency issues early
Once your environment is set up correctly, check out the Gemini API quickstart guide to start building your first AI-powered app.