Skip to main content
To use an image generation model, you need to use the responses API. The API accepts text and image inputs, and returns generated images as base64-encoded data. The examples below use the FLUX.2-klein-4B model, but you can replace it with any supported model that’s listed as an “Image” type.

Requirements

  • An API key. The code below assumes you set it in an environment variable:
  • The openai Python package. You can install it with this command:

Generate an image

To generate an image, set input to a text description of the image and set generation parameters in provider_options.image.
You should quickly see an image that looks like this:

Transform an image

To transform an image, set input to a user message containing:
  • An input_image block with an image URL or base64-encoded data URI.
  • An input_text block describing the transformation.
This example encodes the landscape.png file created above and transforms it into a watercolor painting:
You should see the image transformed like this: