GEMINI LABJP
SIRI — WWDC 2026 confirms the revamped Siri runs on a Google Gemini model, though it won't ship in the EU at iOS 27 due to the DMAFLASH3.5 — Gemini 3.5 Flash is now GA, the top Flash model for sustained frontier performance on agentic and coding tasksIMAGE-GA — Gemini 3.1 Flash Image and 3.1 Pro Image are GA as native visual models; the preview versions shut down Jun 25MANAGED-AGENTS — Managed Agents launch in public preview in the Gemini API, running autonomous agents in Google-hosted isolated Linux sandboxesFILE-SEARCH — File Search now supports multimodal search, with native image embedding and retrieval via gemini-embedding-2DEPRECATION — gemini-3.1-flash-image-preview and gemini-3-pro-image-preview shut down Jun 25 — migrate to the GA models soonSIRI — WWDC 2026 confirms the revamped Siri runs on a Google Gemini model, though it won't ship in the EU at iOS 27 due to the DMAFLASH3.5 — Gemini 3.5 Flash is now GA, the top Flash model for sustained frontier performance on agentic and coding tasksIMAGE-GA — Gemini 3.1 Flash Image and 3.1 Pro Image are GA as native visual models; the preview versions shut down Jun 25MANAGED-AGENTS — Managed Agents launch in public preview in the Gemini API, running autonomous agents in Google-hosted isolated Linux sandboxesFILE-SEARCH — File Search now supports multimodal search, with native image embedding and retrieval via gemini-embedding-2DEPRECATION — gemini-3.1-flash-image-preview and gemini-3-pro-image-preview shut down Jun 25 — migrate to the GA models soon
Articles/Dev Tools
Dev Tools/2026-03-17Beginner

Google Colab × Gemini API: Complete Tutorial for Building AI Apps Free

Step-by-step guide to using the Gemini API in Google Colab. From API key setup to text generation, image analysis, and streaming — all with copy-paste Python code that works out of the box.

gemini114google-colabpython132api20tutorial19beginner27

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-genai is 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:

  1. Go to Google AI Studio and sign in with your Google account
  2. Click "Get API key" in the left sidebar
  3. Select "Create API key" and choose a Google Cloud project
  4. 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:

  1. Click the 🔑 key icon in the left sidebar
  2. Click "Add new secret"
  3. Set the name to GEMINI_API_KEY and paste your API key as the value
  4. 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 -q

No 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 e

INVALID_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.

Share

Thank You for Reading

Gemini Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

If you found this article helpful, a small tip ($1.50) would mean a lot to us. Your support helps keep this site ad-free and covers server and hosting costs.

Related Articles

Dev Tools2026-05-02
Google ADK Quickstart — Build Your First AI Agent in 30 Minutes
A hands-on Google Agent Development Kit (ADK) walkthrough for absolute beginners — from install to a working agent in 30 minutes, with the gotchas called out as you hit them.
API / SDK2026-04-20
Getting Started with Gemini API in Python — A Basics to the google-genai Library
Learn how to use the Gemini API in Python with the google-genai library. From API key setup to text generation, multi-turn chat, and streaming — all explained with working code examples.
API / SDK2026-04-18
Common Gemini API Errors for First-Time Users — And How to Fix Them
Just grabbed your Gemini API key and hitting errors right away? This guide covers the most common pitfalls in your first hours with the API — from SDK setup and environment variables to response handling and rate limits.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →