# Image generation

Allow models to generate or edit images.

The image generation tool allows you to generate images using a text prompt, and
optionally image inputs. It leverages the
[GPT Image model](https://platform.openai.com/docs/models/gpt-image-1), and
automatically optimizes text inputs for improved performance.

To learn more about image generation, refer to our dedicated
[image generation guide](https://platform.openai.com/docs/guides/image-generation?image-generation-model=gpt-image-1&api=responses).

## Usage

When you include the `image_generation` tool in your request, the model can
decide when and how to generate images as part of the conversation, using your
prompt and any provided image inputs.

The `image_generation_call` tool call result will include a base64-encoded
image.

```javascript
import OpenAI from "openai";
const openai = new OpenAI();

const response = await openai.responses.create({
  model: "gpt-5",
  input:
    "Generate an image of gray tabby cat hugging an otter with an orange scarf",
  tools: [{ type: "image_generation" }],
});

// Save the image to a file
const imageData = response.output
  .filter((output) => output.type === "image_generation_call")
  .map((output) => output.result);

if (imageData.length > 0) {
  const imageBase64 = imageData[0];
  const fs = await import("fs");
  fs.writeFileSync("otter.png", Buffer.from(imageBase64, "base64"));
}
```

```python
from openai import OpenAI
import base64

client = OpenAI()

response = client.responses.create(
    model="gpt-5",
    input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
    tools=[{"type": "image_generation"}],
)