Skip to main content

dynamo_async_openai/types/
image.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Based on https://github.com/64bit/async-openai/ by Himanshu Neema
5// Original Copyright (c) 2022 Himanshu Neema
6// Licensed under MIT License (see ATTRIBUTIONS-Rust.md)
7//
8// Modifications Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES.
9// Licensed under Apache 2.0
10
11use derive_builder::Builder;
12use serde::{Deserialize, Serialize};
13
14use crate::error::OpenAIError;
15
16use super::InputSource;
17
18#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
19pub enum ImageSize {
20    #[serde(rename = "256x256")]
21    S256x256,
22    #[serde(rename = "512x512")]
23    S512x512,
24    #[default]
25    #[serde(rename = "1024x1024")]
26    S1024x1024,
27    #[serde(rename = "1792x1024")]
28    S1792x1024,
29    #[serde(rename = "1024x1792")]
30    S1024x1792,
31}
32
33#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
34pub enum DallE2ImageSize {
35    #[serde(rename = "256x256")]
36    S256x256,
37    #[serde(rename = "512x512")]
38    S512x512,
39    #[default]
40    #[serde(rename = "1024x1024")]
41    S1024x1024,
42}
43
44#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
45#[serde(rename_all = "lowercase")]
46pub enum ImageResponseFormat {
47    #[default]
48    Url,
49    #[serde(rename = "b64_json")]
50    B64Json,
51}
52
53#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
54pub enum ImageModel {
55    #[default]
56    #[serde(rename = "dall-e-2")]
57    DallE2,
58    #[serde(rename = "dall-e-3")]
59    DallE3,
60    #[serde(untagged)]
61    Other(String),
62}
63
64#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
65#[serde(rename_all = "lowercase")]
66pub enum ImageQuality {
67    #[default]
68    Standard,
69    HD,
70    High,
71    Medium,
72    Low,
73    Auto,
74}
75
76#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
77#[serde(rename_all = "lowercase")]
78pub enum ImageStyle {
79    #[default]
80    Vivid,
81    Natural,
82}
83
84#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
85#[serde(rename_all = "lowercase")]
86pub enum ImageModeration {
87    #[default]
88    Auto,
89    Low,
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize, Default, Builder, PartialEq)]
93#[builder(name = "CreateImageRequestArgs")]
94#[builder(pattern = "mutable")]
95#[builder(setter(into, strip_option), default)]
96#[builder(derive(Debug))]
97#[builder(build_fn(error = "OpenAIError"))]
98pub struct CreateImageRequest {
99    /// A text description of the desired image(s).
100    pub prompt: String,
101
102    /// The model to use for image generation.
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub model: Option<ImageModel>,
105
106    /// The number of images to generate. Must be between 1 and 10.
107    #[serde(skip_serializing_if = "Option::is_none")]
108    pub n: Option<u8>, // min:1 max:10 default:1
109
110    /// The quality of the image that will be generated. `hd` creates images with finer details and greater
111    /// consistency across the image. This param is only supported for `dall-e-3`.
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub quality: Option<ImageQuality>,
114
115    /// The format in which the generated images are returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been generated.
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub response_format: Option<ImageResponseFormat>,
118
119    /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
120    /// Must be one of `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3` models.
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub size: Option<ImageSize>,
123
124    /// The style of the generated images. Must be one of `vivid` or `natural`.
125    /// Vivid causes the model to lean towards generating hyper-real and dramatic images.
126    /// Natural causes the model to produce more natural, less hyper-real looking images.
127    /// This param is only supported for `dall-e-3`.
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub style: Option<ImageStyle>,
130
131    /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub user: Option<String>,
134
135    /// Control the content-moderation level for images generated by gpt-image-1.
136    /// Must be either `low` for less restrictive filtering or `auto` (default value).
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub moderation: Option<ImageModeration>,
139}
140
141#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
142#[serde(untagged)]
143pub enum Image {
144    /// The URL of the generated image, if `response_format` is `url` (default).
145    Url {
146        url: String,
147        revised_prompt: Option<String>,
148    },
149    /// The base64-encoded JSON of the generated image, if `response_format` is `b64_json`.
150    B64Json {
151        b64_json: std::sync::Arc<String>,
152        revised_prompt: Option<String>,
153    },
154}
155
156#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
157pub struct ImagesResponse {
158    pub created: u32,
159    pub data: Vec<std::sync::Arc<Image>>,
160}
161
162#[derive(Debug, Default, Clone, PartialEq)]
163pub struct ImageInput {
164    pub source: InputSource,
165}
166
167#[derive(Debug, Clone, Default, Builder, PartialEq)]
168#[builder(name = "CreateImageEditRequestArgs")]
169#[builder(pattern = "mutable")]
170#[builder(setter(into, strip_option), default)]
171#[builder(derive(Debug))]
172#[builder(build_fn(error = "OpenAIError"))]
173pub struct CreateImageEditRequest {
174    /// The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.
175    pub image: ImageInput,
176
177    /// A text description of the desired image(s). The maximum length is 1000 characters.
178    pub prompt: String,
179
180    /// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where `image` should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`.
181    pub mask: Option<ImageInput>,
182
183    /// The model to use for image generation. Only `dall-e-2` is supported at this time.
184    pub model: Option<ImageModel>,
185
186    /// The number of images to generate. Must be between 1 and 10.
187    pub n: Option<u8>, // min:1 max:10 default:1
188
189    /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`.
190    pub size: Option<DallE2ImageSize>,
191
192    /// The format in which the generated images are returned. Must be one of `url` or `b64_json`.
193    pub response_format: Option<ImageResponseFormat>,
194
195    /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
196    pub user: Option<String>,
197}
198
199#[derive(Debug, Default, Clone, Builder, PartialEq)]
200#[builder(name = "CreateImageVariationRequestArgs")]
201#[builder(pattern = "mutable")]
202#[builder(setter(into, strip_option), default)]
203#[builder(derive(Debug))]
204#[builder(build_fn(error = "OpenAIError"))]
205pub struct CreateImageVariationRequest {
206    /// The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
207    pub image: ImageInput,
208
209    /// The model to use for image generation. Only `dall-e-2` is supported at this time.
210    pub model: Option<ImageModel>,
211
212    /// The number of images to generate. Must be between 1 and 10.
213    pub n: Option<u8>, // min:1 max:10 default:1
214
215    /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`.
216    pub size: Option<DallE2ImageSize>,
217
218    /// The format in which the generated images are returned. Must be one of `url` or `b64_json`.
219    pub response_format: Option<ImageResponseFormat>,
220
221    /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
222    pub user: Option<String>,
223}