Sunday , November 29 2026
Use Google Gemini from Python: API key, text generation, image questions, chat and settings.

Google Gemini Tutorial: Gemini API with Python

Updated: 28 November 2026Level: BeginnerReading time: about 9 minutesgoogle-genai Python SDK

Google Gemini is Google’s family of multimodal AI models. It can understand and generate text, and also read images, PDFs, audio and video. You can use it in the Gemini app or from Python through the Gemini API.

What You Will Learn

  • What Gemini is and where it is available
  • How to get a free API key from Google AI Studio
  • How to generate text from Python
  • How to send an image together with a question
  • How to hold a multi-turn chat

Prerequisites

ChatGPT Tutorial (same ideas, different SDK).

Where You Can Use Gemini

Place Best for
Gemini app (web and mobile) Everyday questions, writing, planning
Google Workspace Help in Gmail, Docs, Sheets and Slides
Google AI Studio Trying prompts and getting an API key
Gemini API / Vertex AI Building your own apps

Step 1: Install and Get a Key

Install
pip install google-genai pillow

Create a key in Google AI Studio and save it as the environment variable GEMINI_API_KEY. Never put the key directly in your code.

Step 2: Generate Text

Example
from google import genai

client = genai.Client()          # reads GEMINI_API_KEY
MODEL = "gemini-2.5-flash"       # replace with a current model name if needed

response = client.models.generate_content(
    model=MODEL,
    contents="Explain gradient descent to a 12th class student in 3 lines."
)
print(response.text)

Explanation: generate_content sends your prompt and returns a response; response.text holds the generated text.

Output
Gradient descent is a way for a computer to find the lowest point of an error curve.
It checks which direction makes the error go down and takes a small step that way.
Repeating these small steps again and again brings it close to the best answer.
Note: Model names change often. Check the provider’s documentation for the current list and replace the value of MODEL if needed. LLM answers also vary from run to run, so your output will not match the sample exactly.

Step 3: Ask About an Image (Multimodal)

Example
from google import genai
from PIL import Image

client = genai.Client()
img = Image.open("sales_chart.png")

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents=[img, "Describe the trend in this chart and give one business insight."]
)
print(response.text)

Explanation: contents can be a list that mixes images and text. This is what makes Gemini multimodal – see Multimodal AI.

Step 4: Multi-Turn Chat

Example
chat = client.chats.create(model="gemini-2.5-flash")
print(chat.send_message("Suggest a beginner ML project.").text)
print(chat.send_message("Now list the Python libraries I need for it.").text)

Explanation: The chat object keeps the history for you, so the second question (“for it”) is understood in context.

Useful Settings

Example
from google.genai import types

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="Write a tagline for an AI course.",
    config=types.GenerateContentConfig(
        temperature=0.9,
        max_output_tokens=50,
        system_instruction="You write short, catchy marketing lines."
    )
)
Setting Effect
temperature Creativity (0 = focused)
max_output_tokens Maximum length of the reply
system_instruction Role and rules for the model

Common Errors

Problem Cause and fix
ImportError: cannot import name genai Install google-genai (the newer SDK), not only the older google-generativeai.
Permission or API key error Set GEMINI_API_KEY and restart the notebook.
Model not found Check the current model names in Google AI Studio.

Series

Generative AI series: Introduction · ChatGPT · Google Gemini · Claude AI · Prompt Engineering · AI Agents · RAG · Fine-Tuning LLMs · Multimodal AI

Interview Questions

Q: What does multimodal mean?
The model can take more than one type of input, such as text plus images, audio or video.

Q: Gemini API vs Vertex AI?
The Gemini API (AI Studio) is the quickest way to start; Vertex AI is Google Cloud’s enterprise platform with more control, security and MLOps features.

Practice Exercises

  1. Ask Gemini to summarise a PDF of your notes.
  2. Upload a photo of handwritten maths and ask it to solve step by step.
  3. Build a 3-question quiz generator with a fixed system instruction.

Summary

Gemini is Google’s multimodal model family. With the google-genai SDK you can generate text, mix images with questions, chat over several turns and control creativity and length.

Next: Claude AI · Prompt Engineering

About Machine Learning

Check Also

Fine-Tuning LLMs: LoRA, QLoRA and Hugging Face

When and how to fine-tune large language models: data format, LoRA, QLoRA, Hugging Face TRL and evaluation.