Image generation
For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending
.mdto the page URL.
Overview
The API lets you generate and edit images from text prompts using gpt-image-2.5-sunburst and gpt-image-2.5-flare. Choose Sunburst for workflows where editing precision matters most, and Flare for fast, high-quality everyday image generation. You can access image generation capabilities through two APIs:
Image API
The Image API provides two endpoints, each with distinct capabilities:
- Generations: Generate images from scratch based on a text prompt
- Edits: Modify existing images using a new prompt, either partially or entirely
Responses API
The Responses API allows you to generate images as part of conversations or multi-step flows. It supports image generation as a built-in tool, and accepts image inputs and outputs within context.
Compared to the Image API, it adds:
- Multi-turn editing: Iteratively make high fidelity edits to images with prompting
- Flexible inputs: Accept image File IDs as input images, not just bytes
For mainline models that can call the image generation tool, refer to supported models.
Choosing the right API
- If you only need to generate or edit a single image from one prompt, the Image API is your best choice.
- If you want to build conversational, editable image experiences with GPT Image, go with the Responses API.
With the Image API, set model to gpt-image-2.5-sunburst or gpt-image-2.5-flare directly. With the Responses API, select a supported mainline model at the top level and specify gpt-image-2.5-sunburst or gpt-image-2.5-flare in the image generation tool's model field.
Both APIs let you customize output by adjusting quality, size, format, and compression.
To ensure these models are used responsibly, you may need to complete the API Organization Verification from your developer console before using GPT Image models.
GPT-Image-2.6
GPT-Image-2.6 includes Sunburst for detailed visual work and Flare for fast iteration. Both models support complexity control, markup-guided edits, SVG output, and animated GIF output through the Image API. Explore Sunburst examples and their generation settings.
Set image complexity
Use the complexity request field to control how much the image model plans before rendering. Lower complexity can suit focused edits, such as changing a background or a sign. Higher complexity can help with dense text, complex layouts, and relationships between objects. Higher complexity can increase latency and introduce unwanted changes, so compare results for your task.
The supported complexity levels for Sunburst and Flare are low, medium, and high. If you omit complexity, both models use high. The separate quality setting controls rendering quality.
Adjusting complexity has no separate fee.
Sunburst generated these paper-cut football scenes from the same prompt at medium quality. Compare the trophy, the uniforms, and the placement of the “CHAMPIONS” sign as complexity changes.



