Back to HomeAI API

Gemini API Python Tutorial: 2026 Complete Guide to Calling Google AI Models from Scratch

8 min min read
#Gemini API#Python#Google AI SDK#API Integration#Multimodal#Code Examples#Function Calling#Streaming#Error Handling#Tutorial

Gemini API Python Tutorial: 2026 Complete Guide to Calling Google AI Models from Scratch

Get Gemini API Running in 5 Minutes

You've already heard that Gemini API is powerful and affordable.

But you open Google's official documentation and find it long and scattered, with no clear starting point.

This tutorial saves you the time of sifting through docs. I'll walk you through Gemini API Python integration from scratch in the simplest steps -- from SDK installation to multimodal applications, with copy-paste-ready code at every step.

Need a Gemini API enterprise plan? Get better pricing through CloudInsight, no overseas payment hassles.

Python developer integrating Gemini API

TL;DR

Install the google-generativeai package -> Set API Key -> Create a model instance with GenerativeModel -> Call generate_content() and you're done. This tutorial covers text generation, image understanding, Streaming, and Function Calling, with complete runnable code.


Python Environment Preparation & Gemini SDK Installation

Answer-First: All you need is Python 3.9+, pip, and one line pip install google-generativeai to get started.

Environment Requirements

ItemMinimumRecommended
Python3.93.11+
pip21.0Latest
google-generativeai0.8.00.8.x latest
OSWindows / macOS / LinuxAny

Installation Steps

We recommend creating a virtual environment first to avoid package conflicts:

# Create virtual environment
python -m venv gemini-env

# Activate virtual environment (macOS / Linux)
source gemini-env/bin/activate

# Activate virtual environment (Windows)
gemini-env\Scripts\activate

# Install Gemini SDK
pip install google-generativeai

After installation, verify:

python -c "import google.generativeai as genai; print(genai.__version__)"

If you see a version number, the installation was successful.

Common Installation Issues

  • pip version too old: Run pip install --upgrade pip first
  • SSL errors: Corporate networks may need proxy configuration
  • M1/M2 Mac compatibility: The SDK fully supports Apple Silicon

Getting Your Gemini API Key & Setting Up Authentication

Answer-First: Get an API Key in just two clicks at Google AI Studio. Storing it in an environment variable is the safest approach.

Get Your API Key

  1. Go to Google AI Studio
  2. Log in with your Google account
  3. Click "Get API Key" -> "Create API Key"
  4. Copy the generated Key

No credit card required to get a free API Key. For complete application steps, see Gemini API Official Documentation & Feature Guide.

Set Up API Key (The Safe Way)

Never hardcode your API Key in source code.

The correct approach is using environment variables:

# macOS / Linux
export GEMINI_API_KEY="your-api-key-here"

# Windows PowerShell
$env:GEMINI_API_KEY="your-api-key-here"

Then read it in Python:

import os
import google.generativeai as genai

genai.configure(api_key=os.environ["GEMINI_API_KEY"])

If you manage environment variables with .env files, use python-dotenv:

from dotenv import load_dotenv
load_dotenv()

genai.configure(api_key=os.getenv("GEMINI_API_KEY"))

Text Generation API Call Implementation with Code Examples

Answer-First: Create a model instance with GenerativeModel, call generate_content() with your Prompt, and get AI-generated text back.

Basic Text Generation

import os
import google.generativeai as genai

genai.configure(api_key=os.environ["GEMINI_API_KEY"])

# Create model instance
model = genai.GenerativeModel("gemini-2.0-flash")

# Call API
response = model.generate_content("Describe Taiwan's night market culture in 3 key points")

# Print result
print(response.text)

That's the simplest usage. 6 lines of code to get Gemini API running.

Adjusting Generation Parameters

You can control generation results via GenerationConfig:

config = genai.GenerationConfig(
    temperature=0.7,       # Creativity level (0-2, higher = more creative)
    top_p=0.9,             # Sampling range
    top_k=40,              # Candidate token count
    max_output_tokens=1024 # Maximum output length
)

response = model.generate_content(
    "Write a short poem about a rainy day in Taipei",
    generation_config=config
)

Parameter recommendations:

Scenariotemperaturetop_pDescription
Translation, summarization0.1-0.30.8Accuracy needed
General Q&A0.5-0.70.9Balance creativity and accuracy
Creative writing1.0-1.50.95Diversity needed

Multi-Turn Conversation

chat = model.start_chat(history=[])

response = chat.send_message("Hi! I want to learn Python")
print(response.text)

response = chat.send_message("Can you recommend some beginner books?")
print(response.text)

start_chat() automatically maintains conversation history -- you don't need to manage context manually.

Purchase Gemini API through CloudInsight for exclusive enterprise discounts and uniform invoices. Learn about enterprise plans

Gemini API multimodal input processing flow


Multimodal Applications: Image Understanding & Video Analysis

Answer-First: Gemini API natively supports multimodal input -- you can send text + images (or video) simultaneously, letting AI understand visual content and generate text responses.

