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

    Browser-Use Tutorial: AI-Powered Browser Automation with LLM Agents

    Tutorial Browser-Use: Automasi Browser dengan AI Agent Pendahuluan Browser-Use adalah library Python open-source yang me...

    MLX Tutorial: Apple's Machine Learning Framework for Apple Silicon

    Tutorial MLX: Framework Machine Learning Apple untuk Apple Silicon MLX adalah framework machine learning open-source dar...

    TRL Tutorial: LLM Post-Training with SFT, DPO, and Reward Modeling

    Post-Training LLM dengan TRL: SFT, Reward Modeling, dan DPO Setelah sebuah base language model selesai dipretraining, mo...

    Axolotl Tutorial: Configuration-Driven LLM Fine-Tuning

    Fine-Tuning LLM Berbasis Konfigurasi dengan Axolotl Kebanyakan proyek fine-tuning dimulai dengan cara yang sama: seseora...