2025, Nov 28 19:00

Fix ImportError: cannot import name 'genai' from 'google' in Python deployments by switching to google-genai

Fix ImportError: cannot import name 'genai' from 'google' in deployments: install google-genai, replace google-generativeai imports, pin google-genai>=1.19.0.

Fixing ImportError: cannot import name 'genai' from 'google' during deployment is straightforward once you align your code and dependencies with the current Google GenAI SDK. If your script runs locally but fails in production, the mismatch usually comes from which SDK is installed and how you import it. Here’s how to make it consistent and stable.

Problem overview

You deploy a Python app and see the following stack trace ending with an import failure:

Traceback (most recent call last):
  File "/opt/render/project/src/bot.py", line 4, in <module>
    from google import genai as google_genai
ImportError: cannot import name 'genai' from 'google' (unknown location)

The imports in the application look roughly like this:

import discord as dsc
from discord import app_commands as slash
import google.generativeai as legacy_sdk
from google import genai as unified_api
from google.genai.types import Tool as T, GenerateContentConfig as GenCfg, GoogleSearch as GSearch
import os as osmod
import re as regex
from urllib.parse import urlparse as parse_url
from dotenv import load_dotenv as loadenv
from flask import Flask as WebApp
from threading import Thread as BgThread
from datetime import datetime as dt, timedelta as delta
import PIL.Image as pilimg
import io as iobuf

Why this happens

google-generativeai is deprecated. The current, unified SDK is published as google-genai, and the import paths changed accordingly. The error appears when the runtime cannot resolve from google import genai, which is what the new SDK exposes. If it runs locally but fails after deployment, the production environment doesn’t have the new package available under the expected namespace.

Also note that in the traceback, the actionable part is at the end: ImportError: cannot import name 'genai' from 'google'. The preceding lines simply lead you to the failing import location.

Solution

Install the unified Google GenAI SDK and use the updated import paths. The package to install is google-genai:

pip install google-genai

If you deploy with a requirements.txt, ensure the runtime installs the correct version. Adding this line resolves the import error in deployment environments:

google-genai>=1.19.0

Then switch to the unified imports:

from google import genai as gai
from google.genai import types as gai_types

Corrected example

The following snippet keeps the program’s behavior intact while aligning imports with the current SDK. Names are adjusted without altering functionality:

import discord as dsc
from discord import app_commands as slash
from google import genai as gai
from google.genai import types as gai_types
import os as osmod
import re as regex
from urllib.parse import urlparse as parse_url
from dotenv import load_dotenv as loadenv
from flask import Flask as WebApp
from threading import Thread as BgThread
from datetime import datetime as dt, timedelta as delta
import PIL.Image as pilimg
import io as iobuf

If your project previously mixed legacy and unified imports, standardize on the new ones shown above after installing google-genai.

Why you should care

SDK deprecations ripple into CI/CD and hosting platforms. Local machines can mask issues if they carry older or different packages. Being explicit about the dependency—both the package name and a compatible version—prevents surprises when your code lands in a clean environment.

When troubleshooting import failures in deployed apps, focus on two things: use the right package for the import path you rely on, and make sure that package is actually installed in the target environment. If you need to narrow down the cause, reduce your script to a minimal reproducible example that only imports and initializes what you’re testing.

Wrap-up and practical advice

Move from the deprecated google-generativeai to the unified google-genai, install it in your deployment environment, and update import statements to from google import genai and from google.genai import types. Pin or constrain the version in requirements.txt, for example google-genai>=1.19.0, so the runtime consistently matches your local setup. If you still see issues, trim the code to the smallest snippet that reproduces the import to verify that your deployment actually installs the expected package.