Google Generative AI SDK (Gemini)
DEPRECATED. The original Python SDK for Gemini API (AI Studio). Replaced by google-genai (the unified Google Gen AI SDK). End-of-life: November 30, 2025. All support permanently ended. New features like Live API, Veo, Imagen 3 are only available in google-genai. Last version: 0.8.6. Do not use for new projects.
Common errors
-
ModuleNotFoundError: No module named 'google.generativeai'
cause The `google-generativeai` package is either not installed, installed in a different Python environment, or there's a typo in the import statement.fixEnsure the package is installed in your active Python environment: `pip install -U google-generativeai`. Verify your Python version is supported (Python 3.9+ is generally required for recent versions). -
AttributeError: module 'google.generativeai' has no attribute 'GenerativeModel'
cause You are likely using an outdated version of the `google-generativeai` library, or the `GenerativeModel` class has been renamed/moved in your specific version. This error is also common when migrating from `google-generativeai` to the newer `google-genai` SDK, where the client initialization and model access patterns differ.fixUpdate the library to the latest version: `pip install --upgrade google-generativeai`. If this doesn't resolve it, verify the correct API usage for your installed version, or consider migrating to the `google-genai` SDK where model instantiation is typically handled differently (e.g., `genai.GenerativeModel('model_name')` might be replaced by `client.models.get('model_name')` in the new SDK). -
ImportError: cannot import name 'get_default_text_client' from 'google.generativeai.client'
cause The `get_default_text_client` function was a component of older versions of the `google-generativeai` library, often used for the deprecated PaLM API, and has since been removed or is not available in newer versions or the `google-genai` SDK.fixUpdate to the latest `google-generativeai` version and adjust your code to use current methods for client initialization (e.g., `genai.configure(api_key=...)` and `genai.GenerativeModel(...)`). For new projects or if this persists, migrate to the `google-genai` SDK which has a different client and model instantiation approach. -
403 Request had insufficient authentication scopes. [reason: "ACCESS_TOKEN_SCOPE_INSUFFICIENT"]
cause This error indicates that the API key or service account used for authentication does not have the necessary permissions (scopes) to access the requested Generative AI API or that the API key is missing/invalid.fixEnsure your Google API key is correctly set (e.g., via `genai.configure(api_key="YOUR_API_KEY")` or as an environment variable `GOOGLE_API_KEY`). If using Google Cloud credentials, verify that the service account has roles like 'Vertex AI User' or 'AI Platform Developer' enabled, and that the Vertex AI API is enabled for your project. -
google.api_core.exceptions.FailedPrecondition: 400 User location is not supported for the API use.
cause The Google Generative AI API might not be available in your geographical region or the specific model you are trying to use has regional restrictions.fixCheck the official Google Generative AI documentation for supported regions. If your region is not supported, you may need to use a proxy or configure your environment to route requests through a supported region, potentially using the Vertex AI SDK which allows specifying locations.
Warnings
- breaking google-generativeai is fully end-of-life as of November 30, 2025. No bug fixes, no security patches, no new features. The repo is archived at google-gemini/deprecated-generative-ai-python.
- breaking google-cloud-aiplatform generative AI module deprecated June 24, 2026. SDK releases after that date will not include generative AI modules.
- breaking API surface completely changed. genai.configure() removed. GenerativeModel class removed. New SDK uses Client() instance with client.models.generate_content().
- breaking GenerationConfig renamed to GenerateContentConfig and moved to google.genai.types.
- gotcha Two separate env vars for API key depending on mode. Gemini API mode reads GEMINI_API_KEY. Google AI Studio also accepts GOOGLE_API_KEY. Vertex AI mode uses Application Default Credentials.
- gotcha LLMs trained before 2025 have no knowledge of google-genai SDK. Will generate google-generativeai code (old API) by default. High hallucination risk on import pattern and Client() usage.
Install
-
pip install google-genai -
pip install google-generativeai
Imports
- genai.Client (new SDK)
import google.generativeai as genai genai.configure(api_key='GEMINI_API_KEY') model = genai.GenerativeModel('gemini-2.5-flash') response = model.generate_content('Explain quantum computing') print(response.text)from google import genai from google.genai import types client = genai.Client(api_key='GEMINI_API_KEY') response = client.models.generate_content( model='gemini-2.5-flash', contents='Explain quantum computing', config=types.GenerateContentConfig(temperature=0.1) ) print(response.text) - GenerationConfig (new SDK)
import google.generativeai as genai config = genai.GenerationConfig( temperature=0.1, max_output_tokens=1024 )from google.genai import types config = types.GenerateContentConfig( temperature=0.1, max_output_tokens=1024 ) - Vertex AI mode (new SDK)
import vertexai from vertexai.generative_models import GenerativeModel vertexai.init(project='your-project', location='us-central1') model = GenerativeModel('gemini-2.5-flash')from google import genai # Vertex AI mode client = genai.Client( vertexai=True, project='your-project-id', location='us-central1' ) # Gemini API mode (AI Studio) client = genai.Client(api_key='GEMINI_API_KEY')
Quickstart
# pip install google-genai
from google import genai
from google.genai import types
import os
client = genai.Client(api_key=os.environ['GEMINI_API_KEY'])
response = client.models.generate_content(
model='gemini-2.5-flash',
contents='What is the capital of France?',
config=types.GenerateContentConfig(
temperature=0.1,
max_output_tokens=256
)
)
print(response.text)