Safety identifiers
If your application serves individual end users, include a stable,
privacy-preserving safety_identifier in each image generation
or image edit request. Hash a
username or email address instead of sending identifying information, and keep
the identifier to 128 characters or fewer. Safety identifiers are recommended
but not required.
The Images API user parameter is deprecated. Send the same identifier as
safety_identifier instead. Existing requests using user remain supported. A
nonempty safety_identifier takes precedence over user; when
safety_identifier is omitted, null, or an empty string, the API uses user
when available.
Generate Images
You can use the image generation endpoint to create images based on text prompts, or the image generation tool in the Responses API to generate images as part of a conversation.
To learn more about customizing the output (size, quality, format, compression), refer to the customize image output section below.
You can set the n parameter to generate multiple images at once in a single request (by default, the API returns a single image).
Image API
Generate an image
import OpenAI from "openai";
import fs from "fs";
const openai = new OpenAI();
const prompt = `
A children's book drawing of a veterinarian using a stethoscope to
listen to the heartbeat of a baby otter.
`;
const result = await openai.images.generate({
model: "gpt-image-2.5-sunburst",
prompt,
});
// Save the image to a file
const image_base64 = result.data?.[0]?.b64_json;
if (!image_base64) throw new Error("Image generation returned no image.");
const image_bytes = Buffer.from(image_base64, "base64");
fs.writeFileSync("otter.png", image_bytes);
from openai import OpenAI
import base64
client = OpenAI()
prompt = """
A children's book drawing of a veterinarian using a stethoscope to
listen to the heartbeat of a baby otter.
"""
result = client.images.generate(model="gpt-image-2.5-sunburst", prompt=prompt)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Save the image to a file
with open("otter.png", "wb") as f:
f.write(image_bytes)
package main
import (
"context"
"encoding/base64"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
result, err := client.Images.Generate(context.Background(), openai.ImageGenerateParams{
Model: openai.ImageModel("gpt-image-2.5-sunburst"),
Prompt: "A children's book drawing of a veterinarian using a stethoscope to " +
"listen to the heartbeat of a baby otter.",
})
if err != nil {
panic(err)
}
image, err := base64.StdEncoding.DecodeString(result.Data[0].B64JSON)
if err != nil {
panic(err)
}
if err := os.WriteFile("otter.png", image, 0o600); err != nil {
panic(err)
}
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.images.ImageGenerateParams;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
var images =
client
.images()
.generate(
ImageGenerateParams.builder()
.model("gpt-image-2.5-sunburst")
.prompt("A watercolor robot reading in a library")
.build());
Files.write(
Path.of("generated-image.png"),
Base64.getDecoder().decode(images.data().orElseThrow().get(0).b64Json().orElseThrow()));
using OpenAI.Images;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "gpt-image-2.5-sunburst";
ImageClient client = new(model, key);
GeneratedImage image = await client.GenerateImageAsync(
"A children's book drawing of a veterinarian using a stethoscope to "
+ "listen to the heartbeat of a baby otter."
);
await File.WriteAllBytesAsync("otter.png", image.ImageBytes.ToArray());
require "base64"
require "openai"
client = OpenAI::Client.new
result = client.images.generate(
model: "gpt-image-2.5-sunburst",
prompt: "A watercolor robot reading in a library"
)
generated_image = result.data&.first or raise "No image returned"
File.binwrite(
"generated-image.png",
Base64.strict_decode64(generated_image.b64_json)
)
curl -X POST "https://api.openai.com/v1/images/generations" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-type: application/json" \
-d '{
"model": "gpt-image-2.5-sunburst",
"prompt": "A children'\''s book drawing of a veterinarian using a stethoscope to listen to the heartbeat of a baby otter."
}' | jq -r '.data[0].b64_json' | base64 --decode > otter.png
openai images generate \
--model gpt-image-2.5-sunburst \
--prompt "A children's book drawing of a veterinarian using a stethoscope to listen to the heartbeat of a baby otter." \
--raw-output \
--transform 'data.0.b64_json' | base64 --decode > otter.png
Responses API
Generate an image
import OpenAI from "openai";
const openai = new OpenAI();
const response = await openai.responses.create({
model: "gpt-6.2-astra",
input:
"Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],
});
// 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];
if (!imageBase64) throw new Error("Image generation returned no image.");
const fs = await import("fs");
fs.writeFileSync("otter.png", Buffer.from(imageBase64, "base64"));
}
from openai import OpenAI
import base64
client = OpenAI()
response = client.responses.create(
model="gpt-6.2-astra",
input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
)
# 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))
package main
import (
"context"
"encoding/base64"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6.2-astra",
Input: responses.ResponseNewParamsInputUnion{
OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),
},
Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{Model: "gpt-image-2.5-sunburst"}}},
})
if err != nil {
panic(err)
}
saveFirstGeneratedImage(response, "otter.png")
}
func saveFirstGeneratedImage(response *responses.Response, filename string) {
for _, output := range response.Output {
if output.Type != "image_generation_call" {
continue
}
image, err := base64.StdEncoding.DecodeString(output.AsImageGenerationCall().Result)
if err != nil {
panic(err)
}
if err := os.WriteFile(filename, image, 0o600); err != nil {
panic(err)
}
return
}
panic("response did not include an image generation call")
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.Tool;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6.2-astra")
.input("Generate an image of a gray tabby cat hugging an otter with an orange scarf.")
.addTool(Tool.ImageGeneration.builder().build())
.build();
var image =
client.responses().create(params).output().stream()
.flatMap(item -> item.imageGenerationCall().stream())
.findFirst()
.orElseThrow(() -> new IllegalStateException("No image generation call returned"));
String encoded =
image.result().orElseThrow(() -> new IllegalStateException("No image returned"));
Files.write(Path.of("otter.png"), Base64.getDecoder().decode(encoded));
using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
CreateResponseOptions options = new() { Model = "gpt-6.2-astra" };
options.InputItems.Add(
ResponseItem.CreateUserMessageItem(
"Generate an image of a gray tabby cat hugging an otter with an orange scarf."
)
);
options.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));
ResponseResult response = await client.CreateResponseAsync(options);
ImageGenerationCallResponseItem image = response
.OutputItems.OfType<ImageGenerationCallResponseItem>()
.FirstOrDefault()
?? throw new InvalidOperationException("No generated image was returned.");
await File.WriteAllBytesAsync("otter.png", image.ImageResultBytes.ToArray());
require "base64"
require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6.2-astra",
input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",
tools: [
{
type: :image_generation,
model: "gpt-image-2.5-sunburst"
}
]
)
image_call = response.output.find do |item|
item.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
end
unless image_call.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
raise "No image generation call returned"
end
encoded_image = image_call.result or raise "No image returned"
File.binwrite("otter.png", Base64.strict_decode64(encoded_image))
Multi-turn image generation
With the Responses API, you can build multi-turn conversations involving image generation either by providing image generation calls outputs within context (you can also just use the image ID), or by using the previous_response_id parameter.
This lets you iterate on images across multiple turns—refining prompts, applying new instructions, and evolving the visual output as the conversation progresses.
With the Responses API image generation tool, supported tool models can choose whether to generate a new image or edit one already in the conversation. The optional action parameter controls this behavior: keep action: "auto" to let the model decide, set action: "generate" to always create a new image, or set action: "edit" to force editing when an image is in context.
Force image creation with action
import OpenAI from "openai";
const openai = new OpenAI();
const response = await openai.responses.create({
model: "gpt-6.2-astra",
input:
"Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools: [
{ type: "image_generation", model: "gpt-image-2.5-sunburst", action: "generate" },
],
});
// 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];
if (!imageBase64) throw new Error("Image generation returned no image.");
const fs = await import("fs");
fs.writeFileSync("otter.png", Buffer.from(imageBase64, "base64"));
}
from openai import OpenAI
import base64
client = OpenAI()
response = client.responses.create(
model="gpt-6.2-astra",
input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools=[
{"type": "image_generation", "model": "gpt-image-2.5-sunburst", "action": "generate"}
],
)
# 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))
package main
import (
"context"
"encoding/base64"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6.2-astra",
Input: responses.ResponseNewParamsInputUnion{
OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),
},
Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{Model: "gpt-image-2.5-sunburst", Action: "generate"}}},
})
if err != nil {
panic(err)
}
for _, output := range response.Output {
if output.Type != "image_generation_call" {
continue
}
image, err := base64.StdEncoding.DecodeString(output.AsImageGenerationCall().Result)
if err != nil {
panic(err)
}
if err := os.WriteFile("otter.png", image, 0o600); err != nil {
panic(err)
}
return
}
panic("response did not include an image generation call")
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.Tool;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6.2-astra")
.input("Generate an image of a gray tabby cat hugging an otter with an orange scarf.")
.addTool(
Tool.ImageGeneration.builder().action(Tool.ImageGeneration.Action.GENERATE).build())
.build();
String imageResult =
client.responses().create(params).output().stream()
.flatMap(item -> item.imageGenerationCall().stream())
.flatMap(call -> call.result().stream())
.findFirst()
.orElseThrow(() -> new IllegalStateException("No generated image returned"));
Path output = Path.of(System.getenv().getOrDefault("OPENAI_EXAMPLE_OUTPUT_PATH", "otter.png"));
Files.write(output, Base64.getDecoder().decode(imageResult));
System.out.println(output);
using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
CreateResponseOptions options = new() { Model = "gpt-6.2-astra" };
options.InputItems.Add(
ResponseItem.CreateUserMessageItem(
"Generate an image of a gray tabby cat hugging an otter with an orange scarf."
)
);
options.Tools.Add(
ResponseTool.CreateImageGenerationTool(
model: "gpt-image-2.5-sunburst",
action: ImageGenerationToolAction.Generate
)
);
ResponseResult response = await client.CreateResponseAsync(options);
ImageGenerationCallResponseItem image = response
.OutputItems.OfType<ImageGenerationCallResponseItem>()
.FirstOrDefault()
?? throw new InvalidOperationException("No generated image was returned.");
await File.WriteAllBytesAsync("otter.png", image.ImageResultBytes.ToArray());
require "base64"
require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6.2-astra",
input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",
tools: [
{
type: :image_generation,
model: "gpt-image-2.5-sunburst",
action: :generate
}
]
)
image_call = response.output.find do |item|
item.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
end
unless image_call.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
raise "No image generation call returned"
end
encoded_image = image_call.result or raise "No image returned"
output_path = ENV.fetch("OPENAI_EXAMPLE_OUTPUT_PATH", "otter.png")
File.binwrite(output_path, Base64.decode64(encoded_image))
puts(output_path)
If you force edit without providing an image in context, the call will return an error. Leave action at auto to have the model decide when to generate or edit.
Using previous response ID
Multi-turn image generation
import OpenAI from "openai";
const openai = new OpenAI();
const response = await openai.responses.create({
model: "gpt-6.2-astra",
input:
"Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],
});
const imageData = response.output
.filter((output) => output.type === "image_generation_call")
.map((output) => output.result);
if (imageData.length > 0) {
const imageBase64 = imageData[0];
if (!imageBase64) throw new Error("Image generation returned no image.");
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-6.2-astra",
previous_response_id: response.id,
input: "Now make it look realistic",
tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],
});
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];
if (!imageBase64) throw new Error("Image generation returned no image.");
const fs = await import("fs");
fs.writeFileSync(
"cat_and_otter_realistic.png",
Buffer.from(imageBase64, "base64")
);
}
from openai import OpenAI
import base64
client = OpenAI()
response = client.responses.create(
model="gpt-6.2-astra",
input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
)
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))
# Follow up
response_fwup = client.responses.create(
model="gpt-6.2-astra",
previous_response_id=response.id,
input="Now make it look realistic",
tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
)
image_data_fwup = [
output.result
for output in response_fwup.output
if output.type == "image_generation_call"
]
if image_data_fwup:
image_base64 = image_data_fwup[0]
with open("cat_and_otter_realistic.png", "wb") as f:
f.write(base64.b64decode(image_base64))
package main
import (
"context"
"encoding/base64"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
first, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6.2-astra",
Input: responses.ResponseNewParamsInputUnion{
OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),
},
Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{Model: "gpt-image-2.5-sunburst"}}},
})
if err != nil {
panic(err)
}
saveFirstGeneratedImage(first, "cat_and_otter.png")
followUp, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6.2-astra",
PreviousResponseID: openai.String(first.ID),
Input: responses.ResponseNewParamsInputUnion{
OfString: openai.String("Now make it look realistic"),
},
Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{Model: "gpt-image-2.5-sunburst"}}},
})
if err != nil {
panic(err)
}
saveFirstGeneratedImage(followUp, "cat_and_otter_realistic.png")
}
func saveFirstGeneratedImage(response *responses.Response, filename string) {
for _, output := range response.Output {
if output.Type != "image_generation_call" {
continue
}
image, err := base64.StdEncoding.DecodeString(output.AsImageGenerationCall().Result)
if err != nil {
panic(err)
}
if err := os.WriteFile(filename, image, 0o600); err != nil {
panic(err)
}
return
}
panic("response did not include an image generation call")
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.Tool;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
var first =
client
.responses()
.create(
ResponseCreateParams.builder()
.model("gpt-6.2-astra")
.input(
"Generate an image of a gray tabby cat hugging an otter with an orange scarf.")
.addTool(Tool.ImageGeneration.builder().build())
.build());
var firstImage =
first.output().stream()
.flatMap(item -> item.imageGenerationCall().stream())
.findFirst()
.orElseThrow(() -> new IllegalStateException("No image generation call returned"));
Files.write(
Path.of("cat_and_otter.png"),
Base64.getDecoder()
.decode(
firstImage
.result()
.orElseThrow(() -> new IllegalStateException("No image returned"))));
var second =
client
.responses()
.create(
ResponseCreateParams.builder()
.model("gpt-6.2-astra")
.input("Now make it look realistic.")
.previousResponseId(first.id())
.addTool(Tool.ImageGeneration.builder().build())
.build());
var secondImage =
second.output().stream()
.flatMap(item -> item.imageGenerationCall().stream())
.findFirst()
.orElseThrow(
() -> new IllegalStateException("No follow-up image generation call returned"));
Files.write(
Path.of("cat_and_otter_realistic.png"),
Base64.getDecoder()
.decode(
secondImage
.result()
.orElseThrow(() -> new IllegalStateException("No follow-up image returned"))));
using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
CreateResponseOptions options = new() { Model = "gpt-6.2-astra" };
options.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));
options.InputItems.Add(
ResponseItem.CreateUserMessageItem(
"Generate an image of a gray tabby cat hugging an otter with an orange scarf."
)
);
ResponseResult first = await client.CreateResponseAsync(options);
ImageGenerationCallResponseItem initialImage = first
.OutputItems.OfType<ImageGenerationCallResponseItem>()
.First();
await File.WriteAllBytesAsync("cat_and_otter.png", initialImage.ImageResultBytes.ToArray());
CreateResponseOptions followUp = new()
{
Model = "gpt-6.2-astra",
PreviousResponseId = first.Id,
};
followUp.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));
followUp.InputItems.Add(ResponseItem.CreateUserMessageItem("Now make it look realistic."));
ResponseResult second = await client.CreateResponseAsync(followUp);
ImageGenerationCallResponseItem updatedImage = second
.OutputItems.OfType<ImageGenerationCallResponseItem>()
.First();
await File.WriteAllBytesAsync(
"cat_and_otter_realistic.png",
updatedImage.ImageResultBytes.ToArray()
);
require "base64"
require "openai"
client = OpenAI::Client.new
first = client.responses.create(
model: "gpt-6.2-astra",
input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",
tools: [
{
type: :image_generation,
model: "gpt-image-2.5-sunburst"
}
]
)
first_image = first.output.find do |item|
item.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
end
unless first_image.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
raise "No image generation call returned"
end
encoded_image = first_image.result or raise "No image returned"
File.binwrite("cat_and_otter.png", Base64.strict_decode64(encoded_image))
follow_up = client.responses.create(
model: "gpt-6.2-astra",
input: "Now make it look realistic.",
previous_response_id: first.id,
tools: [
{
type: :image_generation,
model: "gpt-image-2.5-sunburst"
}
]
)
follow_up_image = follow_up.output.find do |item|
item.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
end
unless follow_up_image.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
raise "No follow-up image generation call returned"
end
encoded_image = follow_up_image.result or raise "No follow-up image returned"
File.binwrite("cat_and_otter_realistic.png", Base64.strict_decode64(encoded_image))
Using image ID
Multi-turn image generation
import OpenAI from "openai";
const openai = new OpenAI();
const response = await openai.responses.create({
model: "gpt-6.2-astra",
input:
"Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],
});
const imageGenerationCalls = response.output.filter(
(output) => output.type === "image_generation_call"
);
const imageData = imageGenerationCalls.map((output) => output.result);
if (imageData.length > 0) {
const imageBase64 = imageData[0];
if (!imageBase64) throw new Error("Image generation returned no image.");
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-6.2-astra",
input: [
{
role: "user",
content: [{ type: "input_text", text: "Now make it look realistic" }],
},
{
type: "item_reference",
id: imageGenerationCalls[0].id,
},
],
tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],
});
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];
if (!imageBase64) throw new Error("Image generation returned no image.");
const fs = await import("fs");
fs.writeFileSync(
"cat_and_otter_realistic.png",
Buffer.from(imageBase64, "base64")
);
}
import openai
import base64
response = openai.responses.create(
model="gpt-6.2-astra",
input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
)
image_generation_calls = [
output for output in response.output if output.type == "image_generation_call"
]
image_data = [output.result for output in image_generation_calls]
if image_data:
image_base64 = image_data[0]
with open("cat_and_otter.png", "wb") as f:
f.write(base64.b64decode(image_base64))
# Follow up
response_fwup = openai.responses.create(
model="gpt-6.2-astra",
input=[
{
"role": "user",
"content": [{"type": "input_text", "text": "Now make it look realistic"}],
},
{
"type": "image_generation_call",
"id": image_generation_calls[0].id,
},
],
tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
)
image_data_fwup = [
output.result
for output in response_fwup.output
if output.type == "image_generation_call"
]
if image_data_fwup:
image_base64 = image_data_fwup[0]
with open("cat_and_otter_realistic.png", "wb") as f:
f.write(base64.b64decode(image_base64))
package main
import (
"context"
"encoding/base64"
"encoding/json"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
first, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6.2-astra",
Input: responses.ResponseNewParamsInputUnion{
OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),
},
Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{Model: "gpt-image-2.5-sunburst"}}},
})
if err != nil {
panic(err)
}
call := firstImageGenerationCall(first)
saveImage("cat_and_otter.png", call.Result)
input := outputAsInput(first.Output)
input = append(input, responses.ResponseInputItemParamOfMessage(
responses.ResponseInputMessageContentListParam{responses.ResponseInputContentParamOfInputText("Now make it look realistic")},
responses.EasyInputMessageRoleUser,
))
followUp, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6.2-astra",
Input: responses.ResponseNewParamsInputUnion{OfInputItemList: input},
Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{Model: "gpt-image-2.5-sunburst"}}},
})
if err != nil {
panic(err)
}
saveImage("cat_and_otter_realistic.png", firstImageGenerationCall(followUp).Result)
}
func firstImageGenerationCall(response *responses.Response) responses.ResponseOutputItemImageGenerationCall {
for _, output := range response.Output {
if output.Type == "image_generation_call" {
return output.AsImageGenerationCall()
}
}
panic("response did not include an image generation call")
}
func outputAsInput(output []responses.ResponseOutputItemUnion) []responses.ResponseInputItemUnionParam {
input := make([]responses.ResponseInputItemUnionParam, 0, len(output))
for _, item := range output {
var converted responses.ResponseInputItemUnion
if err := json.Unmarshal([]byte(item.RawJSON()), &converted); err != nil {
panic(err)
}
input = append(input, converted.ToParam())
}
return input
}
func saveImage(filename, encoded string) {
image, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
panic(err)
}
if err := os.WriteFile(filename, image, 0o600); err != nil {
panic(err)
}
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.JsonValue;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseInputItem;
import com.openai.models.responses.Tool;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
import java.util.Map;
var first =
client
.responses()
.create(
ResponseCreateParams.builder()
.model("gpt-6.2-astra")
.input(
"Generate an image of a gray tabby cat hugging an otter with an orange scarf.")
.addTool(Tool.ImageGeneration.builder().build())
.build());
var firstImage =
first.output().stream()
.flatMap(item -> item.imageGenerationCall().stream())
.findFirst()
.orElseThrow(() -> new IllegalStateException("No image generation call returned"));
Files.write(
Path.of("cat_and_otter.png"),
Base64.getDecoder()
.decode(
firstImage
.result()
.orElseThrow(() -> new IllegalStateException("No image returned"))));
var second =
client
.responses()
.create(
ResponseCreateParams.builder()
.model("gpt-6.2-astra")
.inputOfResponse(
List.of(
ResponseInputItem.ofMessage(
ResponseInputItem.Message.builder()
.role(ResponseInputItem.Message.Role.USER)
.addInputTextContent("Now make it look realistic.")
.build()),
JsonValue.from(
Map.of("type", "image_generation_call", "id", firstImage.id()))
.convert(ResponseInputItem.class)))
.addTool(Tool.ImageGeneration.builder().build())
.build());
var secondImage =
second.output().stream()
.flatMap(item -> item.imageGenerationCall().stream())
.findFirst()
.orElseThrow(
() -> new IllegalStateException("No follow-up image generation call returned"));
Files.write(
Path.of("cat_and_otter_realistic.png"),
Base64.getDecoder()
.decode(
secondImage
.result()
.orElseThrow(() -> new IllegalStateException("No follow-up image returned"))));
using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
CreateResponseOptions options = new() { Model = "gpt-6.2-astra" };
options.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));
options.InputItems.Add(
ResponseItem.CreateUserMessageItem(
"Generate an image of a gray tabby cat hugging an otter with an orange scarf."
)
);
ResponseResult first = await client.CreateResponseAsync(options);
ImageGenerationCallResponseItem initialImage = first
.OutputItems.OfType<ImageGenerationCallResponseItem>()
.First();
await File.WriteAllBytesAsync("cat_and_otter.png", initialImage.ImageResultBytes.ToArray());
CreateResponseOptions followUp = new() { Model = "gpt-6.2-astra" };
followUp.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));
followUp.InputItems.Add(ResponseItem.CreateUserMessageItem("Now make it look realistic."));
followUp.InputItems.Add(ResponseItem.CreateReferenceItem(initialImage.Id));
ResponseResult second = await client.CreateResponseAsync(followUp);
ImageGenerationCallResponseItem updatedImage = second
.OutputItems.OfType<ImageGenerationCallResponseItem>()
.First();
await File.WriteAllBytesAsync(
"cat_and_otter_realistic.png",
updatedImage.ImageResultBytes.ToArray()
);
require "base64"
require "openai"
client = OpenAI::Client.new
first = client.responses.create(
model: "gpt-6.2-astra",
input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",
tools: [
{
type: :image_generation,
model: "gpt-image-2.5-sunburst"
}
]
)
first_image = first.output.find do |item|
item.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
end
unless first_image.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
raise "No image generation call returned"
end
encoded_image = first_image.result or raise "No image returned"
File.binwrite("cat_and_otter.png", Base64.strict_decode64(encoded_image))
follow_up = client.responses.create(
model: "gpt-6.2-astra",
input: [
{
role: :user,
content: [
{
type: :input_text,
text: "Now make it look realistic."
}
]
},
{
type: :image_generation_call,
id: first_image.id
}
],
tools: [
{
type: :image_generation,
model: "gpt-image-2.5-sunburst"
}
]
)
follow_up_image = follow_up.output.find do |item|
item.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
end
unless follow_up_image.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
raise "No follow-up image generation call returned"
end
encoded_image = follow_up_image.result or raise "No follow-up image returned"
File.binwrite("cat_and_otter_realistic.png", Base64.strict_decode64(encoded_image))
Result
| "Generate an image of gray tabby cat hugging an otter with an orange scarf" |
|
| "Now make it look realistic" |
|
Streaming
The Responses API and Image API support streaming image generation. You can stream partial images as the APIs generate them, providing a more interactive experience.
You can adjust the partial_images parameter to receive 0-3 partial images.
- If you set
partial_imagesto 0, you will only receive the final image. - For values larger than zero, you may not receive the full number of partial images you requested if the full image is generated more quickly.
Responses API
Stream an image
import OpenAI from "openai";
import fs from "fs";
const openai = new OpenAI();
function saveBase64Image(filename: string, imageBase64: string | null) {
if (!imageBase64) throw new Error("Image generation returned no image.");
const imageBuffer = Buffer.from(imageBase64, "base64");
fs.writeFileSync(filename, imageBuffer);
}
const stream = await openai.responses.create({
model: "gpt-6.2-astra",
input:
"Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
stream: true,
tools: [
{ type: "image_generation", model: "gpt-image-2.5-sunburst", partial_images: 2 },
],
});
for await (const event of stream) {
if (event.type === "response.image_generation_call.partial_image") {
const idx = event.partial_image_index;
saveBase64Image(`river-partial-${idx}.png`, event.partial_image_b64);
} else if (event.type === "response.completed") {
const imageData = event.response.output
.filter((output) => output.type === "image_generation_call")
.map((output) => output.result);
if (imageData.length > 0) {
saveBase64Image("river-final.png", imageData[0]);
}
}
}
from openai import OpenAI
import base64
client = OpenAI()
def save_base64_image(filename, image_base64):
image_bytes = base64.b64decode(image_base64)
with open(filename, "wb") as f:
f.write(image_bytes)
stream = client.responses.create(
model="gpt-6.2-astra",
input="Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
stream=True,
tools=[
{"type": "image_generation", "model": "gpt-image-2.5-sunburst", "partial_images": 2}
],
)
for event in stream:
if event.type == "response.image_generation_call.partial_image":
idx = event.partial_image_index
save_base64_image(f"river-partial-{idx}.png", event.partial_image_b64)
elif event.type == "response.completed":
image_data = [
output.result
for output in event.response.output
if output.type == "image_generation_call"
]
if image_data:
save_base64_image("river-final.png", image_data[0])
package main
import (
"context"
"encoding/base64"
"fmt"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
stream := client.Responses.NewStreaming(context.Background(), responses.ResponseNewParams{
Model: "gpt-6.2-astra",
Input: responses.ResponseNewParamsInputUnion{
OfString: openai.String("Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape"),
},
Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{Model: "gpt-image-2.5-sunburst", PartialImages: openai.Int(2)}}},
})
for stream.Next() {
event := stream.Current()
if event.Type == "response.image_generation_call.partial_image" {
partial := event.AsResponseImageGenerationCallPartialImage()
saveImage(fmt.Sprintf("river-partial-%d.png", partial.PartialImageIndex), partial.PartialImageB64)
}
if event.Type == "response.completed" {
for _, output := range event.AsResponseCompleted().Response.Output {
if output.Type == "image_generation_call" {
saveImage("river-final.png", output.AsImageGenerationCall().Result)
}
}
}
}
if err := stream.Err(); err != nil {
panic(err)
}
}
func saveImage(filename, encoded string) {
image, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
panic(err)
}
if err := os.WriteFile(filename, image, 0o600); err != nil {
panic(err)
}
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseStreamEvent;
import com.openai.models.responses.Tool;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6.2-astra")
.input("Generate an image of a river made of white owl feathers.")
.addTool(Tool.ImageGeneration.builder().partialImages(2).build())
.build();
try (StreamResponse<ResponseStreamEvent> stream = client.responses().createStreaming(params)) {
var events = stream.stream().iterator();
while (events.hasNext()) {
ResponseStreamEvent event = events.next();
if (event.imageGenerationCallPartialImage().isPresent()) {
var partial = event.imageGenerationCallPartialImage().orElseThrow();
Files.write(
Path.of("river-partial-" + partial.partialImageIndex() + ".png"),
Base64.getDecoder().decode(partial.partialImageB64()));
}
if (event.completed().isPresent()) {
var image =
event.completed().orElseThrow().response().output().stream()
.flatMap(item -> item.imageGenerationCall().stream())
.findFirst()
.orElseThrow(() -> new IllegalStateException("No generated image returned"));
Files.write(
Path.of("river-final.png"),
Base64.getDecoder()
.decode(
image
.result()
.orElseThrow(
() -> new IllegalStateException("No final image returned"))));
}
}
}
require "base64"
require "openai"
client = OpenAI::Client.new
stream = client.responses.stream(
model: "gpt-6.2-astra",
input: "Generate an image of a river made of white owl feathers.",
tools: [
{
type: :image_generation,
model: "gpt-image-2.5-sunburst",
partial_images: 2
}
]
)
stream.each do |event|
case event
when OpenAI::Models::Responses::ResponseImageGenCallPartialImageEvent
image = Base64.strict_decode64(event.partial_image_b64)
File.binwrite("river-partial-#{event.partial_image_index}.png", image)
when OpenAI::Models::Responses::ResponseCompletedEvent
image_call = event.response.output.find do |item|
item.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
end
next unless image_call.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
File.binwrite(
"river-final.png",
Base64.strict_decode64(image_call.result)
)
end
end
Image API
Stream an image
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const prompt =
"Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape";
const stream = await openai.images.generate({
prompt: prompt,
model: "gpt-image-2.5-sunburst",
stream: true,
partial_images: 2,
});
for await (const event of stream) {
if (event.type === "image_generation.partial_image") {
const idx = event.partial_image_index;
const imageBase64 = event.b64_json;
if (!imageBase64) throw new Error("Image generation returned no image.");
const imageBuffer = Buffer.from(imageBase64, "base64");
fs.writeFileSync(`river${idx}.png`, imageBuffer);
}
}
from openai import OpenAI
import base64
client = OpenAI()
stream = client.images.generate(
prompt="Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
model="gpt-image-2.5-sunburst",
stream=True,
partial_images=2,
)
for event in stream:
if event.type == "image_generation.partial_image":
idx = event.partial_image_index
image_base64 = event.b64_json
image_bytes = base64.b64decode(image_base64)
with open(f"river{idx}.png", "wb") as f:
f.write(image_bytes)
package main
import (
"context"
"encoding/base64"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
stream := client.Images.GenerateStreaming(context.Background(), openai.ImageGenerateParams{
Model: openai.ImageModel("gpt-image-2.5-sunburst"),
Prompt: "Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
PartialImages: openai.Int(2),
})
for stream.Next() {
event := stream.Current()
if event.Type != "image_generation.partial_image" {
continue
}
partial := event.AsImageGenerationPartialImage()
saveImage(fmt.Sprintf("river%d.png", partial.PartialImageIndex), partial.B64JSON)
}
if err := stream.Err(); err != nil {
panic(err)
}
}
func saveImage(filename, encoded string) {
image, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
panic(err)
}
if err := os.WriteFile(filename, image, 0o600); err != nil {
panic(err)
}
}
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.images.ImageGenerateParams;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
var params =
ImageGenerateParams.builder()
.prompt(
"Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape")
.model("gpt-image-2.5-sunburst")
.partialImages(2)
.build();
try (var stream = client.images().generateStreaming(params)) {
var events = stream.stream().iterator();
while (events.hasNext()) {
var event = events.next();
if (event.generationPartialImage().isPresent()) {
var partial = event.generationPartialImage().orElseThrow();
Files.write(
Path.of("river" + partial.partialImageIndex() + ".png"),
Base64.getDecoder().decode(partial.b64Json()));
}
}
}
require "base64"
require "openai"
client = OpenAI::Client.new
stream = client.images.generate_stream_raw(
model: "gpt-image-2.5-sunburst",
prompt: "A river made of white owl feathers in a winter landscape",
partial_images: 2
)
stream.each do |event|
next unless event.is_a?(OpenAI::Models::ImageGenPartialImageEvent)
image = Base64.strict_decode64(event.b64_json)
File.binwrite("river#{event.partial_image_index}.png", image)
end
Result
| Partial 1 | Partial 2 | Final image |
|---|---|---|
![]() |
![]() |
![]() |
Prompt: Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape
Revised prompt
When using the image generation tool in the Responses API, the mainline model (for example, gpt-5.5) will automatically revise your prompt for improved performance.
You can access the revised prompt in the revised_prompt field of the image generation call:
Revised prompt response
{
"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": "..."
}
Edit Images
The image edits endpoint lets you:
- Edit existing images
- Generate new images using other images as a reference
- Edit parts of an image by uploading an image and mask that identifies the areas to replace
Create a new image using image references
You can use one or more images as a reference to generate a new image.
In this example, we'll use 4 input images to generate a new image of a gift basket containing the items in the reference images.
Responses API
With the Responses API, you can provide input images in 3 different ways:
- By providing a fully qualified URL
- By providing an image as a Base64-encoded data URL
- By providing a file ID (created with the Files API)
Create a File
Create a File
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
async function createFile(filePath: fs.PathLike) {
const fileContent = fs.createReadStream(filePath);
const result = await openai.files.create({
file: fileContent,
purpose: "vision",
});
return result.id;
}
from openai import OpenAI
client = OpenAI()
def create_file(file_path):
with open(file_path, "rb") as file_content:
result = client.files.create(
file=file_content,
purpose="vision",
)
return result.id
package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
file, err := os.Open("image.png")
if err != nil {
panic(err)
}
defer file.Close()
uploaded, err := client.Files.New(context.Background(), openai.FileNewParams{
File: file,
Purpose: openai.FilePurposeVision,
})
if err != nil {
panic(err)
}
fmt.Println(uploaded.ID)
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.files.FileCreateParams;
import com.openai.models.files.FilePurpose;
import java.nio.file.Path;
var file =
client
.files()
.create(
FileCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_FILE_PATH")))
.purpose(FilePurpose.VISION)
.build());
System.out.println(file.id());
require "openai"
require "pathname"
client = OpenAI::Client.new
file = client.files.create(
file: Pathname("image.png"),
purpose: OpenAI::Models::FilePurpose::VISION
)
puts(file.id)
Create a base64 encoded image
Create a base64 encoded image
import fs from "fs";
function encodeImage(filePath: fs.PathOrFileDescriptor) {
const base64Image = fs.readFileSync(filePath, "base64");
return base64Image;
}
import base64
def encode_image(file_path):
with open(file_path, "rb") as f:
base64_image = base64.b64encode(f.read()).decode("utf-8")
return base64_image
package main
import (
"encoding/base64"
"fmt"
"os"
)
func main() {
image, err := os.ReadFile("image.png")
if err != nil {
panic(err)
}
fmt.Println(base64.StdEncoding.EncodeToString(image))
}
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
System.out.println(encodeImage(Path.of("example.png")));
private static String encodeImage(Path path) throws java.io.IOException {
return Base64.getEncoder().encodeToString(Files.readAllBytes(path));
}
using System;
Console.WriteLine(EncodeImage("example.png"));
static string EncodeImage(string filePath) => Convert.ToBase64String(File.ReadAllBytes(filePath));
require "base64"
image = File.binread("image.png")
puts(Base64.strict_encode64(image))
Edit an image
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
function encodeImage(filePath: fs.PathOrFileDescriptor) {
return fs.readFileSync(filePath, "base64");
}
async function createFile(filePath: fs.PathLike) {
const result = await openai.files.create({
file: fs.createReadStream(filePath),
purpose: "vision",
});
return result.id;
}
const prompt = `Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures.`;
const base64Image1 = encodeImage("fixtures/body-lotion.png");
const base64Image2 = encodeImage("fixtures/soap.png");
const fileId1 = await createFile("fixtures/bath-bomb.png");
const fileId2 = await createFile("fixtures/incense-kit.png");
const response = await openai.responses.create({
model: "gpt-6.2-astra",
input: [
{
role: "user",
content: [
{ type: "input_text", text: prompt },
{
type: "input_image",
image_url: `data:image/png;base64,${base64Image1}`,
detail: "auto",
},
{
type: "input_image",
image_url: `data:image/png;base64,${base64Image2}`,
detail: "auto",
},
{
type: "input_image",
file_id: fileId1,
detail: "auto",
},
{
type: "input_image",
file_id: fileId2,
detail: "auto",
},
],
},
],
tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],
});
const imageData = response.output
.filter((output) => output.type === "image_generation_call")
.map((output) => output.result);
if (imageData.length > 0) {
const imageBase64 = imageData[0];
if (!imageBase64) throw new Error("Image generation returned no image.");
fs.writeFileSync("gift-basket.png", Buffer.from(imageBase64, "base64"));
} else {
console.log(response.output_text);
}
from openai import OpenAI
import base64
client = OpenAI()
def encode_image(file_path):
with open(file_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def create_file(file_path):
with open(file_path, "rb") as file_content:
result = client.files.create(file=file_content, purpose="vision")
return result.id
prompt = """Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures."""
base64_image1 = encode_image("body-lotion.png")
base64_image2 = encode_image("soap.png")
file_id1 = create_file("bath-bomb.png")
file_id2 = create_file("incense-kit.png")
response = client.responses.create(
model="gpt-6.2-astra",
input=[
{
"role": "user",
"content": [
{"type": "input_text", "text": prompt},
{
"type": "input_image",
"image_url": f"data:image/png;base64,{base64_image1}",
},
{
"type": "input_image",
"image_url": f"data:image/png;base64,{base64_image2}",
},
{
"type": "input_image",
"file_id": file_id1,
},
{
"type": "input_image",
"file_id": file_id2,
},
],
}
],
tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
)
image_generation_calls = [
output for output in response.output if output.type == "image_generation_call"
]
image_data = [output.result for output in image_generation_calls]
if image_data:
image_base64 = image_data[0]
with open("gift-basket.png", "wb") as f:
f.write(base64.b64decode(image_base64))
else:
print(response.output_text)
package main
import (
"context"
"encoding/base64"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
bathBombID := uploadImage(client, "bath-bomb.png")
incenseKitID := uploadImage(client, "incense-kit.png")
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6.2-astra",
Input: responses.ResponseNewParamsInputUnion{OfInputItemList: responses.ResponseInputParam{
responses.ResponseInputItemParamOfMessage(
responses.ResponseInputMessageContentListParam{
responses.ResponseInputContentParamOfInputText("Generate a photorealistic image of a gift basket on a white background labeled 'Relax & Unwind' with a ribbon and handwriting-like font, containing all the items in the reference pictures."),
{OfInputImage: &responses.ResponseInputImageParam{ImageURL: openai.String(dataURL("body-lotion.png")), Detail: responses.ResponseInputImageDetailAuto}},
{OfInputImage: &responses.ResponseInputImageParam{ImageURL: openai.String(dataURL("soap.png")), Detail: responses.ResponseInputImageDetailAuto}},
{OfInputImage: &responses.ResponseInputImageParam{FileID: openai.String(bathBombID), Detail: responses.ResponseInputImageDetailAuto}},
{OfInputImage: &responses.ResponseInputImageParam{FileID: openai.String(incenseKitID), Detail: responses.ResponseInputImageDetailAuto}},
},
responses.EasyInputMessageRoleUser,
),
}},
Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{Model: "gpt-image-2.5-sunburst"}}},
})
if err != nil {
panic(err)
}
saveFirstGeneratedImage(response, "gift-basket.png")
}
func uploadImage(client openai.Client, filename string) string {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
uploaded, err := client.Files.New(context.Background(), openai.FileNewParams{File: file, Purpose: openai.FilePurposeVision})
if err != nil {
panic(err)
}
return uploaded.ID
}
func dataURL(filename string) string {
image, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(image)
}
func saveFirstGeneratedImage(response *responses.Response, filename string) {
for _, output := range response.Output {
if output.Type != "image_generation_call" {
continue
}
image, err := base64.StdEncoding.DecodeString(output.AsImageGenerationCall().Result)
if err != nil {
panic(err)
}
if err := os.WriteFile(filename, image, 0o600); err != nil {
panic(err)
}
return
}
panic("response did not include an image generation call")
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.files.FileCreateParams;
import com.openai.models.files.FilePurpose;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseInputImage;
import com.openai.models.responses.ResponseInputItem;
import com.openai.models.responses.Tool;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
Path lotionImage = Path.of(System.getenv("OPENAI_EXAMPLE_IMAGE_PATH"));
Path soapImage = Path.of(System.getenv("OPENAI_EXAMPLE_IMAGE_PATH_2"));
Path bathBombImage = Path.of(System.getenv("OPENAI_EXAMPLE_IMAGE_PATH_3"));
Path incenseImage = Path.of(System.getenv("OPENAI_EXAMPLE_IMAGE_PATH_4"));
String lotionBase64 = Base64.getEncoder().encodeToString(Files.readAllBytes(lotionImage));
String soapBase64 = Base64.getEncoder().encodeToString(Files.readAllBytes(soapImage));
var firstFile =
client
.files()
.create(
FileCreateParams.builder().file(bathBombImage).purpose(FilePurpose.VISION).build());
var secondFile =
client
.files()
.create(
FileCreateParams.builder().file(incenseImage).purpose(FilePurpose.VISION).build());
String prompt =
"""
Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures.
""";
var input =
ResponseInputItem.ofMessage(
ResponseInputItem.Message.builder()
.role(ResponseInputItem.Message.Role.USER)
.addInputTextContent(prompt)
.addContent(
ResponseInputImage.builder()
.detail(ResponseInputImage.Detail.AUTO)
.imageUrl("data:image/png;base64," + lotionBase64)
.build())
.addContent(
ResponseInputImage.builder()
.detail(ResponseInputImage.Detail.AUTO)
.imageUrl("data:image/png;base64," + soapBase64)
.build())
.addContent(
ResponseInputImage.builder()
.detail(ResponseInputImage.Detail.AUTO)
.fileId(firstFile.id())
.build())
.addContent(
ResponseInputImage.builder()
.detail(ResponseInputImage.Detail.AUTO)
.fileId(secondFile.id())
.build())
.build());
var response =
client
.responses()
.create(
ResponseCreateParams.builder()
.model("gpt-6.2-astra")
.inputOfResponse(List.of(input))
.addTool(Tool.ImageGeneration.builder().build())
.build());
var image =
response.output().stream()
.flatMap(item -> item.imageGenerationCall().stream())
.findFirst()
.orElseThrow(() -> new IllegalStateException("No image generation call returned"));
Files.write(
Path.of("gift-basket.png"),
Base64.getDecoder()
.decode(
image.result().orElseThrow(() -> new IllegalStateException("No image returned"))));
require "base64"
require "openai"
require "pathname"
client = OpenAI::Client.new
base64_images = ["body-lotion.png", "soap.png"].map do |path|
Base64.strict_encode64(File.binread(path))
end
file_ids = [
client.files.create(file: Pathname("bath-bomb.png"), purpose: :vision).id,
client.files.create(file: Pathname("incense-kit.png"), purpose: :vision).id
]
prompt = <<~PROMPT
Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures.
PROMPT
response = client.responses.create(
model: "gpt-6.2-astra",
input: [
{
role: :user,
content: [
{
type: :input_text,
text: prompt
},
*base64_images.map do |image|
{
type: :input_image,
image_url: "data:image/png;base64,#{image}"
}
end,
*file_ids.map do |file_id|
{
type: :input_image,
file_id: file_id
}
end
]
}
],
tools: [
{
type: :image_generation,
model: "gpt-image-2.5-sunburst"
}
]
)
image_call = response.output.find do |item|
item.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
end
unless image_call.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
raise "No image generation call returned"
end
File.binwrite("gift-basket.png", Base64.strict_decode64(image_call.result))
Image API
Edit an image
import fs from "fs";
import OpenAI, { toFile } from "openai";
const client = new OpenAI();
const prompt = `
Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures.
`;
const imageFiles = [
"fixtures/bath-bomb.png",
"fixtures/body-lotion.png",
"fixtures/incense-kit.png",
"fixtures/soap.png",
];
const images = await Promise.all(
imageFiles.map(
async (file) =>
await toFile(fs.createReadStream(file), null, {
type: "image/png",
})
)
);
const response = await client.images.edit({
model: "gpt-image-2.5-sunburst",
image: images,
prompt,
});
// Save the image to a file
const image_base64 = response.data?.[0]?.b64_json;
if (!image_base64) throw new Error("Image generation returned no image.");
const image_bytes = Buffer.from(image_base64, "base64");
fs.writeFileSync("basket.png", image_bytes);
import base64
from openai import OpenAI
client = OpenAI()
prompt = """
Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures.
"""
result = client.images.edit(
model="gpt-image-2.5-sunburst",
image=[
open("body-lotion.png", "rb"),
open("bath-bomb.png", "rb"),
open("incense-kit.png", "rb"),
open("soap.png", "rb"),
],
prompt=prompt,
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Save the image to a file
with open("gift-basket.png", "wb") as f:
f.write(image_bytes)
package main
import (
"context"
"encoding/base64"
"io"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
files, closeFiles := openImages(
"bath-bomb.png",
"body-lotion.png",
"incense-kit.png",
"soap.png",
)
defer closeFiles()
response, err := client.Images.Edit(context.Background(), openai.ImageEditParams{
Model: openai.ImageModel("gpt-image-2.5-sunburst"),
Image: openai.ImageEditParamsImageUnion{OfFileArray: files},
Prompt: "Generate a photorealistic image of a gift basket on a white background " +
"labeled 'Relax & Unwind' with a ribbon and handwriting-like font, containing all the items in the reference pictures.",
})
if err != nil {
panic(err)
}
saveImage("basket.png", response.Data[0].B64JSON)
}
func openImages(names ...string) ([]io.Reader, func()) {
images := make([]io.Reader, 0, len(names))
files := make([]*os.File, 0, len(names))
for _, name := range names {
file, err := os.Open(name)
if err != nil {
closeFiles(files)
panic(err)
}
images = append(images, openai.File(file, name, "image/png"))
files = append(files, file)
}
return images, func() { closeFiles(files) }
}
func closeFiles(files []*os.File) {
for _, file := range files {
if err := file.Close(); err != nil {
panic(err)
}
}
}
func saveImage(filename, encoded string) {
image, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
panic(err)
}
if err := os.WriteFile(filename, image, 0o600); err != nil {
panic(err)
}
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.MultipartField;
import com.openai.models.images.ImageEditParams;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
Path lotion = Path.of(System.getenv("OPENAI_EXAMPLE_IMAGE_PATH"));
Path soap = Path.of(System.getenv("OPENAI_EXAMPLE_IMAGE_PATH_2"));
Path bathBomb = Path.of(System.getenv("OPENAI_EXAMPLE_IMAGE_PATH_3"));
Path incense = Path.of(System.getenv("OPENAI_EXAMPLE_IMAGE_PATH_4"));
try (InputStream lotionImage = Files.newInputStream(lotion);
InputStream bathBombImage = Files.newInputStream(bathBomb);
InputStream incenseImage = Files.newInputStream(incense);
InputStream soapImage = Files.newInputStream(soap)) {
var images =
client
.images()
.edit(
ImageEditParams.builder()
.model("gpt-image-2.5-sunburst")
.image(
MultipartField.<ImageEditParams.Image>builder()
.value(
ImageEditParams.Image.ofInputStreams(
List.of(lotionImage, bathBombImage, incenseImage, soapImage)))
.contentType("image/png")
.filename("gift-basket-reference.png")
.build())
.prompt(
"""
Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures.
""")
.build());
Files.write(
Path.of("gift-basket.png"),
Base64.getDecoder().decode(images.data().orElseThrow().get(0).b64Json().orElseThrow()));
}
require "base64"
require "openai"
require "pathname"
client = OpenAI::Client.new
images = %w[body-lotion.png bath-bomb.png incense-kit.png soap.png].map do |path|
OpenAI::FilePart.new(Pathname(path), content_type: "image/png")
end
result = client.images.edit(
image: images,
model: "gpt-image-2.5-sunburst",
prompt: <<~PROMPT
Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures.
PROMPT
)
generated_image = result.data&.first or raise "No image returned"
File.binwrite("gift-basket.png", Base64.strict_decode64(generated_image.b64_json))
curl -s -D >(grep -i x-request-id >&2) \
-o >(jq -r '.data[0].b64_json' | base64 --decode > gift-basket.png) \
-X POST "https://api.openai.com/v1/images/edits" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F "model=gpt-image-2.5-sunburst" \
-F "image[]=@body-lotion.png" \
-F "image[]=@bath-bomb.png" \
-F "image[]=@incense-kit.png" \
-F "image[]=@soap.png" \
-F 'prompt=Generate a photorealistic image of a gift basket on a white background labeled "Relax & Unwind" with a ribbon and handwriting-like font, containing all the items in the reference pictures'
openai images edit \
--model gpt-image-2.5-sunburst \
--image body-lotion.png \
--image bath-bomb.png \
--image incense-kit.png \
--image soap.png \
--prompt 'Generate a photorealistic image of a gift basket on a white background labeled "Relax & Unwind" with a ribbon and handwriting-like font, containing all the items in the reference pictures' \
--raw-output \
--transform 'data.0.b64_json' | base64 --decode > gift-basket.png
Guide an edit with markup
For markup-guided edits with supported GPT-Image-2.5 and GPT-Image-2.6 models, use the POST /v1/images/edits endpoint in the Image API. For regional edits with the Responses API, use a mask. Use an annotation layer to show where a change should happen and describe the change in prompt.
For example, download the kitchen photo and transparent overlay into the same directory. The overlay circles the vase to recolor.
Edit the circled vase with the Image API
import base64
from pathlib import Path
from openai import OpenAI
client = OpenAI()
overlay = Path("markup-vase-overlay-gpt-image-2-6.png")
with open("kitchen.webp", "rb") as image, overlay.open("rb") as markup:
result = client.images.edit(
model="gpt-image-2.6-flare",
image=("kitchen.webp", image, "image/webp"),
markup=(overlay.name, markup, "image/png"),
quality="medium",
prompt=(
"Recolor the striped vase inside the red circle to solid cobalt blue ceramic. "
"Keep its shape, the dried stems, and the rest of the kitchen unchanged. "
"Remove the red circle from the finished photo."
),
)
Path("vase.png").write_bytes(base64.b64decode(result.data[0].b64_json))
client := openai.NewClient()
image, err := os.Open("kitchen.webp")
if err != nil {
panic(err)
}
defer image.Close()
markup, err := os.Open("markup-vase-overlay-gpt-image-2-6.png")
if err != nil {
panic(err)
}
defer markup.Close()
response, err := client.Images.Edit(context.Background(), openai.ImageEditParams{
Model: openai.ImageModel("gpt-image-2.6-flare"),
Image: openai.ImageEditParamsImageUnion{OfFile: openai.File(image, "kitchen.webp", "image/webp")},
Markup: openai.File(markup, "markup-vase-overlay-gpt-image-2-6.png", "image/png"),
Quality: "medium",
Prompt: "Recolor the striped vase inside the red circle to solid cobalt blue ceramic. " +
"Keep its shape, the dried stems, and the rest of the kitchen unchanged. " +
"Remove the red circle from the finished photo.",
})
if err != nil {
panic(err)
}
result, err := base64.StdEncoding.DecodeString(response.Data[0].B64JSON)
if err != nil {
panic(err)
}
if err := os.WriteFile("vase.png", result, 0o600); err != nil {
panic(err)
}
require "openai"
require "pathname"
require "base64"
client = OpenAI::Client.new
result = client.images.edit(
model: "gpt-image-2.6-flare",
image: OpenAI::FilePart.new(Pathname("kitchen.webp"), content_type: "image/webp"),
markup: OpenAI::FilePart.new(Pathname("markup-vase-overlay-gpt-image-2-6.png"), content_type: "image/png"),
quality: :medium,
prompt: "Recolor the striped vase inside the red circle to solid cobalt blue ceramic. " \
"Keep its shape, the dried stems, and the rest of the kitchen unchanged. " \
"Remove the red circle from the finished photo."
)
image = result.data&.first or raise "No image returned"
encoded = image.b64_json or raise "No image data returned"
File.binwrite("vase.png", Base64.strict_decode64(encoded))
set -euo pipefail
curl --fail --silent --show-error https://api.openai.com/v1/images/edits \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F model=gpt-image-2.6-flare \
-F quality=medium \
-F 'image=@kitchen.webp;type=image/webp' \
-F 'markup=@markup-vase-overlay-gpt-image-2-6.png;type=image/png' \
-F 'prompt=Recolor the striped vase inside the red circle to solid cobalt blue ceramic. Keep its shape, the dried stems, and the rest of the kitchen unchanged. Remove the red circle from the finished photo.' |
jq -er '.data[0].b64_json' | base64 --decode > vase.png


- Keep the original image as the first input image.
- Create a separate transparent PNG containing your circles, sketches, or other annotations. After orientation, the annotation layer and the first image must have the same width and height. The PNG must contain transparent pixels; do not flatten it onto the original image.
- Send the annotation layer as
markup. Multipart requests accept a file upload. JSON requests accept an object containing exactly one ofimage_urlorfile_id;image_urlcan contain an HTTPS URL or a base64 data URL. JSON edit requests useimagesfor the input image array. - Describe the requested change and the details you want to preserve. Review the result before using it.
Markup applies to the first input image. Additional images serve as references. The first input image and the annotation layer can each contain at most 16,777,216 pixels. The annotation file also has a 50 MB size limit. Do not combine markup with mask.
Edit an image using a mask
You can provide a mask to indicate which part of the image should be edited.
When using a mask with GPT Image, additional instructions are sent to the model to help guide the editing process accordingly.
Masking with GPT Image is entirely prompt-based. The model uses the mask as guidance, but may not follow its exact shape with complete precision.
If you provide multiple input images, the mask will be applied to the first image.
Responses API
Edit an image with a mask
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
async function createFile(filePath: fs.PathLike) {
const result = await openai.files.create({
file: fs.createReadStream(filePath),
purpose: "vision",
});
return result.id;
}
const fileId = await createFile("fixtures/sunlit_lounge.png");
const maskId = await createFile("fixtures/mask.png");
const response = await openai.responses.create({
model: "gpt-6.2-astra",
input: [
{
role: "user",
content: [
{
type: "input_text",
text: "generate an image of the same sunlit indoor lounge area with a pool but the pool should contain a flamingo",
},
{
type: "input_image",
file_id: fileId,
detail: "auto",
},
],
},
],
tools: [
{
type: "image_generation",
model: "gpt-image-2.5-sunburst",
quality: "high",
input_image_mask: {
file_id: maskId,
},
},
],
});
const imageData = response.output
.filter((output) => output.type === "image_generation_call")
.map((output) => output.result);
if (imageData.length > 0) {
const imageBase64 = imageData[0];
if (!imageBase64) throw new Error("Image generation returned no image.");
fs.writeFileSync("lounge.png", Buffer.from(imageBase64, "base64"));
}
from openai import OpenAI
import base64
client = OpenAI()
def create_file(file_path):
with open(file_path, "rb") as file_content:
result = client.files.create(file=file_content, purpose="vision")
return result.id
fileId = create_file("sunlit_lounge.png")
maskId = create_file("mask.png")
response = client.responses.create(
model="gpt-6.2-astra",
input=[
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "generate an image of the same sunlit indoor lounge area with a pool but the pool should contain a flamingo",
},
{
"type": "input_image",
"file_id": fileId,
},
],
},
],
tools=[
{
"type": "image_generation",
"model": "gpt-image-2.5-sunburst",
"quality": "high",
"input_image_mask": {
"file_id": maskId,
},
},
],
)
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("lounge.png", "wb") as f:
f.write(base64.b64decode(image_base64))
package main
import (
"context"
"encoding/base64"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
imageID := uploadImage(client, "sunlit_lounge.png")
maskID := uploadImage(client, "mask.png")
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6.2-astra",
Input: responses.ResponseNewParamsInputUnion{OfInputItemList: responses.ResponseInputParam{
responses.ResponseInputItemParamOfMessage(
responses.ResponseInputMessageContentListParam{
responses.ResponseInputContentParamOfInputText("Generate an image of the same sunlit indoor lounge area with a pool, but the pool should contain a flamingo."),
{OfInputImage: &responses.ResponseInputImageParam{FileID: openai.String(imageID), Detail: responses.ResponseInputImageDetailAuto}},
},
responses.EasyInputMessageRoleUser,
),
}},
Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{
Model: "gpt-image-2.5-sunburst",
Quality: "high",
InputImageMask: responses.ToolImageGenerationInputImageMaskParam{FileID: openai.String(maskID)},
}}},
})
if err != nil {
panic(err)
}
saveFirstGeneratedImage(response, "lounge.png")
}
func uploadImage(client openai.Client, filename string) string {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
uploaded, err := client.Files.New(context.Background(), openai.FileNewParams{File: file, Purpose: openai.FilePurposeVision})
if err != nil {
panic(err)
}
return uploaded.ID
}
func saveFirstGeneratedImage(response *responses.Response, filename string) {
for _, output := range response.Output {
if output.Type != "image_generation_call" {
continue
}
image, err := base64.StdEncoding.DecodeString(output.AsImageGenerationCall().Result)
if err != nil {
panic(err)
}
if err := os.WriteFile(filename, image, 0o600); err != nil {
panic(err)
}
return
}
panic("response did not include an image generation call")
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.files.FileCreateParams;
import com.openai.models.files.FilePurpose;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseInputImage;
import com.openai.models.responses.ResponseInputItem;
import com.openai.models.responses.Tool;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
var image =
client
.files()
.create(
FileCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_FILE_PATH")))
.purpose(FilePurpose.VISION)
.build());
var mask =
client
.files()
.create(
FileCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_IMAGE_MASK_PATH")))
.purpose(FilePurpose.VISION)
.build());
var response =
client
.responses()
.create(
ResponseCreateParams.builder()
.model("gpt-6.2-astra")
.inputOfResponse(
List.of(
ResponseInputItem.ofMessage(
ResponseInputItem.Message.builder()
.role(ResponseInputItem.Message.Role.USER)
.addInputTextContent("Add a flamingo to the pool.")
.addContent(
ResponseInputImage.builder()
.detail(ResponseInputImage.Detail.AUTO)
.fileId(image.id())
.build())
.build())))
.addTool(
Tool.ImageGeneration.builder()
.inputImageMask(
Tool.ImageGeneration.InputImageMask.builder()
.fileId(mask.id())
.build())
.build())
.build());
String imageResult =
response.output().stream()
.flatMap(item -> item.imageGenerationCall().stream())
.flatMap(call -> call.result().stream())
.findFirst()
.orElseThrow(() -> new IllegalStateException("No generated image returned"));
Files.write(Path.of("lounge.png"), Base64.getDecoder().decode(imageResult));
require "base64"
require "openai"
require "pathname"
client = OpenAI::Client.new
image = client.files.create(file: Pathname("sunlit_lounge.png"), purpose: :vision)
mask = client.files.create(file: Pathname("mask.png"), purpose: :vision)
response = client.responses.create(
model: "gpt-6.2-astra",
input: [
{
role: :user,
content: [
{
type: :input_text,
text: "Add a flamingo to the pool."
},
{
type: :input_image,
file_id: image.id
}
]
}
],
tools: [
{
type: :image_generation,
model: "gpt-image-2.5-sunburst",
input_image_mask: { file_id: mask.id }
}
]
)
image_call = response.output.find do |item|
item.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
end
unless image_call.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)
raise "No image generation call returned"
end
File.binwrite("lounge.png", Base64.strict_decode64(image_call.result))
Image API
Edit an image with a mask
import fs from "fs";
import OpenAI, { toFile } from "openai";
const client = new OpenAI();
const rsp = await client.images.edit({
model: "gpt-image-2.5-sunburst",
image: await toFile(fs.createReadStream("fixtures/sunlit_lounge.png"), null, {
type: "image/png",
}),
mask: await toFile(fs.createReadStream("fixtures/mask.png"), null, {
type: "image/png",
}),
prompt: "A sunlit indoor lounge area with a pool containing a flamingo",
});
// Save the image to a file
const image_base64 = rsp.data?.[0]?.b64_json;
if (!image_base64) throw new Error("Image generation returned no image.");
const image_bytes = Buffer.from(image_base64, "base64");
fs.writeFileSync("lounge.png", image_bytes);
from openai import OpenAI
import base64
client = OpenAI()
result = client.images.edit(
model="gpt-image-2.5-sunburst",
image=open("sunlit_lounge.png", "rb"),
mask=open("mask.png", "rb"),
prompt="A sunlit indoor lounge area with a pool containing a flamingo",
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Save the image to a file
with open("composition.png", "wb") as f:
f.write(image_bytes)
package main
import (
"context"
"encoding/base64"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
image, err := os.Open("sunlit_lounge.png")
if err != nil {
panic(err)
}
defer image.Close()
mask, err := os.Open("mask.png")
if err != nil {
panic(err)
}
defer mask.Close()
response, err := client.Images.Edit(context.Background(), openai.ImageEditParams{
Model: openai.ImageModel("gpt-image-2.5-sunburst"),
Image: openai.ImageEditParamsImageUnion{OfFile: openai.File(image, "sunlit_lounge.png", "image/png")},
Mask: openai.File(mask, "mask.png", "image/png"),
Prompt: "A sunlit indoor lounge area with a pool containing a flamingo",
})
if err != nil {
panic(err)
}
result, err := base64.StdEncoding.DecodeString(response.Data[0].B64JSON)
if err != nil {
panic(err)
}
if err := os.WriteFile("lounge.png", result, 0o600); err != nil {
panic(err)
}
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.MultipartField;
import com.openai.models.images.ImageEditParams;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
Path imagePath = Path.of(System.getenv("OPENAI_EXAMPLE_FILE_PATH"));
Path maskPath = Path.of(System.getenv("OPENAI_EXAMPLE_IMAGE_MASK_PATH"));
try (InputStream image = Files.newInputStream(imagePath);
InputStream mask = Files.newInputStream(maskPath)) {
var images =
client
.images()
.edit(
ImageEditParams.builder()
.model("gpt-image-2.5-sunburst")
.image(
MultipartField.<ImageEditParams.Image>builder()
.value(ImageEditParams.Image.ofInputStream(image))
.contentType("image/png")
.filename(imagePath.getFileName().toString())
.build())
.prompt("A sunlit indoor lounge area with a pool containing a flamingo")
.mask(
MultipartField.<InputStream>builder()
.value(mask)
.contentType("image/png")
.filename(maskPath.getFileName().toString())
.build())
.build());
Files.write(
Path.of("lounge.png"),
Base64.getDecoder().decode(images.data().orElseThrow().get(0).b64Json().orElseThrow()));
}
require "openai"
require "pathname"
require "base64"
client = OpenAI::Client.new
image = OpenAI::FilePart.new(Pathname("sunlit_lounge.png"), content_type: "image/png")
mask = OpenAI::FilePart.new(Pathname("mask.png"), content_type: "image/png")
result = client.images.edit(
image: image,
mask: mask,
model: "gpt-image-2.5-sunburst",
prompt: "A sunlit indoor lounge area with a pool containing a flamingo"
)
generated_image = result.data&.first or raise "No image returned"
File.binwrite("lounge.png", Base64.strict_decode64(generated_image.b64_json))
curl -s -D >(grep -i x-request-id >&2) \
-o >(jq -r '.data[0].b64_json' | base64 --decode > lounge.png) \
-X POST "https://api.openai.com/v1/images/edits" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F "model=gpt-image-2.5-sunburst" \
-F "mask=@mask.png" \
-F "image[]=@sunlit_lounge.png" \
-F 'prompt=A sunlit indoor lounge area with a pool containing a flamingo'
openai images edit \
--model gpt-image-2.5-sunburst \
--image sunlit_lounge.png \
--mask mask.png \
--prompt "A sunlit indoor lounge area with a pool containing a flamingo" \
--raw-output \
--transform 'data.0.b64_json' | base64 --decode > out.png
| Image | Mask | Output |
|---|---|---|
![]() |
![]() |
![]() |
Prompt: a sunlit indoor lounge area with a pool containing a flamingo
Mask requirements
The image to edit and mask must be of the same format and size (less than 50MB in size).
The mask image must also contain an alpha channel. If you're using an image editing tool to create the mask, make sure to save the mask with an alpha channel.
You can modify a black and white image programmatically to add an alpha channel.
Add an alpha channel to a black and white mask
from PIL import Image
from io import BytesIO
# 1. Load your black & white mask as a grayscale image
mask = Image.open("mask.png").convert("L")
# 2. Convert it to RGBA so it has space for an alpha channel
mask_rgba = mask.convert("RGBA")
# 3. Then use the mask itself to fill that alpha channel
mask_rgba.putalpha(mask)
# 4. Convert the mask into bytes
buf = BytesIO()
mask_rgba.save(buf, format="PNG")
mask_bytes = buf.getvalue()
# 5. Save the resulting file
img_path_mask_alpha = "mask_alpha.png"
with open(img_path_mask_alpha, "wb") as f:
f.write(mask_bytes)
package main
import (
"image"
"image/color"
"image/png"
"os"
)
func main() {
file, err := os.Open("mask.png")
if err != nil {
panic(err)
}
defer file.Close()
mask, _, err := image.Decode(file)
if err != nil {
panic(err)
}
bounds := mask.Bounds()
withAlpha := image.NewNRGBA(bounds)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
gray := color.GrayModel.Convert(mask.At(x, y)).(color.Gray)
withAlpha.SetNRGBA(x, y, color.NRGBA{R: gray.Y, G: gray.Y, B: gray.Y, A: gray.Y})
}
}
output, err := os.Create("mask_alpha.png")
if err != nil {
panic(err)
}
if err := png.Encode(output, withAlpha); err != nil {
panic(err)
}
if err := output.Close(); err != nil {
panic(err)
}
}
require "chunky_png"
mask = ChunkyPNG::Image.from_file("mask.png")
mask.pixels.map! do |pixel|
gray = (0.299 * ChunkyPNG::Color.r(pixel) + 0.587 * ChunkyPNG::Color.g(pixel) + 0.114 * ChunkyPNG::Color.b(pixel)).round
ChunkyPNG::Color.rgba(gray, gray, gray, gray)
end
mask.save("mask_alpha.png", color_mode: ChunkyPNG::COLOR_TRUECOLOR_ALPHA)
Customize Image Output
You can configure the following output options:
- Size: Image dimensions (for example,
1024x1024,1024x1536) - Quality: Rendering quality (for example,
low,medium,high) - Format: File output format
- Compression: Compression level (0-100%) for JPEG and WebP formats
- Background: Transparent, opaque, or automatic
size, quality, and background support the auto option, where the model will automatically select the best option based on the prompt.
Size and quality options
Sunburst and Flare in GPT-Image-2.6 and GPT-Image-2.5 support xhigh and max quality settings and default to auto. Earlier GPT Image models support quality settings up to high.
| Setting | Options |
|---|---|
| Recommended sizes | 1024x1024 (square), 1536x1024 (landscape), 1024x1536 (portrait) |
| Quality | low, medium, high, xhigh, max, auto |
Sunburst and Flare also support custom dimensions as WIDTHxHEIGHT strings, such as 1536x864. Width and height must be multiples of 16, the aspect ratio must be between 1:3 and 3:1, and neither edge may exceed 3840 pixels. The total pixel count must be between 655,360 and 8,294,400 (4K). For GPT-Image-2.5, resolutions above 2560x1440 are experimental.
For transparent backgrounds, set background: "transparent" and use output_format: "png" or "webp".
Use quality: "low" for quick drafts. For final assets, compare higher quality settings to find the right balance of detail, latency, and cost.
Output format
The Image API returns base64-encoded image data.
The default format is png, but you can also request jpeg or webp.
If using jpeg or webp, you can also specify the output_compression parameter to control the compression level (0-100%). For example, output_compression=50 will compress the image by 50%.
Using jpeg is faster than png, so you should prioritize this format if
latency is a concern.
Generate SVG output
Set output_format: "svg" on POST /v1/images/generations. Use SVG for graphics that need editable paths, shapes, and colors, such as icons and illustrations.
Set n to 1. SVG output does not support streaming, partial images, or output_compression. The image edit endpoint does not support SVG output, and an SVG is not a supported raster input for image edits.
The API creates a reference image and uses it to produce the SVG. The quality setting controls that reference image. Decode data[0].b64_json as base64 and save the resulting UTF-8 XML bytes with a .svg extension. The returned document contains static vector content; it does not support scripts, external resources, or embedded raster images.
Flare and Sunburst generated these SVG logos from the same prompt for a Field & Flour bakery logo.
Generate animated GIF output
Set output_format: "gif" on POST /v1/images/generations or POST /v1/images/edits. Describe the subject, motion, and desired loop in your prompt. To guide the animation with still reference images, send them to the image edit endpoint.
Use these request fields for GIF output from either endpoint:
| Field | Values | Default |
|---|---|---|
frame_count |
2–16 frames | 9 |
frame_duration_ms |
20–2000 milliseconds per frame, rounded to 10 ms | 200 |
size |
auto, or WIDTHxHEIGHT with positive dimensions that are multiples of 16 |
auto |
size describes each finished frame. The frame count, frame dimensions, and number of animations must fit the model's size limits and the total request pixel budget. If the request exceeds those limits, reduce frame_count, the frame dimensions, or n. Use auto to let the API choose a frame size within the model's limits.
The response contains the finished animation. Decode each result's b64_json value as base64 and save the bytes with a .gif extension. GIF output does not support streaming, partial images, or custom output_compression. Use frame_count and frame_duration_ms only with GIF output.
Sunburst generated this lantern animation with frame_count=9 and frame_duration_ms=200. The monkey returns to its starting pose after 1.8 seconds.

