# Save the image to a file
image_data = [
    output.result
    for output in response.output
    if output.type == "image_generation_call"
]

if image_data:
    image_base64 = image_data[0]
    with open("otter.png", "wb") as f:
        f.write(base64.b64decode(image_base64))
```

You can
[provide input images](https://platform.openai.com/docs/guides/image-generation?image-generation-model=gpt-image-1#edit-images)
using file IDs or base64 data.

To force the image generation tool call, you can set the parameter `tool_choice`
to `{"type": "image_generation"}`.

### Tool options

You can configure the following output options as parameters for the
[image generation tool](https://platform.openai.com/docs/api-reference/responses/create#responses-create-tools):

- Size: Image dimensions (e.g., 1024x1024, 1024x1536)
- Quality: Rendering quality (e.g. low, medium, high)
- Format: File output format
- Compression: Compression level (0-100%) for JPEG and WebP formats
- Background: Transparent or opaque

`size`, `quality`, and `background` support the `auto` option, where the model
will automatically select the best option based on the prompt.

For more details on available options, refer to the
[image generation guide](https://platform.openai.com/docs/guides/image-generation#customize-image-output).

### Revised prompt

When using the image generation tool, the mainline model (e.g. `gpt-4.1`) will
automatically revise your prompt for improved performance.

You can access the revised prompt in the `revised_prompt` field of the image
generation call:

```json
{
  "id": "ig_123",
  "type": "image_generation_call",
  "status": "completed",
  "revised_prompt": "A gray tabby cat hugging an otter. The otter is wearing an orange scarf. Both animals are cute and friendly, depicted in a warm, heartwarming style.",
  "result": "..."
}
```

### Prompting tips

Image generation works best when you use terms like "draw" or "edit" in your
prompt.

For example, if you want to combine images, instead of saying "combine" or
"merge", you can say something like "edit the first image by adding this element
from the second image".

## Multi-turn editing

You can iteratively edit images by referencing previous response or image IDs.
This allows you to refine images across multiple turns in a conversation.

Using previous response ID

```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" }],
});

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("cat_and_otter.png", Buffer.from(imageBase64, "base64"));
}

// Follow up

const response_fwup = await openai.responses.create({
  model: "gpt-5",
  previous_response_id: response.id,
  input: "Now make it look realistic",
  tools: [{ type: "image_generation" }],
});

const imageData_fwup = response_fwup.output
  .filter((output) => output.type === "image_generation_call")
  .map((output) => output.result);

if (imageData_fwup.length > 0) {
  const imageBase64 = imageData_fwup[0];
  const fs = await import("fs");
  fs.writeFileSync(
    "cat_and_otter_realistic.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"}],
)

image_data = [
    output.result
    for output in response.output
    if output.type == "image_generation_call"
]

if image_data:
    image_base64 = image_data[0]

    with open("cat_and_otter.png", "wb") as f:
        f.write(base64.b64decode(image_base64))