Skip to main content
Lesson 1 of 12 · Image Generation via API: GPT Image

The Image Generation API in 2026

Lesson 1 of 12 Updated November 2, 2026 Developer
On this page
  1. The Image Generation API in 2026
  2. What GPT Image Changes
  3. The Two Main Endpoints
  4. Installation and Configuration
  5. Model and Pricing
  6. What You Will Learn

Updated November 2, 2026 — Image models. gpt-image-1, the model this course was originally written for, was discontinued on October 23, 2026. Additionally, gpt-image-1.5, gpt-image-1-mini, and chatgpt-image-latest will be retired on December 1, 2026. Examples now use gpt-image-2.5-sunburst, which offers the highest precision for editing; gpt-image-2.5-flare is faster for standard generation. Both models also support xhigh and max quality settings. GPT Image models always return images in base64: the response_format parameter from older DALL·E models is unsupported and has been removed from the code. Source: Image generation (OpenAI).

The Image Generation API in 2026

AI image generation has evolved significantly. In 2026, GPT Image is OpenAI’s reference model for creating and editing images via the API; it replaces the now-obsolete DALL·E 2 and DALL·E 3 models. If you have existing code for these earlier generations, treat it as a starting point for a rewrite rather than a foundation to keep: parameters, return formats, and especially prompt engineering strategies have all changed.

This lesson introduces the current ecosystem, GPT Image capabilities, and how you will interact with the API throughout this course.

What GPT Image Changes

The first major shift involves text. Previous generations produced approximate, decorative but illegible letters; GPT Image renders faithful typography, finally making posters, promotional banners, and interface mockups viable where the message matters as much as the image. The second shift concerns contextual understanding: the model interprets complex instructions with nuance, so a multi-sentence prompt describing a scene, its lighting, and framing yields a coherent result rather than a collage of keywords.

Next comes native editing. You no longer start systematically from a blank page: the model can modify an existing image by replacing an interior area (inpainting) or extending the image beyond its borders (outpainting). Practically, changing the sky in a landscape photograph no longer requires regenerating everything and losing the subject in the process. Added to this is stylistic consistency maintained across successive generations—valuable when producing a series of illustrations for the same article—and multiple resolution options, from square to portrait and landscape formats.

The Two Main Endpoints

The API exposes two methods, which you will explore in depth in subsequent lessons. The first creates an image from a text description.

import base64
from openai import OpenAI

client = OpenAI()

response = client.images.generate(
    model="gpt-image-2.5-sunburst",
    prompt="A Breton lighthouse at sunset, watercolor style",
    n=1,
    size="1024x1024"
)

with open("lighthouse.png", "wb") as f:
    f.write(base64.b64decode(response.data[0].b64_json))

The second takes an image you provide and regenerates only the part designated by a mask.

response = client.images.edit(
    model="gpt-image-2.5-sunburst",
    image=open("original_photo.png", "rb"),
    mask=open("mask.png", "rb"),
    prompt="Replace the sky with a starry sky"
)

Installation and Configuration

Install the official SDK first.

pip install openai

Next, place your API key in a .env file, never hard-coded in your script: a shared script or one pushed to a repository would expose your key.

OPENAI_API_KEY=sk-your-api-key

The client then retrieves it automatically at startup, without you needing to pass it as an argument.

import os
from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()
client = OpenAI()  # automatically uses OPENAI_API_KEY

Model and Pricing

Current models are gpt-image-2.5-sunburst, the most precise for editing, and gpt-image-2.5-flare, faster for standard generation; gpt-image-1, the model this course was originally written for, was discontinued on October 23, 2026. Here are the characteristics you will handle constantly.

ParameterValue
Modelgpt-image-2.5-sunburst or gpt-image-2.5-flare
Resolutions1024×1024, 1024×1536, 1536×1024 recommended; custom dimensions possible (multiples of 16, up to 3840 pixels per side)
Qualitieslow, medium, high, xhigh, max, auto (default)
Output FormatsBase64-encoded image, PNG (default), JPEG, or WebP
PricingPer token, varies by quality and size

Keep in mind one point that surprises DALL·E veterans: GPT Image models never return a URL, always a base64-encoded image. Decode it and save the file immediately after generation; your application stores and serves this file, not the API response.

What You Will Learn

The course progresses in four stages. You start with GPT Image fundamentals via the API—generation and editing—before moving to concrete use cases in e-commerce, marketing, and design. Advanced techniques will then teach you to build complex visual prompts and integrate generation into multi-tool pipelines. The final part covers production deployment, including cost optimization and content moderation. Each lesson contains functional Python code you can run directly.

Before going further, verify your environment is ready. Run this minimal script: if an image file appears, your key is valid, the SDK is correctly installed, and you can proceed to the next lesson.

import base64
from openai import OpenAI

client = OpenAI()

response = client.images.generate(
    model="gpt-image-2.5-sunburst",
    prompt="A ginger cat sitting on a pile of books, realistic photo",
    n=1,
    size="1024x1024",
    quality="medium"
)

with open("cat.png", "wb") as f:
    f.write(base64.b64decode(response.data[0].b64_json))

print("Image generated: cat.png")