Google Colab is a free, browser-based Python environment that requires zero local setup. Combined with the Gemini API, it's the fastest way to start building AI-powered applications — no installation, no configuration, just open a notebook and start coding. This guide covers everything from your first API call to practical use cases like image analysis and multi-turn chat.
Why Google Colab + Gemini API Is the Perfect Pair
Google Colab removes the biggest barrier for new developers: environment setup. You get a fully functional Python runtime with GPU/TPU access right in your browser, connected to your Google Drive for seamless data handling.
Key advantages of this combination:
- Zero setup: No Python installation, no virtual environments, no dependency conflicts
- Free compute: CPU runtime is always free; GPU/TPU available on free and paid tiers
- Secure secrets management: Built-in Colab Secrets keep your API keys out of your code
- Gemini SDK installs in one line:
pip install google-genaiis all you need - Drive integration: Process your own documents and files directly from Google Drive
Prerequisites: Getting a Gemini API Key
Before writing any code, you need a Gemini API key:
- Go to Google AI Studio and sign in with your Google account
- Click "Get API key" in the left sidebar
- Select "Create API key" and choose a Google Cloud project
- Copy the key — it starts with
AIza...
Keep this key private. Never paste it directly into a shared notebook. We'll store it safely using Colab Secrets in the next step.
Step 1: Open a New Colab Notebook
Head to Google Colab and create a new notebook. Give it a descriptive name like gemini_tutorial.ipynb.
Step 2: Store Your API Key in Colab Secrets
Colab Secrets is a secure way to manage sensitive credentials without hardcoding them:
- Click the 🔑 key icon in the left sidebar
- Click "Add new secret"
- Set the name to
GEMINI_API_KEYand paste your API key as the value - Toggle "Notebook access" to on
You can now retrieve the key in code using userdata.get('GEMINI_API_KEY') — safe from accidental exposure when sharing notebooks.
Step 3: Install the Gemini SDK
In a new Colab cell, run:
# Install the Google Gemini SDK quietly
!pip install google-genai -qNo runtime restart is needed. The -q flag suppresses verbose output.
Step 4: Your First Text Generation
Let's start with the simplest possible Gemini API call:
from google import genai
from google.colab import userdata
# Securely retrieve the API key from Secrets
api_key = userdata.get('GEMINI_API_KEY')
client = genai.Client(api_key=api_key)
# Generate text using Gemini 3.1 Pro
response = client.models.generate_content(
model='gemini-3.1-pro',
contents='Write a Python function that generates a Fibonacci sequence with detailed comments.'
)
print(response.text)
# Expected output:
# def fibonacci(n):
# """Returns a list of Fibonacci numbers up to the nth term."""
# if n <= 0:
# return []
# ...That's it. response.text contains the model's full response. With just a few lines, you have access to one of the most capable AI models available.
Step 5: Streaming Responses in Real Time
For long outputs, streaming lets you see results as they're generated rather than waiting for the full response:
# Stream the response chunk by chunk — great for long outputs
for chunk in client.models.generate_content_stream(
model='gemini-3.1-pro',
contents='Explain the key features of the Gemini API in detail.'
):
print(chunk.text, end='', flush=True)
# Expected output:
# The Gemini API offers several powerful features... (text appears progressively)Setting end='' and flush=True ensures characters appear continuously without newline interruptions.
Step 6: Multi-Turn Conversation (Chat)
Build a context-aware chatbot that remembers previous messages:
# Create a chat session — conversation history is managed automatically
chat = client.chats.create(model='gemini-3.1-pro')
# First message
response1 = chat.send_message('Explain Python dictionaries to me.')
print('AI:', response1.text[:200], '...\n')
# Follow-up (the model remembers the previous context)
response2 = chat.send_message('Show me three practical examples with code.')
print('AI:', response2.text[:300], '...')
# Check how many turns we've had
print(f'\nConversation turns: {len(chat.get_history())}')The client.chats.create() method returns a session object that accumulates history with every send_message() call — no manual context management required.
Step 7: Image Analysis with Multimodal Inputs
Gemini's multimodal capabilities let you analyze images alongside text:
import requests
from PIL import Image
from io import BytesIO
# Fetch a sample image from the web
img_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg'
img_response = requests.get(img_url)
img = Image.open(BytesIO(img_response.content))
# Pass the image and a text prompt together
response = client.models.generate_content(
model='gemini-3.1-pro',
contents=[
img, # PIL Image object works directly
'Describe this image in detail. What do you see?'
]
)
print(response.text)
# Expected output:
# The image shows an orange tabby cat with...You can pass PIL Image objects directly in the contents list — no base64 encoding needed. This also works with images stored in your Google Drive.
Step 8: Processing Files from Google Drive
Colab's Drive integration makes it easy to analyze your own documents:
from google.colab import drive
import pathlib
# Mount Google Drive (requires one-time authentication)
drive.mount('/content/drive')
# Path to a file in your Drive (change as needed)
file_path = '/content/drive/MyDrive/sample.txt'
if pathlib.Path(file_path).exists():
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Use gemini-3.1-flash for cost-efficient batch tasks
response = client.models.generate_content(
model='gemini-3.1-flash',
contents=f'Summarize the following text in three bullet points:\n\n{content}'
)
print(response.text)
else:
print('File not found. Check the file path.')gemini-3.1-flash is the go-to model when you need cost efficiency without sacrificing quality for simpler tasks.
Step 9: Customizing Behavior with System Instructions
System instructions let you define a persona, set tone, or enforce a specific output format:
from google.genai import types
# Define model behavior with a system instruction
config = types.GenerateContentConfig(
system_instruction="""You are a seasoned Python expert.
Always structure your answers as follows:
1. A concise one-sentence explanation
2. A working code example
3. One common pitfall to avoid
Keep your tone friendly and direct — no corporate speak.""",
temperature=0.7, # Controls randomness (0 = deterministic, 2 = very creative)
max_output_tokens=1024
)
response = client.models.generate_content(
model='gemini-3.1-pro',
contents='How do list comprehensions work in Python?',
config=config
)
print(response.text)Lower temperature values produce consistent, predictable output — ideal for code generation. Higher values introduce more variety, better for creative writing.
Common Errors and How to Fix Them
RESOURCE_EXHAUSTED — Rate Limit Hit
The free tier allows up to 15 requests per minute. Use exponential backoff to handle limits gracefully:
import time
def generate_with_retry(client, prompt, max_retries=3):
"""Generate content with automatic retry on rate limit errors."""
for attempt in range(max_retries):
try:
return client.models.generate_content(
model='gemini-3.1-flash',
contents=prompt
)
except Exception as e:
if 'RESOURCE_EXHAUSTED' in str(e) and attempt < max_retries - 1:
wait_time = 2 ** attempt # 1s → 2s → 4s
print(f'Rate limited. Retrying in {wait_time}s...')
time.sleep(wait_time)
else:
raise eINVALID_ARGUMENT — Wrong Model Name
List all available models to find the correct identifier:
# List all models that support content generation
for model in client.models.list():
if 'generateContent' in [m for m in model.supported_actions]:
print(model.name)Secrets Not Loading After Session Reset
If userdata.get() throws an error after a Colab session restarts, toggle the "Notebook access" switch in Secrets off and back on to re-authorize.
Wrapping Up
In this tutorial, you learned how to:
- ✅ Set up the Gemini SDK in Google Colab and store API keys securely
- ✅ Generate text, stream responses, and build multi-turn conversations
- ✅ Analyze images and process files from Google Drive
- ✅ Customize model behavior with System Instructions
For deeper dives, check out these related articles:
- [Gemini API Quickstart]((/articles/gemini-api/gemini-api-quickstart) — Set up a local development environment
- Function Calling Complete Guide — Build agents that call external APIs
- [Google AI Studio Guide]((/articles/gemini-dev/google-ai-studio-guide) — Prototype prompts visually before writing code
Google Colab is the ideal sandbox for Gemini experimentation. Open a notebook, paste in the code above, and start building today.