Complete Google Gemini API Tutorial with Python: From Basics to Advanced
Google Gemini API is one of the most powerful AI APIs available today. With native multimodal capabilities — processing text, images, audio, and video — Gemini provides exceptional flexibility for building modern AI applications. This tutorial will guide you from installation to advanced usage of the Gemini API using Python.
Why Google Gemini API?
Gemini API offers several advantages that make it worth learning:
- Native Multimodal: Unlike many other LLMs that only process text, Gemini is designed from the ground up to understand text, images, audio, and video simultaneously
- Large Context Window: Gemini 2.5 Pro supports context windows up to 1 million tokens, enabling analysis of long documents or large codebases
- Grounding with Google Search: Ability to connect responses with real-time data from Google Search
- Competitive Pricing: Generous free tier and competitive pricing for production use
- Official Python SDK: Well-documented and easy-to-use
google-genailibrary
Installation and Setup
Prerequisites
- Python 3.9 or later
- Google Cloud account or API key from Google AI Studio
- pip or uv package manager
SDK Installation
pip install google-genai
Or using uv:
uv pip install google-genai
Getting Your API Key
Environment Configuration
Store your API key as an environment variable for security:
export GOOGLEAPIKEY="your-api-key-here"
Or create a .env file:
GOOGLEAPIKEY=your-api-key-here
Basic Usage: Text Generation
Hello Gemini
from google import genai
client = genai.Client(apikey="YOURAPIKEY")
response = client.models.generatecontent(
model="gemini-2.5-flash",
contents="Explain what machine learning is in 3 sentences."
)
print(response.text)
Example output:
Machine learning is a branch of artificial intelligence that enables
computers to learn from data without being explicitly programmed. ML
algorithms identify patterns in training data and use those patterns
to make predictions or decisions on new data. This technique is widely
used in various applications such as image recognition, natural language
processing, and recommendation systems.
Choosing the Right Model
Google provides several Gemini models:
| Model | Strength | Use Case |
|-------|----------|----------|
| gemini-2.5-pro | Most capable, best reasoning | Complex tasks, coding, analysis |
| gemini-2.5-flash | Fast and efficient | Chat, summarization, general tasks |
| gemini-2.0-flash | Balanced speed/quality | Production workloads |
models = client.models.list()
for model in models:
print(f"{model.name}: {model.displayname}")
Generation Configuration
You can control model behavior with parameters:
from google.genai import types
response = client.models.generatecontent(
model="gemini-2.5-flash",
contents="Write a poem about artificial intelligence.",
config=types.GenerateContentConfig(
temperature=0.7,
topp=0.9,
topk=40,
maxoutputtokens=500,
)
)
print(response.text)
Key parameters:
- temperature (0.0-2.0): Higher values produce more creative output, lower values are more deterministic
- topp: Nucleus sampling, controls output diversity
- topk: Number of top candidate tokens to consider
- maxoutputtokens: Maximum response length