go/resources/images/methods/generate/index.md +0 −326 deleted
File Deleted View Diff
1## Create image
2
3`client.Images.Generate(ctx, body) (*ImagesResponse, error)`
4
5**post** `/images/generations`
6
7Creates an image given a prompt. [Learn more](https://platform.openai.com/docs/guides/images).
8
9### Parameters
10
11- `body ImageGenerateParams`
12
13 - `Prompt param.Field[string]`
14
15 A text description of the desired image(s). The maximum length is 32000 characters for the GPT image models, 1000 characters for `dall-e-2` and 4000 characters for `dall-e-3`.
16
17 - `Background param.Field[ImageGenerateParamsBackground]`
18
19 Allows to set transparency for the background of the generated image(s).
20 This parameter is only supported for GPT image models that support
21 transparent backgrounds. Must be one of `transparent`, `opaque`, or
22 `auto` (default value). When `auto` is used, the model will
23 automatically determine the best background for the image.
24
25 `gpt-image-2` and `gpt-image-2-2026-04-21` do not support
26 transparent backgrounds. Requests with `background` set to
27 `transparent` will return an error for these models; use `opaque` or
28 `auto` instead.
29
30 If `transparent`, the output format needs to support transparency,
31 so it should be set to either `png` (default value) or `webp`.
32
33 - `const ImageGenerateParamsBackgroundTransparent ImageGenerateParamsBackground = "transparent"`
34
35 - `const ImageGenerateParamsBackgroundOpaque ImageGenerateParamsBackground = "opaque"`
36
37 - `const ImageGenerateParamsBackgroundAuto ImageGenerateParamsBackground = "auto"`
38
39 - `Model param.Field[ImageModel]`
40
41 The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or a GPT image model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`, `gpt-image-2`, or `gpt-image-2-2026-04-21`). Defaults to `dall-e-2` unless a parameter specific to the GPT image models is used.
42
43 - `string`
44
45 - `type ImageModel string`
46
47 - `const ImageModelGPTImage1 ImageModel = "gpt-image-1"`
48
49 - `const ImageModelGPTImage1Mini ImageModel = "gpt-image-1-mini"`
50
51 - `const ImageModelGPTImage2 ImageModel = "gpt-image-2"`
52
53 - `const ImageModelGPTImage2_2026_04_21 ImageModel = "gpt-image-2-2026-04-21"`
54
55 - `const ImageModelGPTImage1_5 ImageModel = "gpt-image-1.5"`
56
57 - `const ImageModelChatgptImageLatest ImageModel = "chatgpt-image-latest"`
58
59 - `const ImageModelDallE2 ImageModel = "dall-e-2"`
60
61 - `const ImageModelDallE3 ImageModel = "dall-e-3"`
62
63 - `Moderation param.Field[ImageGenerateParamsModeration]`
64
65 Control the content-moderation level for images generated by the GPT image models. Must be either `low` for less restrictive filtering or `auto` (default value).
66
67 - `const ImageGenerateParamsModerationLow ImageGenerateParamsModeration = "low"`
68
69 - `const ImageGenerateParamsModerationAuto ImageGenerateParamsModeration = "auto"`
70
71 - `N param.Field[int64]`
72
73 The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
74
75 - `OutputCompression param.Field[int64]`
76
77 The compression level (0-100%) for the generated images. This parameter is only supported for the GPT image models with the `webp` or `jpeg` output formats, and defaults to 100.
78
79 - `OutputFormat param.Field[ImageGenerateParamsOutputFormat]`
80
81 The format in which the generated images are returned. This parameter is only supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
82
83 - `const ImageGenerateParamsOutputFormatPNG ImageGenerateParamsOutputFormat = "png"`
84
85 - `const ImageGenerateParamsOutputFormatJPEG ImageGenerateParamsOutputFormat = "jpeg"`
86
87 - `const ImageGenerateParamsOutputFormatWebP ImageGenerateParamsOutputFormat = "webp"`
88
89 - `PartialImages param.Field[int64]`
90
91 The number of partial images to generate. This parameter is used for
92 streaming responses that return partial images. Value must be between 0 and 3.
93 When set to 0, the response will be a single image sent in one streaming event.
94
95 Note that the final image may be sent before the full number of partial images
96 are generated if the full image is generated more quickly.
97
98 - `Quality param.Field[ImageGenerateParamsQuality]`
99
100 The quality of the image that will be generated.
101
102 - `auto` (default value) will automatically select the best quality for the given model.
103 - `high`, `medium` and `low` are supported for the GPT image models.
104 - `hd` and `standard` are supported for `dall-e-3`.
105 - `standard` is the only option for `dall-e-2`.
106
107 - `const ImageGenerateParamsQualityStandard ImageGenerateParamsQuality = "standard"`
108
109 - `const ImageGenerateParamsQualityHD ImageGenerateParamsQuality = "hd"`
110
111 - `const ImageGenerateParamsQualityLow ImageGenerateParamsQuality = "low"`
112
113 - `const ImageGenerateParamsQualityMedium ImageGenerateParamsQuality = "medium"`
114
115 - `const ImageGenerateParamsQualityHigh ImageGenerateParamsQuality = "high"`
116
117 - `const ImageGenerateParamsQualityAuto ImageGenerateParamsQuality = "auto"`
118
119 - `ResponseFormat param.Field[ImageGenerateParamsResponseFormat]`
120
121 The format in which generated images with `dall-e-2` and `dall-e-3` are returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been generated. This parameter isn't supported for the GPT image models, which always return base64-encoded images.
122
123 - `const ImageGenerateParamsResponseFormatURL ImageGenerateParamsResponseFormat = "url"`
124
125 - `const ImageGenerateParamsResponseFormatB64JSON ImageGenerateParamsResponseFormat = "b64_json"`
126
127 - `Size param.Field[ImageGenerateParamsSize]`
128
129 The size of the generated images. For `gpt-image-2` and `gpt-image-2-2026-04-21`, arbitrary resolutions are supported as `WIDTHxHEIGHT` strings, for example `1536x864`. Width and height must both be divisible by 16 and the requested aspect ratio must be between 1:3 and 3:1. Resolutions above `2560x1440` are experimental, and the maximum supported resolution is `3840x2160`. The requested size must also satisfy the model's current pixel and edge limits. The standard sizes `1024x1024`, `1536x1024`, and `1024x1536` are supported by the GPT image models; `auto` is supported for models that allow automatic sizing. For `dall-e-2`, use one of `256x256`, `512x512`, or `1024x1024`. For `dall-e-3`, use one of `1024x1024`, `1792x1024`, or `1024x1792`.
130
131 - `string`
132
133 - `ImageGenerateParamsSize`
134
135 - `const ImageGenerateParamsSizeAuto ImageGenerateParamsSize = "auto"`
136
137 - `const ImageGenerateParamsSize1024x1024 ImageGenerateParamsSize = "1024x1024"`
138
139 - `const ImageGenerateParamsSize1536x1024 ImageGenerateParamsSize = "1536x1024"`
140
141 - `const ImageGenerateParamsSize1024x1536 ImageGenerateParamsSize = "1024x1536"`
142
143 - `const ImageGenerateParamsSize256x256 ImageGenerateParamsSize = "256x256"`
144
145 - `const ImageGenerateParamsSize512x512 ImageGenerateParamsSize = "512x512"`
146
147 - `const ImageGenerateParamsSize1792x1024 ImageGenerateParamsSize = "1792x1024"`
148
149 - `const ImageGenerateParamsSize1024x1792 ImageGenerateParamsSize = "1024x1792"`
150
151 - ``
152
153 - `Style param.Field[ImageGenerateParamsStyle]`
154
155 The style of the generated images. This parameter is only supported for `dall-e-3`. Must be one of `vivid` or `natural`. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images.
156
157 - `const ImageGenerateParamsStyleVivid ImageGenerateParamsStyle = "vivid"`
158
159 - `const ImageGenerateParamsStyleNatural ImageGenerateParamsStyle = "natural"`
160
161 - `User param.Field[string]`
162
163 A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
164
165### Returns
166
167- `type ImagesResponse struct{…}`
168
169 The response from the image generation endpoint.
170
171 - `Created int64`
172
173 The Unix timestamp (in seconds) of when the image was created.
174
175 - `Background ImagesResponseBackground`
176
177 The background parameter used for the image generation. Either `transparent` or `opaque`.
178
179 - `const ImagesResponseBackgroundTransparent ImagesResponseBackground = "transparent"`
180
181 - `const ImagesResponseBackgroundOpaque ImagesResponseBackground = "opaque"`
182
183 - `Data []Image`
184
185 The list of generated images.
186
187 - `B64JSON string`
188
189 The base64-encoded JSON of the generated image. Returned by default for the GPT image models, and only present if `response_format` is set to `b64_json` for `dall-e-2` and `dall-e-3`.
190
191 - `RevisedPrompt string`
192
193 For `dall-e-3` only, the revised prompt that was used to generate the image.
194
195 - `URL string`
196
197 When using `dall-e-2` or `dall-e-3`, the URL of the generated image if `response_format` is set to `url` (default value). Unsupported for the GPT image models.
198
199 - `OutputFormat ImagesResponseOutputFormat`
200
201 The output format of the image generation. Either `png`, `webp`, or `jpeg`.
202
203 - `const ImagesResponseOutputFormatPNG ImagesResponseOutputFormat = "png"`
204
205 - `const ImagesResponseOutputFormatWebP ImagesResponseOutputFormat = "webp"`
206
207 - `const ImagesResponseOutputFormatJPEG ImagesResponseOutputFormat = "jpeg"`
208
209 - `Quality ImagesResponseQuality`
210
211 The quality of the image generated. Either `low`, `medium`, or `high`.
212
213 - `const ImagesResponseQualityLow ImagesResponseQuality = "low"`
214
215 - `const ImagesResponseQualityMedium ImagesResponseQuality = "medium"`
216
217 - `const ImagesResponseQualityHigh ImagesResponseQuality = "high"`
218
219 - `Size ImagesResponseSize`
220
221 The size of the image generated. Either `1024x1024`, `1024x1536`, or `1536x1024`.
222
223 - `const ImagesResponseSize1024x1024 ImagesResponseSize = "1024x1024"`
224
225 - `const ImagesResponseSize1024x1536 ImagesResponseSize = "1024x1536"`
226
227 - `const ImagesResponseSize1536x1024 ImagesResponseSize = "1536x1024"`
228
229 - `Usage ImagesResponseUsage`
230
231 For `gpt-image-1` only, the token usage information for the image generation.
232
233 - `InputTokens int64`
234
235 The number of tokens (images and text) in the input prompt.
236
237 - `InputTokensDetails ImagesResponseUsageInputTokensDetails`
238
239 The input tokens detailed information for the image generation.
240
241 - `ImageTokens int64`
242
243 The number of image tokens in the input prompt.
244
245 - `TextTokens int64`
246
247 The number of text tokens in the input prompt.
248
249 - `OutputTokens int64`
250
251 The number of output tokens generated by the model.
252
253 - `TotalTokens int64`
254
255 The total number of tokens (images and text) used for the image generation.
256
257 - `OutputTokensDetails ImagesResponseUsageOutputTokensDetails`
258
259 The output token details for the image generation.
260
261 - `ImageTokens int64`
262
263 The number of image output tokens generated by the model.
264
265 - `TextTokens int64`
266
267 The number of text output tokens generated by the model.
268
269### Example
270
271```go
272package main
273
274import (
275 "context"
276 "fmt"
277
278 "github.com/openai/openai-go"
279 "github.com/openai/openai-go/option"
280)
281
282func main() {
283 client := openai.NewClient(
284 option.WithAPIKey("My API Key"),
285 )
286 imagesResponse, err := client.Images.Generate(context.TODO(), openai.ImageGenerateParams{
287 Prompt: "A cute baby sea otter",
288 })
289 if err != nil {
290 panic(err.Error())
291 }
292 fmt.Printf("%+v\n", imagesResponse)
293}
294```
295
296#### Response
297
298```json
299{
300 "created": 0,
301 "background": "transparent",
302 "data": [
303 {
304 "b64_json": "b64_json",
305 "revised_prompt": "revised_prompt",
306 "url": "https://example.com"
307 }
308 ],
309 "output_format": "png",
310 "quality": "low",
311 "size": "1024x1024",
312 "usage": {
313 "input_tokens": 0,
314 "input_tokens_details": {
315 "image_tokens": 0,
316 "text_tokens": 0
317 },
318 "output_tokens": 0,
319 "total_tokens": 0,
320 "output_tokens_details": {
321 "image_tokens": 0,
322 "text_tokens": 0
323 }
324 }
325}
326```