async_openai/types/images/image.rs
1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::error::OpenAIError;
5use crate::types::images::ImageInput;
6
7#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq)]
8pub enum ImageSize {
9 #[default]
10 #[serde(rename = "auto")]
11 Auto,
12 #[serde(rename = "256x256")]
13 S256x256,
14 #[serde(rename = "512x512")]
15 S512x512,
16 #[serde(rename = "1024x1024")]
17 S1024x1024,
18 #[serde(rename = "1792x1024")]
19 S1792x1024,
20 #[serde(rename = "1024x1792")]
21 S1024x1792,
22 #[serde(rename = "1536x1024")]
23 S1536x1024,
24 #[serde(rename = "1024x1536")]
25 S1024x1536,
26 #[serde(untagged)]
27 Other(String),
28}
29
30#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
31pub enum DallE2ImageSize {
32 #[serde(rename = "256x256")]
33 S256x256,
34 #[serde(rename = "512x512")]
35 S512x512,
36 #[default]
37 #[serde(rename = "1024x1024")]
38 S1024x1024,
39}
40
41#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
42pub enum DallE3ImageSize {
43 #[default]
44 #[serde(rename = "1024x1024")]
45 S1024x1024,
46 #[serde(rename = "1792x1024")]
47 S1792x1024,
48 #[serde(rename = "1024x1792")]
49 S1024x1792,
50}
51
52#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
53pub enum GptImage1ImageSize {
54 #[default]
55 #[serde(rename = "auto")]
56 Auto,
57 #[serde(rename = "1024x1024")]
58 S1024x1024,
59 #[serde(rename = "1536x1024")]
60 S1536x1024,
61 #[serde(rename = "1024x1536")]
62 S1024x1536,
63}
64
65#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
66#[serde(rename_all = "lowercase")]
67pub enum ImageResponseFormat {
68 #[default]
69 Url,
70 #[serde(rename = "b64_json")]
71 B64Json,
72}
73
74#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
75pub enum ImageModel {
76 #[serde(rename = "gpt-image-2")]
77 GptImage2,
78 #[serde(rename = "gpt-image-2-2026-04-21")]
79 GptImage2_2026_04_21,
80 #[serde(rename = "gpt-image-2.5-sunburst")]
81 GptImage2_5Sunburst,
82 #[serde(rename = "gpt-image-2.5-sunburst-2026-09-08")]
83 GptImage2_5Sunburst2026_09_08,
84 #[serde(rename = "gpt-image-2.5-flare")]
85 GptImage2_5Flare,
86 #[serde(rename = "gpt-image-2.5-flare-2026-09-08")]
87 GptImage2_5Flare2026_09_08,
88 #[serde(rename = "gpt-image-1")]
89 GptImage1,
90 #[serde(rename = "gpt-image-1.5")]
91 GptImage1dot5,
92 #[serde(rename = "gpt-image-1-mini")]
93 GptImage1Mini,
94 #[serde(rename = "chatgpt-image-latest")]
95 ChatGptImageLatest,
96 #[default]
97 #[serde(rename = "dall-e-2")]
98 DallE2,
99 #[serde(rename = "dall-e-3")]
100 DallE3,
101 #[serde(untagged)]
102 Other(String),
103}
104
105#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
106#[serde(rename_all = "lowercase")]
107pub enum ImageQuality {
108 Standard,
109 HD,
110 High,
111 Medium,
112 Low,
113 XHigh,
114 Max,
115 #[default]
116 Auto,
117}
118
119#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
120#[serde(rename_all = "lowercase")]
121pub enum ImageStyle {
122 #[default]
123 Vivid,
124 Natural,
125}
126
127#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
128#[serde(rename_all = "lowercase")]
129pub enum ImageModeration {
130 #[default]
131 Auto,
132 Low,
133}
134
135#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
136#[serde(rename_all = "lowercase")]
137pub enum ImageOutputFormat {
138 #[default]
139 Png,
140 Jpeg,
141 Webp,
142}
143
144#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
145#[serde(rename_all = "lowercase")]
146pub enum ImageBackground {
147 #[default]
148 Auto,
149 Transparent,
150 Opaque,
151}
152
153#[derive(Debug, Clone, Serialize, Deserialize, Default, Builder, PartialEq)]
154#[builder(name = "CreateImageRequestArgs")]
155#[builder(pattern = "mutable")]
156#[builder(setter(into, strip_option), default)]
157#[builder(derive(Debug))]
158#[builder(build_fn(error = "OpenAIError"))]
159pub struct CreateImageRequest {
160 /// A text description of the desired image(s). The maximum length is 32000 characters for
161 /// the GPT image models, 1000 characters for `dall-e-2` and 4000 characters for `dall-e-3`.
162 pub prompt: String,
163
164 /// The model to use for image generation. Supported models include `dall-e-2`,
165 /// `dall-e-3`, and the GPT image model family. Defaults to `dall-e-2` unless
166 /// a parameter specific to the GPT image models is used.
167 #[serde(skip_serializing_if = "Option::is_none")]
168 pub model: Option<ImageModel>,
169
170 /// The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
171 #[serde(skip_serializing_if = "Option::is_none")]
172 pub n: Option<u8>, // min:1 max:10 default:1
173
174 /// The quality of the image that will be generated.
175 ///
176 /// - `auto` (default value) will automatically select the best quality for the given model.
177 /// - `max`, `xhigh`, `high`, `medium`, and `low` are supported for compatible GPT image models.
178 /// - `hd` and `standard` are supported for `dall-e-3`.
179 /// - `standard` is the only option for `dall-e-2`.
180 #[serde(skip_serializing_if = "Option::is_none")]
181 pub quality: Option<ImageQuality>,
182
183 /// The format in which generated images with `dall-e-2` and `dall-e-3` are returned. Must be one of
184 /// `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been generated. This
185 /// parameter isn't supported for the GPT image models which will always return base64-encoded images.
186 #[serde(skip_serializing_if = "Option::is_none")]
187 pub response_format: Option<ImageResponseFormat>,
188
189 /// The format in which the generated images are returned. This parameter is only supported for
190 /// the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
191 #[serde(skip_serializing_if = "Option::is_none")]
192 pub output_format: Option<ImageOutputFormat>,
193
194 /// The compression level (0-100%) for the generated images. This parameter is only supported for
195 /// the GPT image models with the `webp` or `jpeg` output formats, and defaults to 100.
196 #[serde(skip_serializing_if = "Option::is_none")]
197 pub output_compression: Option<u8>,
198
199 /// Generate the image in streaming mode. Defaults to `false`. See the
200 /// [Image generation guide](https://platform.openai.com/docs/guides/image-generation) for more
201 /// information. This parameter is only supported for the GPT image models.
202 #[serde(skip_serializing_if = "Option::is_none")]
203 pub stream: Option<bool>,
204
205 /// The number of partial images to generate. This parameter is used for
206 /// streaming responses that return partial images. Value must be between 0 and 3.
207 /// When set to 0, the response will be a single image sent in one streaming event.
208 /// Note that the final image may be sent before the full number of partial images
209 /// are generated if the full image is generated more quickly.
210 #[serde(skip_serializing_if = "Option::is_none")]
211 pub partial_images: Option<u8>,
212
213 /// The size of the generated images. For `gpt-image-2` and
214 /// `gpt-image-2-2026-04-21`, arbitrary resolutions are supported as
215 /// `WIDTHxHEIGHT` strings, for example `1536x864`. Width and height
216 /// must both be divisible by 16 and the requested aspect ratio must be
217 /// between 1:3 and 3:1. Resolutions above `2560x1440` are experimental,
218 /// and the maximum supported resolution is `3840x2160`. The requested
219 /// size must also satisfy the model's current pixel and edge limits.
220 /// The standard sizes `1024x1024`, `1536x1024`, and `1024x1536` are
221 /// supported by the GPT image models; `auto` is supported for models
222 /// that allow automatic sizing. For `dall-e-2`, use one of `256x256`,
223 /// `512x512`, or `1024x1024`. For `dall-e-3`, use one of `1024x1024`,
224 /// `1792x1024`, or `1024x1792`.
225 #[serde(skip_serializing_if = "Option::is_none")]
226 pub size: Option<ImageSize>,
227
228 /// Control the content-moderation level for images generated by the GPT image models. Must be either `low`
229 /// for less restrictive filtering or `auto` (default value).
230 #[serde(skip_serializing_if = "Option::is_none")]
231 pub moderation: Option<ImageModeration>,
232
233 /// Set the background of the generated image(s). This parameter is only
234 /// supported for the GPT image models. Must be one of `transparent`, `opaque`,
235 /// or `auto` (default value). When `auto` is used, the model will automatically
236 /// determine the best background for the image.
237 ///
238 /// Transparent backgrounds are available for supported GPT Image models. For
239 /// `gpt-image-2` and `gpt-image-2-2026-04-21`, this support is in preview. When
240 /// using `transparent`, set the output format to `png` or `webp`.
241 #[serde(skip_serializing_if = "Option::is_none")]
242 pub background: Option<ImageBackground>,
243
244 /// The style of the generated images. This parameter is only supported for `dall-e-3`. Must be one of
245 ///`vivid` or `natural`. Vivid causes the model to lean towards generating hyper-real and dramatic
246 /// images. Natural causes the model to produce more natural, less hyper-real looking images.
247 #[serde(skip_serializing_if = "Option::is_none")]
248 pub style: Option<ImageStyle>,
249
250 /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
251 ///[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
252 #[serde(skip_serializing_if = "Option::is_none")]
253 pub user: Option<String>,
254}
255
256#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
257#[serde(untagged)]
258pub enum Image {
259 /// The URL of the generated image, if `response_format` is `url` (default).
260 Url {
261 url: String,
262 revised_prompt: Option<String>,
263 },
264 /// The base64-encoded JSON of the generated image, if `response_format` is `b64_json`.
265 B64Json {
266 b64_json: std::sync::Arc<String>,
267 revised_prompt: Option<String>,
268 },
269}
270
271#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
272#[serde(rename_all = "lowercase")]
273pub enum ImageResponseBackground {
274 Transparent,
275 Opaque,
276}
277
278#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
279pub struct ImageGenInputUsageDetails {
280 /// The number of text tokens in the input prompt.
281 pub text_tokens: u32,
282 /// The number of image tokens in the input prompt.
283 pub image_tokens: u32,
284}
285
286#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
287pub struct ImageGenOutputTokensDetails {
288 /// The number of text output tokens generated by the model.
289 pub text_tokens: u32,
290 /// The number of image output tokens generated by the model.
291 pub image_tokens: u32,
292}
293
294#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
295pub struct ImageGenUsage {
296 /// The number of tokens (images and text) in the input prompt.
297 pub input_tokens: u32,
298 /// The total number of tokens (images and text) used for the image generation.
299 pub total_tokens: u32,
300 /// The number of output tokens generated by the model.
301 pub output_tokens: u32,
302 /// The output token details for the image generation.
303 pub output_token_details: Option<ImageGenOutputTokensDetails>,
304 /// The input tokens detailed information for the image generation.
305 pub input_tokens_details: ImageGenInputUsageDetails,
306}
307
308#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
309pub struct ImagesResponse {
310 /// The Unix timestamp (in seconds) of when the image was created.
311 pub created: u32,
312 /// The list of generated images.
313 pub data: Vec<std::sync::Arc<Image>>,
314 /// The background parameter used for the image generation. Either `transparent` or `opaque`.
315 pub background: Option<ImageResponseBackground>,
316 /// The output format of the image generation. Either `png`, `webp`, or `jpeg`.
317 pub output_format: Option<ImageOutputFormat>,
318 /// The size of the generated image as a `WIDTHxHEIGHT` string.
319 pub size: Option<ImageSize>,
320 /// The quality of the image generated. One of `low`, `medium`, `high`, `xhigh`, or `max`.
321 pub quality: Option<ImageQuality>,
322 /// For the GPT image models only, the token usage information for the image generation.
323 pub usage: Option<ImageGenUsage>,
324}
325
326#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
327#[serde(rename_all = "lowercase")]
328pub enum InputFidelity {
329 High,
330 #[default]
331 Low,
332}
333
334#[derive(Debug, Clone, PartialEq)]
335pub enum ImageEditInput {
336 Image(ImageInput),
337 Images(Vec<ImageInput>),
338}
339
340#[derive(Debug, Clone, Default, Builder, PartialEq)]
341#[builder(name = "CreateImageEditRequestArgs")]
342#[builder(pattern = "mutable")]
343#[builder(setter(into, strip_option), default)]
344#[builder(derive(Debug))]
345#[builder(build_fn(error = "OpenAIError"))]
346pub struct CreateImageEditRequest {
347 /// The image(s) to edit. Must be a supported image file or an array of images.
348 ///
349 /// For the GPT image models, each image should be a `png`, `webp`, or `jpg`
350 /// file less than 50MB. You can provide up to 16 images.
351 /// `chatgpt-image-latest` follows the same input constraints as GPT image models.
352 ///
353 /// For `dall-e-2`, you can only provide one image, and it should be a square
354 /// `png` file less than 4MB.
355 pub image: ImageEditInput,
356
357 /// A text description of the desired image(s). The maximum length is 1000 characters
358 /// for `dall-e-2`, and 32000 characters for the GPT image models.
359 pub prompt: String,
360
361 /// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where
362 /// `image` should be edited. If there are multiple images provided, the mask will be applied on the
363 /// first image. Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`.
364 pub mask: Option<ImageInput>,
365
366 /// Set the background of the generated image(s). This parameter is only
367 /// supported for the GPT image models. Must be one of `transparent`, `opaque`,
368 /// or `auto` (default value). When `auto` is used, the model will automatically
369 /// determine the best background for the image.
370 ///
371 /// Transparent backgrounds are available for supported GPT Image models. For
372 /// `gpt-image-2` and `gpt-image-2-2026-04-21`, this support is in preview. When
373 /// using `transparent`, set the output format to `png` or `webp`.
374 pub background: Option<ImageBackground>,
375
376 /// The model to use for image generation. Supports `dall-e-2`, the GPT image
377 /// model family, and `chatgpt-image-latest`. Defaults to `gpt-image-1.5`.
378 pub model: Option<ImageModel>,
379
380 /// The number of images to generate. Must be between 1 and 10.
381 pub n: Option<u8>, // min:1 max:10 default:1
382
383 /// The size of the generated images. For `gpt-image-2` and
384 /// `gpt-image-2-2026-04-21`, arbitrary resolutions are supported as
385 /// `WIDTHxHEIGHT` strings, for example `1536x864`. Width and height
386 /// must both be divisible by 16 and the requested aspect ratio must be
387 /// between 1:3 and 3:1. Resolutions above `2560x1440` are experimental,
388 /// and the maximum supported resolution is `3840x2160`. The requested
389 /// size must also satisfy the model's current pixel and edge limits.
390 /// The standard sizes `1024x1024`, `1536x1024`, and `1024x1536` are
391 /// supported by the GPT image models; `auto` is supported for models
392 /// that allow automatic sizing. For `dall-e-2`, use one of `256x256`,
393 /// `512x512`, or `1024x1024`. For `dall-e-3`, use one of `1024x1024`,
394 /// `1792x1024`, or `1024x1792`.
395 pub size: Option<ImageSize>,
396
397 /// The format in which the generated images are returned. Must be one of `url` or `b64_json`. URLs
398 /// are only valid for 60 minutes after the image has been generated. This parameter is only supported
399 /// for `dall-e-2`, as the GPT image models will always return base64-encoded images.
400 pub response_format: Option<ImageResponseFormat>,
401
402 /// The format in which the generated images are returned. This parameter is
403 /// only supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
404 /// The default value is `png`.
405 pub output_format: Option<ImageOutputFormat>,
406
407 /// The compression level (0-100%) for the generated images. This parameter
408 /// is only supported for the GPT image models with the `webp` or `jpeg` output
409 /// formats, and defaults to 100.
410 pub output_compression: Option<u8>,
411
412 /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
413 /// [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
414 pub user: Option<String>,
415
416 /// Control how much effort the model will exert to match the style and features, especially facial
417 /// features, of input images. This parameter is only supported for the GPT image models. Unsupported for
418 /// `gpt-image-1-mini`. Supports `high` and `low`. Defaults to `low`.
419 pub input_fidelity: Option<InputFidelity>,
420
421 /// Edit the image in streaming mode. Defaults to `false`. See the
422 /// [Image generation guide](https://platform.openai.com/docs/guides/image-generation) for more
423 /// information.
424 pub stream: Option<bool>,
425
426 /// The number of partial images to generate. This parameter is used for
427 /// streaming responses that return partial images. Value must be between 0 and 3.
428 /// When set to 0, the response will be a single image sent in one streaming event.
429
430 /// Note that the final image may be sent before the full number of partial images
431 /// are generated if the full image is generated more quickly.
432 pub partial_images: Option<u8>,
433
434 /// The quality of the image that will be generated. GPT image models support
435 /// quality levels through `max`; `dall-e-2` supports `standard`. Defaults to `auto`.
436 pub quality: Option<ImageQuality>,
437}
438
439#[derive(Debug, Default, Clone, Builder, PartialEq)]
440#[builder(name = "CreateImageVariationRequestArgs")]
441#[builder(pattern = "mutable")]
442#[builder(setter(into, strip_option), default)]
443#[builder(derive(Debug))]
444#[builder(build_fn(error = "OpenAIError"))]
445pub struct CreateImageVariationRequest {
446 /// The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and
447 /// square.
448 pub image: ImageInput,
449
450 /// The model to use for image generation. Only `dall-e-2` is supported at this time.
451 pub model: Option<ImageModel>,
452
453 /// The number of images to generate. Must be between 1 and 10.
454 pub n: Option<u8>, // min:1 max:10 default:1
455
456 /// The format in which the generated images are returned. Must be one of `url` or `b64_json`. URLs
457 /// are only valid for 60 minutes after the image has been generated.
458 pub response_format: Option<ImageResponseFormat>,
459
460 /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`.
461 pub size: Option<DallE2ImageSize>,
462
463 /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
464 /// [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
465 pub user: Option<String>,
466}
467
468/// Reference an input image by either URL or uploaded file ID.
469/// Provide exactly one of `image_url` or `file_id`.
470#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
471pub struct ImageRefParam {
472 /// A fully qualified URL or base64-encoded data URL.
473 #[serde(skip_serializing_if = "Option::is_none")]
474 pub image_url: Option<String>,
475 /// The File API ID of an uploaded image to use as input.
476 #[serde(skip_serializing_if = "Option::is_none")]
477 pub file_id: Option<String>,
478}
479
480/// JSON request body for image edits.
481///
482/// Use `images` (array of `ImageRefParam`) instead of multipart `image` uploads.
483/// You can reference images via external URLs, data URLs, or uploaded file IDs.
484/// JSON edits support GPT image models only; DALL-E edits require multipart (`dall-e-2` only).
485#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Builder, Default)]
486#[builder(name = "EditImageJsonRequestArgs")]
487#[builder(pattern = "mutable")]
488#[builder(setter(into, strip_option), default)]
489#[builder(derive(Debug))]
490#[builder(build_fn(error = "OpenAIError"))]
491pub struct EditImageJsonRequest {
492 /// The model to use for image editing.
493 #[serde(skip_serializing_if = "Option::is_none")]
494 pub model: Option<ImageModel>,
495
496 /// Input image references to edit. For GPT image models, you can provide up to 16 images.
497 pub images: Vec<ImageRefParam>,
498
499 /// An optional mask image reference indicating which areas of the image should be edited.
500 #[serde(skip_serializing_if = "Option::is_none")]
501 pub mask: Option<ImageRefParam>,
502
503 /// A text description of the desired image edit.
504 pub prompt: String,
505
506 /// The number of edited images to generate. Must be between 1 and 10.
507 #[serde(skip_serializing_if = "Option::is_none")]
508 pub n: Option<u8>,
509
510 /// The quality of the image that will be generated.
511 #[serde(skip_serializing_if = "Option::is_none")]
512 pub quality: Option<ImageQuality>,
513
514 /// Control how much effort the model will exert to match the style and features,
515 /// especially facial features, of input images. Supports `high` and `low`. Defaults to `low`.
516 #[serde(skip_serializing_if = "Option::is_none")]
517 pub input_fidelity: Option<InputFidelity>,
518
519 /// The size of the generated image.
520 #[serde(skip_serializing_if = "Option::is_none")]
521 pub size: Option<ImageSize>,
522
523 /// A unique identifier representing your end-user.
524 #[serde(skip_serializing_if = "Option::is_none")]
525 pub user: Option<String>,
526
527 /// The output format for the generated image.
528 #[serde(skip_serializing_if = "Option::is_none")]
529 pub output_format: Option<ImageOutputFormat>,
530
531 /// The compression level (0-100%) for the generated images.
532 #[serde(skip_serializing_if = "Option::is_none")]
533 pub output_compression: Option<u8>,
534
535 /// Control the content-moderation level for images.
536 #[serde(skip_serializing_if = "Option::is_none")]
537 pub moderation: Option<ImageModeration>,
538
539 /// The background style for the generated image.
540 #[serde(skip_serializing_if = "Option::is_none")]
541 pub background: Option<ImageBackground>,
542
543 /// Whether to stream the image generation. Defaults to `false`.
544 #[serde(skip_serializing_if = "Option::is_none")]
545 pub stream: Option<bool>,
546
547 /// The number of partial images to generate during streaming.
548 #[serde(skip_serializing_if = "Option::is_none")]
549 pub partial_images: Option<u8>,
550}