Limitations
GPT Image models are powerful and versatile image generation models, but they still have some limitations to be aware of:
- Latency: Complex prompts may take up to 2 minutes to process.
- Text Rendering: Although significantly improved, the model can still struggle with precise text placement and clarity.
- Consistency: While capable of producing consistent imagery, the model may occasionally struggle to maintain visual consistency for recurring characters or brand elements across multiple generations.
- Composition Control: Despite improved instruction following, the model may have difficulty placing elements precisely in structured or layout-sensitive compositions.
Content Moderation
All prompts and generated images are filtered in accordance with our content policy.
For image generation using GPT Image models, you can control moderation strictness with the moderation parameter. This parameter supports two values:
auto(default): Standard filtering that seeks to limit creating certain categories of potentially age-inappropriate content.low: Less restrictive filtering.
Handling blocked requests and other errors
Handle image generation failures the same way you handle other API errors: check the HTTP status or SDK exception type, log the request ID, and refer to the error codes guide for authentication, quota, rate-limit, and server failures. Retry transient rate-limit and server failures with backoff. Don't automatically retry quota errors or image generation user errors that require changing the request.
Some image generation failures are user-correctable and may return error.type = "image_generation_user_error". Don't automatically retry these errors without modifying the prompt or input images. For programmatic handling, use error.code as the stable discriminator.
When error.code = "moderation_blocked", the error may also include an optional error.moderation_details object:
{
"error": {
"type": "image_generation_user_error",
"code": "moderation_blocked",
"moderation_details": {
"moderation_stage": "input",
"categories": ["harassment"]
}
}
}
The moderation_details object provides coarse debugging context without exposing internal classifier labels or scores.
moderation_stage can be:
input: The block came from the prompt or request inputs.output: The block came from a generated image or downstream output moderation stage.unknown: A rare fallback when provenance is hard to determine.
categories contains coarse public labels. For example, you might see values like harassment, self-harm, sexual, or violence.
For most apps, keep the primary end-user message generic. Use moderation_details for developer logs, support workflows, analytics, and light remediation hints.
Handle moderation-blocked image generation errors
import OpenAI from "openai";
const openai = new OpenAI();
try {
// The same error handling pattern applies to image generation requests,
// image edits, and Responses API tool calls that generate images.
await openai.images.generate({
model: "gpt-image-2.5-sunburst",
prompt: "Create a poster humiliating my coworker with insulting captions",
});
} catch (error) {
if (
!(error instanceof OpenAI.APIError) ||
error.code !== "moderation_blocked"
) {
throw error;
}
const moderationDetails =
error.error && "moderation_details" in error.error
? (error.error.moderation_details as {
categories?: string[];
moderation_stage?: string;
})
: undefined;
const categories = moderationDetails?.categories ?? [];
const stage = moderationDetails?.moderation_stage;
let hint =
"This request could not be completed because it did not meet safety requirements.";
if (categories.includes("harassment")) {
hint =
"Try removing abusive or targeting language and focus on neutral visual details instead.";
} else if (stage === "input") {
hint =
"Try revising the prompt or input images and submit the request again.";
} else if (stage === "output") {
hint =
"The generated result was blocked by a safety check. Try changing the prompt and generating again.";
}
console.error("Image generation blocked", {
request_id: error?.requestID,
code: error?.code,
moderation_details: moderationDetails,
});
console.log(hint);
}
import openai
from openai import OpenAI
client = OpenAI()
try:
# The same error handling pattern applies to image generation requests,
# image edits, and Responses API tool calls that generate images.
client.images.generate(
model="gpt-image-2.5-sunburst",
prompt="Create a poster humiliating my coworker with insulting captions",
)
except openai.BadRequestError as error:
if error.code != "moderation_blocked":
raise
error_body = error.body if isinstance(error.body, dict) else {}
moderation_details = error_body.get("moderation_details") or {}
categories = moderation_details.get("categories") or []
stage = moderation_details.get("moderation_stage")
hint = "This request could not be completed because it did not meet safety requirements."
if "harassment" in categories:
hint = "Try removing abusive or targeting language and focus on neutral visual details instead."
elif stage == "input":
hint = "Try revising the prompt or input images and submit the request again."
elif stage == "output":
hint = "The generated result was blocked by a safety check. Try changing the prompt and generating again."
print(
"Image generation blocked",
{
"request_id": error.request_id,
"code": error.code,
"moderation_details": moderation_details,
},
)
print(hint)
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
_, err := client.Images.Generate(context.Background(), openai.ImageGenerateParams{
Model: openai.ImageModel("gpt-image-2.5-sunburst"),
Prompt: "Create a poster humiliating my coworker with insulting captions",
})
if err == nil {
return
}
var apiError *openai.Error
if !errors.As(err, &apiError) || apiError.Code != "moderation_blocked" {
panic(err)
}
var body struct {
ModerationDetails struct {
Categories []string `json:"categories"`
ModerationStage string `json:"moderation_stage"`
} `json:"moderation_details"`
}
if err := json.Unmarshal([]byte(apiError.RawJSON()), &body); err != nil {
panic(err)
}
hint := "This request could not be completed because it did not meet safety requirements."
if slices.Contains(body.ModerationDetails.Categories, "harassment") {
hint = "Try removing abusive or targeting language and focus on neutral visual details instead."
} else if body.ModerationDetails.ModerationStage == "input" {
hint = "Try revising the prompt or input images and submit the request again."
} else if body.ModerationDetails.ModerationStage == "output" {
hint = "The generated result was blocked by a safety check. Try changing the prompt and generating again."
}
fmt.Printf("Image generation blocked (%s): %s\n", apiError.Code, hint)
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.errors.BadRequestException;
import com.openai.models.images.ImageGenerateParams;
import java.util.List;
import java.util.Map;
try {
var images =
client
.images()
.generate(
ImageGenerateParams.builder()
.model("gpt-image-2.5-sunburst")
.prompt("Create a poster humiliating my coworker with insulting captions")
.build());
System.out.println(images.data().orElseThrow().get(0).b64Json().orElseThrow());
} catch (BadRequestException error) {
if (!error.code().orElse("").equals("moderation_blocked")) {
throw error;
}
Map<?, ?> body = error.body().convert(Map.class);
Object detailsValue = body.get("moderation_details");
Map<?, ?> details = detailsValue instanceof Map<?, ?> values ? values : Map.of();
Object categories = details.get("categories");
Object stage = details.get("moderation_stage");
String hint = "This request did not meet safety requirements.";
if (categories instanceof List<?> values && values.contains("harassment")) {
hint = "Remove abusive or targeting language and focus on neutral visual details.";
} else if ("input".equals(stage)) {
hint = "Revise the prompt or input images, then submit the request again.";
} else if ("output".equals(stage)) {
hint = "Change the prompt and generate again; the generated result was blocked.";
}
System.err.println("Image generation blocked (" + error.code().orElseThrow() + "): " + hint);
}
require "openai"
client = OpenAI::Client.new
begin
client.images.generate(
model: "gpt-image-2.5-sunburst",
prompt: "Create a poster humiliating my coworker with insulting captions"
)
rescue OpenAI::Errors::BadRequestError => error
raise unless error.code == "moderation_blocked"
body = Hash.try_convert(error.body) || {}
body = Hash.try_convert(body[:error] || body["error"]) || body
moderation_details = body[:moderation_details] || body["moderation_details"] || {}
categories = moderation_details[:categories] || moderation_details["categories"] || []
stage = moderation_details[:moderation_stage] || moderation_details["moderation_stage"]
hint = "This request did not meet safety requirements."
if categories.include?("harassment")
hint = "Remove abusive or targeting language and focus on neutral visual details."
elsif stage == "input"
hint = "Revise the prompt or input images, then submit the request again."
elsif stage == "output"
hint = "Change the prompt and generate again; the generated result was blocked."
end
warn("Image generation blocked (#{error.code}): #{hint}")
end
Supported models
When using image generation in the Responses API, gpt-5 and newer models should support the image generation tool. Check the model detail page for your model to confirm if your desired model can use the image generation tool.
Cost and latency
GPT-Image-2.6 costs
Image output costs $30 per million billable tokens. Use the calculator to estimate the output cost for one image.
See image generation pricing for text and image rates per million tokens.
Markup-guided edits use the same token rates as other edits, with no extra fee. For SVG output, you pay the image model’s rate for the reference image and the authoring model’s rate for the tokens it uses to produce the SVG. The total depends on how many tokens each model uses.
GPT-Image-2.5 costs
Responses API requests include the mainline model's token usage in addition to image generation costs.
Both GPT-Image-2.5 models use the same token rates: $8 per million image input tokens, $2 per million cached image input tokens, $30 per million image output tokens, $5 per million text input tokens, and $1.25 per million cached text input tokens. See pricing.
Use the response's usage to measure token consumption for your prompts, sizes, and quality settings. Equal token rates don't mean equal cost per image: token consumption can differ by model and quality setting. For older-model pricing examples, see Earlier GPT Image models.
Cached input pricing
For GPT-Image-2 and GPT-Image-2.5, cached input pricing applies only to the image generation tool in the Responses API. It doesn't apply to direct Images API requests, including /v1/images/edits.
Cached image generation inputs are reflected in billing, but their cached token counts aren't included in the Responses API output. The response's usage can't verify these cache hits.
Calculating costs
Select a model, quality, and size to estimate output tokens and image output cost.
For the Sunburst and Flare models in both GPT-Image-2.6 and GPT-Image-2.5, the quality options are low, medium, high, xhigh, and max.
For gpt-image-2, the options are low, medium, and high.
The models can use different token counts for the same quality setting and share the same price per image output token.
Use explicit quality and size values for this estimate; auto depends on the generated image.
<GptImageTokenCalculator client:load outputPricePerMillion={Number( pricing.latest.subsections .find((section) => section.price_type === "Image tokens") ?.items.find((item) => item.name === "gpt-image-2")?.values.main.output )} />
Partial images cost
If you want to stream image generation using the partial_images parameter, each partial image will incur an additional 100 image output tokens.
Earlier GPT Image models
The details below apply to earlier models, not Sunburst or Flare. For new integrations, use GPT-Image-2.6 Sunburst or Flare.
GPT-Image-2 settings and input fidelity
gpt-image-2 accepts any resolution in the size parameter when it satisfies the constraints below. Square images are typically fastest to generate.
| Popular sizes |
|
| Size constraints |
|
| Quality options |
|
Image input fidelity
The input_fidelity parameter controls how strongly a model preserves details from input images during edits and reference-image workflows. For gpt-image-2, omit this parameter; the API doesn't allow changing it because the model processes every image input at high fidelity automatically.
Because gpt-image-2 always processes image inputs at high fidelity, image
input tokens can be higher for edit requests that include reference images. To
understand the cost implications, refer to the vision
costs
section.
Older-model pricing examples
Models prior to gpt-image-2
GPT Image models prior to gpt-image-2 generate images by first producing specialized image tokens. Both latency and eventual cost are proportional to the number of tokens required to render an image—larger image sizes and higher quality settings result in more tokens.
The number of tokens generated depends on image dimensions and quality:
| Quality | Square (1024×1024) | Portrait (1024×1536) | Landscape (1536×1024) |
|---|---|---|---|
| Low | 272 tokens | 408 tokens | 400 tokens |
| Medium | 1056 tokens | 1584 tokens | 1568 tokens |
| High | 4160 tokens | 6240 tokens | 6208 tokens |
Note that you will also need to account for input tokens: text tokens for the prompt and image tokens for the input images if editing images.
Because gpt-image-2 always processes image inputs at high fidelity, edit requests that include reference images can use more input tokens.
Refer to the pricing page for current text and image token prices, and use the Calculating costs section to estimate image output costs.
The final cost is the sum of:
- input text tokens
- input image tokens if using the edits endpoint
- image output tokens
Historical output costs
gpt-image-2 supports thousands of valid resolutions; the table below lists the
same sizes used for previous GPT Image models for comparison. For GPT Image 1.5,
GPT Image 1, and GPT Image 1 mini, the legacy per-image output pricing table is
also listed below. You should still account for text and image input tokens when
estimating the total cost of a request.
gpt-image-1 was shut down on October 23, 2026. Its prices below are retained
for historical comparisons. See the deprecation notice
for replacement models.
A larger non-square resolution can sometimes produce fewer output tokens than a smaller or square resolution at the same quality setting.
| Model | Quality | 1024 × 1024 | 1024 × 1536 | 1536 × 1024 |
|---|---|---|---|---|
|
GPT-Image-2
Additional sizes available
|
guides/image-generation.md +82 −51
289const openai = new OpenAI();289const openai = new OpenAI();
290 290
291const response = await openai.responses.create({291const response = await openai.responses.create({
292292 model: "gpt-6-astra", model: "gpt-6.2-astra",
293 input:293 input:
294 "Generate an image of gray tabby cat hugging an otter with an orange scarf",294 "Generate an image of gray tabby cat hugging an otter with an orange scarf",
295 tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],295 tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],
315client = OpenAI()315client = OpenAI()
316 316
317response = client.responses.create(317response = client.responses.create(
318318 model="gpt-6-astra", model="gpt-6.2-astra",
319 input="Generate an image of gray tabby cat hugging an otter with an orange scarf",319 input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
320 tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],320 tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
321)321)
348func main() {348func main() {
349 client := openai.NewClient()349 client := openai.NewClient()
350 response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{350 response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
351351 Model: "gpt-6-astra", Model: "gpt-6.2-astra",
352 Input: responses.ResponseNewParamsInputUnion{352 Input: responses.ResponseNewParamsInputUnion{
353 OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),353 OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),
354 },354 },
389 389
390ResponseCreateParams params =390ResponseCreateParams params =
391 ResponseCreateParams.builder()391 ResponseCreateParams.builder()
392392 .model("gpt-6-astra") .model("gpt-6.2-astra")
393 .input("Generate an image of a gray tabby cat hugging an otter with an orange scarf.")393 .input("Generate an image of a gray tabby cat hugging an otter with an orange scarf.")
394 .addTool(Tool.ImageGeneration.builder().build())394 .addTool(Tool.ImageGeneration.builder().build())
395 .build();395 .build();
411string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;411string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
412ResponsesClient client = new(key);412ResponsesClient client = new(key);
413 413
414414CreateResponseOptions options = new() { Model = "gpt-6-astra" };CreateResponseOptions options = new() { Model = "gpt-6.2-astra" };
415options.InputItems.Add(415options.InputItems.Add(
416 ResponseItem.CreateUserMessageItem(416 ResponseItem.CreateUserMessageItem(
417 "Generate an image of a gray tabby cat hugging an otter with an orange scarf."417 "Generate an image of a gray tabby cat hugging an otter with an orange scarf."
433 433
434client = OpenAI::Client.new434client = OpenAI::Client.new
435response = client.responses.create(435response = client.responses.create(
436436 model: "gpt-6-astra", model: "gpt-6.2-astra",
437 input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",437 input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",
438 tools: [438 tools: [
439 {439 {
470const openai = new OpenAI();470const openai = new OpenAI();
471 471
472const response = await openai.responses.create({472const response = await openai.responses.create({
473473 model: "gpt-6-astra", model: "gpt-6.2-astra",
474 input:474 input:
475 "Generate an image of gray tabby cat hugging an otter with an orange scarf",475 "Generate an image of gray tabby cat hugging an otter with an orange scarf",
476 tools: [476 tools: [
498client = OpenAI()498client = OpenAI()
499 499
500response = client.responses.create(500response = client.responses.create(
501501 model="gpt-6-astra", model="gpt-6.2-astra",
502 input="Generate an image of gray tabby cat hugging an otter with an orange scarf",502 input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
503 tools=[503 tools=[
504 {"type": "image_generation", "model": "gpt-image-2.5-sunburst", "action": "generate"}504 {"type": "image_generation", "model": "gpt-image-2.5-sunburst", "action": "generate"}
533func main() {533func main() {
534 client := openai.NewClient()534 client := openai.NewClient()
535 response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{535 response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
536536 Model: "gpt-6-astra", Model: "gpt-6.2-astra",
537 Input: responses.ResponseNewParamsInputUnion{537 Input: responses.ResponseNewParamsInputUnion{
538 OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),538 OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),
539 },539 },
571 571
572ResponseCreateParams params =572ResponseCreateParams params =
573 ResponseCreateParams.builder()573 ResponseCreateParams.builder()
574574 .model("gpt-6-astra") .model("gpt-6.2-astra")
575 .input("Generate an image of a gray tabby cat hugging an otter with an orange scarf.")575 .input("Generate an image of a gray tabby cat hugging an otter with an orange scarf.")
576 .addTool(576 .addTool(
577 Tool.ImageGeneration.builder().action(Tool.ImageGeneration.Action.GENERATE).build())577 Tool.ImageGeneration.builder().action(Tool.ImageGeneration.Action.GENERATE).build())
595string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;595string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
596ResponsesClient client = new(key);596ResponsesClient client = new(key);
597 597
598598CreateResponseOptions options = new() { Model = "gpt-6-astra" };CreateResponseOptions options = new() { Model = "gpt-6.2-astra" };
599options.InputItems.Add(599options.InputItems.Add(
600 ResponseItem.CreateUserMessageItem(600 ResponseItem.CreateUserMessageItem(
601 "Generate an image of a gray tabby cat hugging an otter with an orange scarf."601 "Generate an image of a gray tabby cat hugging an otter with an orange scarf."
622 622
623client = OpenAI::Client.new623client = OpenAI::Client.new
624response = client.responses.create(624response = client.responses.create(
625625 model: "gpt-6-astra", model: "gpt-6.2-astra",
626 input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",626 input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",
627 tools: [627 tools: [
628 {628 {
660const openai = new OpenAI();660const openai = new OpenAI();
661 661
662const response = await openai.responses.create({662const response = await openai.responses.create({
663663 model: "gpt-6-astra", model: "gpt-6.2-astra",
664 input:664 input:
665 "Generate an image of gray tabby cat hugging an otter with an orange scarf",665 "Generate an image of gray tabby cat hugging an otter with an orange scarf",
666 tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],666 tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],
680// Follow up680// Follow up
681 681
682const response_fwup = await openai.responses.create({682const response_fwup = await openai.responses.create({
683683 model: "gpt-6-astra", model: "gpt-6.2-astra",
684 previous_response_id: response.id,684 previous_response_id: response.id,
685 input: "Now make it look realistic",685 input: "Now make it look realistic",
686 tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],686 tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],
708client = OpenAI()708client = OpenAI()
709 709
710response = client.responses.create(710response = client.responses.create(
711711 model="gpt-6-astra", model="gpt-6.2-astra",
712 input="Generate an image of gray tabby cat hugging an otter with an orange scarf",712 input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
713 tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],713 tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
714)714)
729# Follow up729# Follow up
730 730
731response_fwup = client.responses.create(731response_fwup = client.responses.create(
732732 model="gpt-6-astra", model="gpt-6.2-astra",
733 previous_response_id=response.id,733 previous_response_id=response.id,
734 input="Now make it look realistic",734 input="Now make it look realistic",
735 tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],735 tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
762func main() {762func main() {
763 client := openai.NewClient()763 client := openai.NewClient()
764 first, err := client.Responses.New(context.Background(), responses.ResponseNewParams{764 first, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
765765 Model: "gpt-6-astra", Model: "gpt-6.2-astra",
766 Input: responses.ResponseNewParamsInputUnion{766 Input: responses.ResponseNewParamsInputUnion{
767 OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),767 OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),
768 },768 },
774 saveFirstGeneratedImage(first, "cat_and_otter.png")774 saveFirstGeneratedImage(first, "cat_and_otter.png")
775 775
776 followUp, err := client.Responses.New(context.Background(), responses.ResponseNewParams{776 followUp, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
777777 Model: "gpt-6-astra", Model: "gpt-6.2-astra",
778 PreviousResponseID: openai.String(first.ID),778 PreviousResponseID: openai.String(first.ID),
779 Input: responses.ResponseNewParamsInputUnion{779 Input: responses.ResponseNewParamsInputUnion{
780 OfString: openai.String("Now make it look realistic"),780 OfString: openai.String("Now make it look realistic"),
819 .responses()819 .responses()
820 .create(820 .create(
821 ResponseCreateParams.builder()821 ResponseCreateParams.builder()
822822 .model("gpt-6-astra") .model("gpt-6.2-astra")
823 .input(823 .input(
824 "Generate an image of a gray tabby cat hugging an otter with an orange scarf.")824 "Generate an image of a gray tabby cat hugging an otter with an orange scarf.")
825 .addTool(Tool.ImageGeneration.builder().build())825 .addTool(Tool.ImageGeneration.builder().build())
842 .responses()842 .responses()
843 .create(843 .create(
844 ResponseCreateParams.builder()844 ResponseCreateParams.builder()
845845 .model("gpt-6-astra") .model("gpt-6.2-astra")
846 .input("Now make it look realistic.")846 .input("Now make it look realistic.")
847 .previousResponseId(first.id())847 .previousResponseId(first.id())
848 .addTool(Tool.ImageGeneration.builder().build())848 .addTool(Tool.ImageGeneration.builder().build())
869string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;869string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
870ResponsesClient client = new(key);870ResponsesClient client = new(key);
871 871
872872CreateResponseOptions options = new() { Model = "gpt-6-astra" };CreateResponseOptions options = new() { Model = "gpt-6.2-astra" };
873options.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));873options.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));
874options.InputItems.Add(874options.InputItems.Add(
875 ResponseItem.CreateUserMessageItem(875 ResponseItem.CreateUserMessageItem(
885 885
886CreateResponseOptions followUp = new()886CreateResponseOptions followUp = new()
887{887{
888888 Model = "gpt-6-astra", Model = "gpt-6.2-astra",
889 PreviousResponseId = first.Id,889 PreviousResponseId = first.Id,
890};890};
891followUp.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));891followUp.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));
907 907
908client = OpenAI::Client.new908client = OpenAI::Client.new
909first = client.responses.create(909first = client.responses.create(
910910 model: "gpt-6-astra", model: "gpt-6.2-astra",
911 input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",911 input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",
912 tools: [912 tools: [
913 {913 {
928File.binwrite("cat_and_otter.png", Base64.strict_decode64(encoded_image))928File.binwrite("cat_and_otter.png", Base64.strict_decode64(encoded_image))
929 929
930follow_up = client.responses.create(930follow_up = client.responses.create(
931931 model: "gpt-6-astra", model: "gpt-6.2-astra",
932 input: "Now make it look realistic.",932 input: "Now make it look realistic.",
933 previous_response_id: first.id,933 previous_response_id: first.id,
934 tools: [934 tools: [
964const openai = new OpenAI();964const openai = new OpenAI();
965 965
966const response = await openai.responses.create({966const response = await openai.responses.create({
967967 model: "gpt-6-astra", model: "gpt-6.2-astra",
968 input:968 input:
969 "Generate an image of gray tabby cat hugging an otter with an orange scarf",969 "Generate an image of gray tabby cat hugging an otter with an orange scarf",
970 tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],970 tools: [{ type: "image_generation", model: "gpt-image-2.5-sunburst" }],
986// Follow up986// Follow up
987 987
988const response_fwup = await openai.responses.create({988const response_fwup = await openai.responses.create({
989989 model: "gpt-6-astra", model: "gpt-6.2-astra",
990 input: [990 input: [
991 {991 {
992 role: "user",992 role: "user",
1020import base641020import base64
1021 1021
1022response = openai.responses.create(1022response = openai.responses.create(
10231023 model="gpt-6-astra", model="gpt-6.2-astra",
1024 input="Generate an image of gray tabby cat hugging an otter with an orange scarf",1024 input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
1025 tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],1025 tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
1026)1026)
1041# Follow up1041# Follow up
1042 1042
1043response_fwup = openai.responses.create(1043response_fwup = openai.responses.create(
10441044 model="gpt-6-astra", model="gpt-6.2-astra",
1045 input=[1045 input=[
1046 {1046 {
1047 "role": "user",1047 "role": "user",
1083func main() {1083func main() {
1084 client := openai.NewClient()1084 client := openai.NewClient()
1085 first, err := client.Responses.New(context.Background(), responses.ResponseNewParams{1085 first, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
10861086 Model: "gpt-6-astra", Model: "gpt-6.2-astra",
1087 Input: responses.ResponseNewParamsInputUnion{1087 Input: responses.ResponseNewParamsInputUnion{
1088 OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),1088 OfString: openai.String("Generate an image of gray tabby cat hugging an otter with an orange scarf"),
1089 },1089 },
1101 ))1101 ))
1102 1102
1103 followUp, err := client.Responses.New(context.Background(), responses.ResponseNewParams{1103 followUp, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
11041104 Model: "gpt-6-astra", Model: "gpt-6.2-astra",
1105 Input: responses.ResponseNewParamsInputUnion{OfInputItemList: input},1105 Input: responses.ResponseNewParamsInputUnion{OfInputItemList: input},
1106 Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{Model: "gpt-image-2.5-sunburst"}}},1106 Tools: []responses.ToolUnionParam{{OfImageGeneration: &responses.ToolImageGenerationParam{Model: "gpt-image-2.5-sunburst"}}},
1107 })1107 })
1161 .responses()1161 .responses()
1162 .create(1162 .create(
1163 ResponseCreateParams.builder()1163 ResponseCreateParams.builder()
11641164 .model("gpt-6-astra") .model("gpt-6.2-astra")
1165 .input(1165 .input(
1166 "Generate an image of a gray tabby cat hugging an otter with an orange scarf.")1166 "Generate an image of a gray tabby cat hugging an otter with an orange scarf.")
1167 .addTool(Tool.ImageGeneration.builder().build())1167 .addTool(Tool.ImageGeneration.builder().build())
1184 .responses()1184 .responses()
1185 .create(1185 .create(
1186 ResponseCreateParams.builder()1186 ResponseCreateParams.builder()
11871187 .model("gpt-6-astra") .model("gpt-6.2-astra")
1188 .inputOfResponse(1188 .inputOfResponse(
1189 List.of(1189 List.of(
1190 ResponseInputItem.ofMessage(1190 ResponseInputItem.ofMessage(
1219string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;1219string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
1220ResponsesClient client = new(key);1220ResponsesClient client = new(key);
1221 1221
12221222CreateResponseOptions options = new() { Model = "gpt-6-astra" };CreateResponseOptions options = new() { Model = "gpt-6.2-astra" };
1223options.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));1223options.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));
1224options.InputItems.Add(1224options.InputItems.Add(
1225 ResponseItem.CreateUserMessageItem(1225 ResponseItem.CreateUserMessageItem(
1233 .First();1233 .First();
1234await File.WriteAllBytesAsync("cat_and_otter.png", initialImage.ImageResultBytes.ToArray());1234await File.WriteAllBytesAsync("cat_and_otter.png", initialImage.ImageResultBytes.ToArray());
1235 1235
12361236CreateResponseOptions followUp = new() { Model = "gpt-6-astra" };CreateResponseOptions followUp = new() { Model = "gpt-6.2-astra" };
1237followUp.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));1237followUp.Tools.Add(ResponseTool.CreateImageGenerationTool(model: "gpt-image-2.5-sunburst"));
1238followUp.InputItems.Add(ResponseItem.CreateUserMessageItem("Now make it look realistic."));1238followUp.InputItems.Add(ResponseItem.CreateUserMessageItem("Now make it look realistic."));
1239followUp.InputItems.Add(ResponseItem.CreateReferenceItem(initialImage.Id));1239followUp.InputItems.Add(ResponseItem.CreateReferenceItem(initialImage.Id));
1254 1254
1255client = OpenAI::Client.new1255client = OpenAI::Client.new
1256first = client.responses.create(1256first = client.responses.create(
12571257 model: "gpt-6-astra", model: "gpt-6.2-astra",
1258 input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",1258 input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.",
1259 tools: [1259 tools: [
1260 {1260 {
1275File.binwrite("cat_and_otter.png", Base64.strict_decode64(encoded_image))1275File.binwrite("cat_and_otter.png", Base64.strict_decode64(encoded_image))
1276 1276
1277follow_up = client.responses.create(1277follow_up = client.responses.create(
12781278 model: "gpt-6-astra", model: "gpt-6.2-astra",
1279 input: [1279 input: [
1280 {1280 {
1281 role: :user,1281 role: :user,
1379}1379}
1380 1380
1381const stream = await openai.responses.create({1381const stream = await openai.responses.create({
13821382 model: "gpt-6-astra", model: "gpt-6.2-astra",
1383 input:1383 input:
1384 "Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",1384 "Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
1385 stream: true,1385 stream: true,
1418 1418
1419 1419
1420stream = client.responses.create(1420stream = client.responses.create(
14211421 model="gpt-6-astra", model="gpt-6.2-astra",
1422 input="Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",1422 input="Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
1423 stream=True,1423 stream=True,
1424 tools=[1424 tools=[
1457func main() {1457func main() {
1458 client := openai.NewClient()1458 client := openai.NewClient()
1459 stream := client.Responses.NewStreaming(context.Background(), responses.ResponseNewParams{1459 stream := client.Responses.NewStreaming(context.Background(), responses.ResponseNewParams{
14601460 Model: "gpt-6-astra", Model: "gpt-6.2-astra",
1461 Input: responses.ResponseNewParamsInputUnion{1461 Input: responses.ResponseNewParamsInputUnion{
1462 OfString: openai.String("Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape"),1462 OfString: openai.String("Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape"),
1463 },1463 },
1507 1507
1508ResponseCreateParams params =1508ResponseCreateParams params =
1509 ResponseCreateParams.builder()1509 ResponseCreateParams.builder()
15101510 .model("gpt-6-astra") .model("gpt-6.2-astra")
1511 .input("Generate an image of a river made of white owl feathers.")1511 .input("Generate an image of a river made of white owl feathers.")
1512 .addTool(Tool.ImageGeneration.builder().partialImages(2).build())1512 .addTool(Tool.ImageGeneration.builder().partialImages(2).build())
1513 .build();1513 .build();
1547 1547
1548client = OpenAI::Client.new1548client = OpenAI::Client.new
1549stream = client.responses.stream(1549stream = client.responses.stream(
15501550 model: "gpt-6-astra", model: "gpt-6.2-astra",
1551 input: "Generate an image of a river made of white owl feathers.",1551 input: "Generate an image of a river made of white owl feathers.",
1552 tools: [1552 tools: [
1553 {1553 {
1984const fileId2 = await createFile("fixtures/incense-kit.png");1984const fileId2 = await createFile("fixtures/incense-kit.png");
1985 1985
1986const response = await openai.responses.create({1986const response = await openai.responses.create({
19871987 model: "gpt-6-astra", model: "gpt-6.2-astra",
1988 input: [1988 input: [
1989 {1989 {
1990 role: "user",1990 role: "user",
2057file_id2 = create_file("incense-kit.png")2057file_id2 = create_file("incense-kit.png")
2058 2058
2059response = client.responses.create(2059response = client.responses.create(
20602060 model="gpt-6-astra", model="gpt-6.2-astra",
2061 input=[2061 input=[
2062 {2062 {
2063 "role": "user",2063 "role": "user",
2117 incenseKitID := uploadImage(client, "incense-kit.png")2117 incenseKitID := uploadImage(client, "incense-kit.png")
2118 2118
2119 response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{2119 response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
21202120 Model: "gpt-6-astra", Model: "gpt-6.2-astra",
2121 Input: responses.ResponseNewParamsInputUnion{OfInputItemList: responses.ResponseInputParam{2121 Input: responses.ResponseNewParamsInputUnion{OfInputItemList: responses.ResponseInputParam{
2122 responses.ResponseInputItemParamOfMessage(2122 responses.ResponseInputItemParamOfMessage(
2123 responses.ResponseInputMessageContentListParam{2123 responses.ResponseInputMessageContentListParam{
2244 .responses()2244 .responses()
2245 .create(2245 .create(
2246 ResponseCreateParams.builder()2246 ResponseCreateParams.builder()
22472247 .model("gpt-6-astra") .model("gpt-6.2-astra")
2248 .inputOfResponse(List.of(input))2248 .inputOfResponse(List.of(input))
2249 .addTool(Tool.ImageGeneration.builder().build())2249 .addTool(Tool.ImageGeneration.builder().build())
2250 .build());2250 .build());
2279 containing all the items in the reference pictures.2279 containing all the items in the reference pictures.
2280PROMPT2280PROMPT
2281response = client.responses.create(2281response = client.responses.create(
22822282 model: "gpt-6-astra", model: "gpt-6.2-astra",
2283 input: [2283 input: [
2284 {2284 {
2285 role: :user,2285 role: :user,
2634}2634}
2635```2635```
2636 2636
2637```ruby
2638require "openai"
2639require "pathname"
2640require "base64"
2641
2642client = OpenAI::Client.new
2643result = client.images.edit(
2644 model: "gpt-image-2.6-flare",
2645 image: OpenAI::FilePart.new(Pathname("kitchen.webp"), content_type: "image/webp"),
2646 markup: OpenAI::FilePart.new(Pathname("markup-vase-overlay-gpt-image-2-6.png"), content_type: "image/png"),
2647 quality: :medium,
2648 prompt: "Recolor the striped vase inside the red circle to solid cobalt blue ceramic. " \
2649 "Keep its shape, the dried stems, and the rest of the kitchen unchanged. " \
2650 "Remove the red circle from the finished photo."
2651)
2652image = result.data&.first or raise "No image returned"
2653encoded = image.b64_json or raise "No image data returned"
2654File.binwrite("vase.png", Base64.strict_decode64(encoded))
2655```
2656
2637```bash2657```bash
2638set -euo pipefail2658set -euo pipefail
2639 2659
2715const maskId = await createFile("fixtures/mask.png");2735const maskId = await createFile("fixtures/mask.png");
2716 2736
2717const response = await openai.responses.create({2737const response = await openai.responses.create({
27182738 model: "gpt-6-astra", model: "gpt-6.2-astra",
2719 input: [2739 input: [
2720 {2740 {
2721 role: "user",2741 role: "user",
2772maskId = create_file("mask.png")2792maskId = create_file("mask.png")
2773 2793
2774response = client.responses.create(2794response = client.responses.create(
27752795 model="gpt-6-astra", model="gpt-6.2-astra",
2776 input=[2796 input=[
2777 {2797 {
2778 "role": "user",2798 "role": "user",
2829 imageID := uploadImage(client, "sunlit_lounge.png")2849 imageID := uploadImage(client, "sunlit_lounge.png")
2830 maskID := uploadImage(client, "mask.png")2850 maskID := uploadImage(client, "mask.png")
2831 response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{2851 response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
28322852 Model: "gpt-6-astra", Model: "gpt-6.2-astra",
2833 Input: responses.ResponseNewParamsInputUnion{OfInputItemList: responses.ResponseInputParam{2853 Input: responses.ResponseNewParamsInputUnion{OfInputItemList: responses.ResponseInputParam{
2834 responses.ResponseInputItemParamOfMessage(2854 responses.ResponseInputItemParamOfMessage(
2835 responses.ResponseInputMessageContentListParam{2855 responses.ResponseInputMessageContentListParam{
2920 .responses()2940 .responses()
2921 .create(2941 .create(
2922 ResponseCreateParams.builder()2942 ResponseCreateParams.builder()
29232943 .model("gpt-6-astra") .model("gpt-6.2-astra")
2924 .inputOfResponse(2944 .inputOfResponse(
2925 List.of(2945 List.of(
2926 ResponseInputItem.ofMessage(2946 ResponseInputItem.ofMessage(
2960image = client.files.create(file: Pathname("sunlit_lounge.png"), purpose: :vision)2980image = client.files.create(file: Pathname("sunlit_lounge.png"), purpose: :vision)
2961mask = client.files.create(file: Pathname("mask.png"), purpose: :vision)2981mask = client.files.create(file: Pathname("mask.png"), purpose: :vision)
2962response = client.responses.create(2982response = client.responses.create(
29632983 model: "gpt-6-astra", model: "gpt-6.2-astra",
2964 input: [2984 input: [
2965 {2985 {
2966 role: :user,2986 role: :user,
3268}3288}
3269```3289```
3270 3290
3291```ruby
3292require "chunky_png"
3293
3294mask = ChunkyPNG::Image.from_file("mask.png")
3295mask.pixels.map! do |pixel|
3296 gray = (0.299 * ChunkyPNG::Color.r(pixel) + 0.587 * ChunkyPNG::Color.g(pixel) + 0.114 * ChunkyPNG::Color.b(pixel)).round
3297 ChunkyPNG::Color.rgba(gray, gray, gray, gray)
3298end
3299mask.save("mask_alpha.png", color_mode: ChunkyPNG::COLOR_TRUECOLOR_ALPHA)
3300```
3301
3271 3302
3272## Customize Image Output3303## Customize Image Output
3273 3304