Image Understanding

import PIL.Image

model = genai.GenerativeModel("gemini-2.0-flash")

# Load local image
img = PIL.Image.open("receipt.jpg")

# Send image + text prompt
response = model.generate_content([
    "Please identify the item names and amounts on this receipt, output in table format",
    img
])

print(response.text)

Supported image formats: JPEG, PNG, GIF, WebP. Maximum 20MB per image.

Multi-Image Comparison

img1 = PIL.Image.open("product_a.jpg")
img2 = PIL.Image.open("product_b.jpg")

response = model.generate_content([
    "Compare the visual differences between these two products",
    img1,
    img2
])

Video Analysis

Gemini API supports direct video file uploads:

video_file = genai.upload_file("demo.mp4")

# Wait for file processing to complete
import time
while video_file.state.name == "PROCESSING":
    time.sleep(2)
    video_file = genai.get_file(video_file.name)

response = model.generate_content([
    "Please generate 5 key takeaways from this video",
    video_file
])

Video analysis is currently a unique Gemini API advantage -- neither OpenAI nor Claude supports direct video uploads.

But note: video analysis consumes a lot of tokens. A 1-minute video uses approximately 4,000-8,000 tokens. Long videos can get expensive.

If you also want to learn OpenAI's Python integration approach, see OpenAI API Python SDK Integration Complete Tutorial. The two APIs have different design philosophies, and learning both helps you choose the best fit for your project.


Advanced Techniques: Streaming, Function Calling & Error Handling

Answer-First: Streaming enables real-time response display, Function Calling lets AI call custom functions, and error handling ensures stable production operation. These three advanced techniques are essential for going live.

Streaming Response

Don't want to wait for AI to finish before seeing results? Use Streaming:

response = model.generate_content(
    "Give a detailed introduction to 5 must-visit tourist spots in Taiwan",
    stream=True
)

for chunk in response:
    print(chunk.text, end="", flush=True)

Streaming is especially useful for chatbot scenarios. Users don't have to stare at a blank screen waiting for the AI to finish.

Function Calling

Let AI call functions you define:

def get_weather(city: str) -> dict:
    """Get weather information for a specified city"""
    # Would actually call a weather API
    return {"city": city, "temp": 28, "condition": "Sunny"}

model = genai.GenerativeModel(
    "gemini-2.0-flash",
    tools=[get_weather]
)

chat = model.start_chat()
response = chat.send_message("What's the weather like in Taipei today?")

Gemini automatically determines when to call get_weather and passes the correct city parameter.

Error Handling

Error handling is a must for production environments:

import google.api_core.exceptions as exceptions

try:
    response = model.generate_content("Your Prompt")
    print(response.text)
except exceptions.ResourceExhausted:
    print("Rate limit exceeded, please try again later")
except exceptions.InvalidArgument as e:
    print(f"Invalid request parameters: {e}")
except exceptions.PermissionDenied:
    print("Invalid API Key or insufficient permissions")
except Exception as e:
    print(f"Unknown error: {e}")

Common error codes:

Error CodeCauseSolution
429Rate limit exceededAdd retry logic with increasing intervals
400Invalid request formatCheck Prompt and parameters
403Invalid API KeyConfirm Key is correct and active
500Server-side errorRetry later

API error handling flow


Next Steps: From Practice to Production

You've now learned the complete Gemini API Python integration process.

But there are several things to keep in mind between "it runs" and "it's live":

  • API Key security: Never commit to Git; use environment variables or Secret Manager. For more security tips, see API Key Management & Security Best Practices
  • Cost monitoring: Set daily usage limits to avoid unexpected overcharges. For cost differences across APIs, see AI API Pricing Comparison Complete Guide
  • Model selection: Use Flash for development testing (cheap), choose Pro or Ultra for production based on quality needs
  • Rate limits: Implement exponential backoff retry mechanisms

For a comprehensive look at Gemini API features and pricing, see Gemini API Complete Development Guide.

If you have broader interest in Python AI development, Python AI API Integration Beginner's Tutorial covers common concepts and comparisons across providers. For a deeper look at pricing differences, AI API Pricing Comparison Complete Guide is very helpful.

Need an enterprise-grade Gemini API plan? CloudInsight offers bulk token purchase discounts, uniform invoices, and Chinese technical support. Get an enterprise quote now, or join LINE Official Account for instant technical support.


References

  1. Google AI for Developers -- Gemini API Quickstart with Python (https://ai.google.dev/gemini-api/docs/quickstart?lang=python)
  2. google-generativeai PyPI package (https://pypi.org/project/google-generativeai/)
  3. Gemini API Cookbook -- GitHub (https://github.com/google-gemini/cookbook)
  4. Google AI Studio (https://aistudio.google.com)

Need Professional Cloud Advice?

Whether you're evaluating cloud platforms, optimizing existing architecture, or looking for cost-saving solutions, we can help

Book Free Consultation

Related Articles