ruby/resources/images/methods/generate/index.md +0 −315 deleted
File Deleted View Diff
1## Create image
2
3`images.generate(**kwargs) -> ImagesResponse`
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- `prompt: String`
12
13 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`.
14
15- `background: :transparent | :opaque | :auto`
16
17 Allows to set transparency for the background of the generated image(s).
18 This parameter is only supported for GPT image models that support
19 transparent backgrounds. Must be one of `transparent`, `opaque`, or
20 `auto` (default value). When `auto` is used, the model will
21 automatically determine the best background for the image.
22
23 `gpt-image-2` and `gpt-image-2-2026-04-21` do not support
24 transparent backgrounds. Requests with `background` set to
25 `transparent` will return an error for these models; use `opaque` or
26 `auto` instead.
27
28 If `transparent`, the output format needs to support transparency,
29 so it should be set to either `png` (default value) or `webp`.
30
31 - `:transparent`
32
33 - `:opaque`
34
35 - `:auto`
36
37- `model: String | ImageModel`
38
39 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.
40
41 - `String = String`
42
43 - `ImageModel = :"gpt-image-1" | :"gpt-image-1-mini" | :"gpt-image-2" | 5 more`
44
45 - `:"gpt-image-1"`
46
47 - `:"gpt-image-1-mini"`
48
49 - `:"gpt-image-2"`
50
51 - `:"gpt-image-2-2026-04-21"`
52
53 - `:"gpt-image-1.5"`
54
55 - `:"chatgpt-image-latest"`
56
57 - `:"dall-e-2"`
58
59 - `:"dall-e-3"`
60
61- `moderation: :low | :auto`
62
63 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).
64
65 - `:low`
66
67 - `:auto`
68
69- `n: Integer`
70
71 The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
72
73- `output_compression: Integer`
74
75 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.
76
77- `output_format: :png | :jpeg | :webp`
78
79 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`.
80
81 - `:png`
82
83 - `:jpeg`
84
85 - `:webp`
86
87- `partial_images: Integer`
88
89 The number of partial images to generate. This parameter is used for
90 streaming responses that return partial images. Value must be between 0 and 3.
91 When set to 0, the response will be a single image sent in one streaming event.
92
93 Note that the final image may be sent before the full number of partial images
94 are generated if the full image is generated more quickly.
95
96- `quality: :standard | :hd | :low | 3 more`
97
98 The quality of the image that will be generated.
99
100 - `auto` (default value) will automatically select the best quality for the given model.
101 - `high`, `medium` and `low` are supported for the GPT image models.
102 - `hd` and `standard` are supported for `dall-e-3`.
103 - `standard` is the only option for `dall-e-2`.
104
105 - `:standard`
106
107 - `:hd`
108
109 - `:low`
110
111 - `:medium`
112
113 - `:high`
114
115 - `:auto`
116
117- `response_format: :url | :b64_json`
118
119 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.
120
121 - `:url`
122
123 - `:b64_json`
124
125- `size: String | :auto | :"1024x1024" | :"1536x1024" | 5 more`
126
127 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`.
128
129 - `String = String`
130
131 - `Size = :auto | :"1024x1024" | :"1536x1024" | 5 more`
132
133 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`.
134
135 - `:auto`
136
137 - `:"1024x1024"`
138
139 - `:"1536x1024"`
140
141 - `:"1024x1536"`
142
143 - `:"256x256"`
144
145 - `:"512x512"`
146
147 - `:"1792x1024"`
148
149 - `:"1024x1792"`
150
151- `stream: bool`
152
153 Generate the image in streaming mode. Defaults to `false`. See the
154 [Image generation guide](https://platform.openai.com/docs/guides/image-generation) for more information.
155 This parameter is only supported for the GPT image models.
156
157- `style: :vivid | :natural`
158
159 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.
160
161 - `:vivid`
162
163 - `:natural`
164
165- `user: String`
166
167 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).
168
169### Returns
170
171- `class ImagesResponse`
172
173 The response from the image generation endpoint.
174
175 - `created: Integer`
176
177 The Unix timestamp (in seconds) of when the image was created.
178
179 - `background: :transparent | :opaque`
180
181 The background parameter used for the image generation. Either `transparent` or `opaque`.
182
183 - `:transparent`
184
185 - `:opaque`
186
187 - `data: Array[Image]`
188
189 The list of generated images.
190
191 - `b64_json: String`
192
193 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`.
194
195 - `revised_prompt: String`
196
197 For `dall-e-3` only, the revised prompt that was used to generate the image.
198
199 - `url: String`
200
201 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.
202
203 - `output_format: :png | :webp | :jpeg`
204
205 The output format of the image generation. Either `png`, `webp`, or `jpeg`.
206
207 - `:png`
208
209 - `:webp`
210
211 - `:jpeg`
212
213 - `quality: :low | :medium | :high`
214
215 The quality of the image generated. Either `low`, `medium`, or `high`.
216
217 - `:low`
218
219 - `:medium`
220
221 - `:high`
222
223 - `size: :"1024x1024" | :"1024x1536" | :"1536x1024"`
224
225 The size of the image generated. Either `1024x1024`, `1024x1536`, or `1536x1024`.
226
227 - `:"1024x1024"`
228
229 - `:"1024x1536"`
230
231 - `:"1536x1024"`
232
233 - `usage: Usage{ input_tokens, input_tokens_details, output_tokens, 2 more}`
234
235 For `gpt-image-1` only, the token usage information for the image generation.
236
237 - `input_tokens: Integer`
238
239 The number of tokens (images and text) in the input prompt.
240
241 - `input_tokens_details: InputTokensDetails{ image_tokens, text_tokens}`
242
243 The input tokens detailed information for the image generation.
244
245 - `image_tokens: Integer`
246
247 The number of image tokens in the input prompt.
248
249 - `text_tokens: Integer`
250
251 The number of text tokens in the input prompt.
252
253 - `output_tokens: Integer`
254
255 The number of output tokens generated by the model.
256
257 - `total_tokens: Integer`
258
259 The total number of tokens (images and text) used for the image generation.
260
261 - `output_tokens_details: OutputTokensDetails{ image_tokens, text_tokens}`
262
263 The output token details for the image generation.
264
265 - `image_tokens: Integer`
266
267 The number of image output tokens generated by the model.
268
269 - `text_tokens: Integer`
270
271 The number of text output tokens generated by the model.
272
273### Example
274
275```ruby
276require "openai"
277
278openai = OpenAI::Client.new(api_key: "My API Key")
279
280images_response = openai.images.generate(prompt: "A cute baby sea otter")
281
282puts(images_response)
283```
284
285#### Response
286
287```json
288{
289 "created": 0,
290 "background": "transparent",
291 "data": [
292 {
293 "b64_json": "b64_json",
294 "revised_prompt": "revised_prompt",
295 "url": "https://example.com"
296 }
297 ],
298 "output_format": "png",
299 "quality": "low",
300 "size": "1024x1024",
301 "usage": {
302 "input_tokens": 0,
303 "input_tokens_details": {
304 "image_tokens": 0,
305 "text_tokens": 0
306 },
307 "output_tokens": 0,
308 "total_tokens": 0,
309 "output_tokens_details": {
310 "image_tokens": 0,
311 "text_tokens": 0
312 }
313 }
314}
315```