Complete Google Gemini API Tutorial with Python: From Basics to Advanced

# Tutorial Lengkap Google Gemini API dengan Python: Dari Dasar hingga Mahir Google Gemini API adalah salah satu API AI paling powerful yang tersedia saat ini. Dengan kemampuan multimodal — memproses...

By Ruby Abdullah · · tutorial
Gemini APIGoogle AILLMMultimodal AIPython

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-genai library

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

  • Visit Google AI Studio
  • Click "Get API Key" in the sidebar menu
  • Select "Create API key in new project" or choose an existing project
  • Copy the generated 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

    Related Articles

    OpenRouter: One API for Hundreds of LLMs (OpenAI, Anthropic, Google, Meta, Mistral)

    OpenRouter: Satu API untuk Ratusan LLM (OpenAI, Anthropic, Google, Meta, Mistral) Halo temen-temen, ketemu lagi sama aku...

    Complete Guidance Tutorial: Constrained Generation and Structured Output from LLMs

    Tutorial Lengkap Guidance: Constrained Generation dan Structured Output dari LLM Halo temen-temen, di tutorial kali ini ...

    Zep Tutorial: Long-Term Memory for AI Agents with a Temporal Knowledge Graph

    Zep: Bikin AI Agent Punya Memori Jangka Panjang dengan Temporal Knowledge Graph Temen-temen, pernah ngobrol sama chatbot...

    Fireworks AI: A Super Fast Inference Platform for Open LLMs and Multimodal Models

    Fireworks AI: Platform Inference Super Cepat buat Open LLM dan Model Multimodal Halo temen-temen, di tutorial kali ini a...