Google AI Generative Language API Client Library
The `google-ai-generativelanguage` library is the low-level Python client for the Google Generative Language API. It provides programmatic access to Google's generative AI models, such as Gemini, allowing developers to perform tasks like text generation, multimodal content understanding, and more. Maintained by Google, it is currently at version 0.11.0 and is compatible with Python 3.9 and newer. While functional for direct API interaction, for a higher-level and more unified experience with Gemini models, the `google-genai` library is often recommended.
Warnings
- breaking The `google-generativeai` library (also known as the Google AI Python SDK for the Gemini API) was officially deprecated on November 30, 2025, in favor of the newer `google-genai` library. While `google-ai-generativelanguage` is the low-level GAPIC client, users migrating from `google-generativeai` might mistakenly try to use it as a direct replacement. Ensure you are using the correct library for your needs, preferably `google-genai` for high-level API access.
- gotcha This library (`google-ai-generativelanguage`) is a low-level GAPIC client, offering direct interaction with the Generative Language API. For simpler interactions with Gemini models (especially Gemini 2.0+ via AI Studio), the `google-genai` library offers a higher-level, more unified API that simplifies many common use cases. Using `google-ai-generativelanguage` directly requires more explicit handling of API requests/responses.
- gotcha Authentication for `google-ai-generativelanguage` typically relies on Application Default Credentials (ADC), which are automatically discovered in Google Cloud environments or set up locally via `gcloud auth application-default login`. If you intend to use an API key (e.g., from Google AI Studio) for direct access, you should generally use the `google-genai` library, which is designed for API key-based authentication via the `GEMINI_API_KEY` environment variable.
- deprecated This library requires Python 3.9 or newer. Python versions 3.8 and older are no longer supported.
Install
-
pip install google-ai-generativelanguage
Imports
- GenerativeServiceClient
from google.ai import generativelanguage_v1beta as glm client = glm.GenerativeServiceClient()
Quickstart
import os
from google.ai import generativelanguage_v1beta as glm
# Initialize the client. This client will attempt to ascertain credentials
# from the environment, e.g., via Application Default Credentials (ADC).
# For local development, consider `gcloud auth application-default login`.
# For API Key-based authentication (e.g., from Google AI Studio), the `google-genai`
# library (pip install google-genai) is generally recommended.
client = glm.GenerativeServiceClient()
model_name = "models/gemini-pro" # Or another supported model like "models/text-bison-001"
# Prepare the content for generation
content = glm.Content(
parts=[glm.Part(text="Write a short poem about a cat sitting by a window.")]
)
try:
# Make the generate content request
response = client.generate_content(model=model_name, contents=[content])
# Extract the generated text
if response.candidates:
print(response.candidates[0].content.parts[0].text)
else:
print("No content generated.")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure you have enabled the Generative Language API in your Google Cloud project and set up authentication.")
# It's good practice to explicitly close the client when done to release resources.
client.close()