How to beginner · 3 min read

How to install Google Generative AI python library

Quick answer
Install the Google Generative AI Python library using pip install google-generativeai. Then import it in your Python code with import google.generativeai as genai to start using the Gemini API.

PREREQUISITES

  • Python 3.8+
  • pip installed
  • Google Cloud project with Generative AI API enabled
  • Google Cloud authentication setup (service account or ADC)

Setup

Install the official Google Generative AI Python library using pip. Ensure you have Python 3.8 or higher and pip installed on your system. Also, set up Google Cloud authentication by creating a service account with the appropriate permissions and downloading its JSON key file or use Application Default Credentials.

bash
pip install google-generativeai
output
Collecting google-generativeai\n  Downloading google_generativeai-0.1.0-py3-none-any.whl (XX kB)\nInstalling collected packages: google-generativeai\nSuccessfully installed google-generativeai-0.1.0

Step by step

Here is a minimal example to initialize the Google Generative AI client and generate text using the Gemini model.

python
import os
import google.generativeai as genai

# Set your Google API key or ensure your environment is authenticated
os.environ['GOOGLE_API_KEY'] = os.environ.get('GOOGLE_API_KEY')

# Initialize the client
client = genai.Client()

# Generate text with Gemini model
response = client.generate_text(
    model='gemini-1.5-turbo',
    prompt='Write a short poem about AI advancements.'
)

print(response.text)
output
AI advances bright and clear,
Shaping futures far and near,
Code and thought entwined as one,
New horizons just begun.

Common variations

You can use different Gemini models by changing the model parameter, such as gemini-2.0-flash for faster responses. The library supports asynchronous calls and streaming responses for real-time applications.

python
import asyncio
import google.generativeai as genai

async def async_generate():
    client = genai.Client()
    response = await client.generate_text_async(
        model='gemini-2.0-flash',
        prompt='Explain quantum computing in simple terms.'
    )
    print(response.text)

asyncio.run(async_generate())
output
Quantum computing uses quantum bits, or qubits, which can be both 0 and 1 at the same time, enabling powerful calculations that classical computers can't easily perform.

Troubleshooting

If you encounter authentication errors, verify your Google Cloud credentials are correctly set in the environment or your service account JSON path is configured. For installation issues, ensure your pip is up to date and Python version is 3.8 or higher.

Key Takeaways

  • Use pip install google-generativeai to install the official Gemini Python SDK.
  • Set up Google Cloud authentication before using the library to avoid authorization errors.
  • Switch Gemini models by changing the model parameter for different performance and capabilities.
Verified 2026-04 · gemini-1.5-turbo, gemini-2.0-flash
Verify ↗