Skip to main content

async_openai/types/images/
stream.rs

1use serde::{Deserialize, Serialize};
2
3use crate::types::images::{
4    ImageBackground, ImageGenUsage, ImageOutputFormat, ImageQuality, ImageSize,
5};
6
7/// Emitted when a partial image is available during image generation streaming.
8#[derive(Debug, Clone, Serialize, Deserialize, Default)]
9pub struct ImageGenPartialImageEvent {
10    /// Base64-encoded partial image data, suitable for rendering as an image.
11    pub b64_json: String,
12    /// The Unix timestamp when the event was created.
13    pub created_at: u64,
14    /// The image dimensions as a `WIDTHxHEIGHT` string, for example `1536x864`.
15    pub size: ImageSize,
16    /// The quality setting for the requested image.
17    pub quality: ImageQuality,
18    /// The background setting for the requested image.
19    pub background: ImageBackground,
20    /// The output format for the requested image.
21    pub output_format: ImageOutputFormat,
22    /// 0-based index for the partial image (streaming).
23    pub partial_image_index: u8,
24}
25
26/// Emitted when image generation has completed and the final image is available.
27#[derive(Debug, Clone, Serialize, Deserialize, Default)]
28pub struct ImageGenCompletedEvent {
29    /// Base64-encoded image data, suitable for rendering as an image.
30    pub b64_json: String,
31    /// The Unix timestamp when the event was created.
32    pub created_at: u64,
33    /// The image dimensions as a `WIDTHxHEIGHT` string, for example `1536x864`.
34    pub size: ImageSize,
35    /// The quality setting for the generated image.
36    pub quality: ImageQuality,
37    /// The background setting for the generated image.
38    pub background: ImageBackground,
39    /// The output format for the generated image.
40    pub output_format: ImageOutputFormat,
41    /// Token usage information for the image generation.
42    pub usage: ImageGenUsage,
43
44    /// The effective safety identifier used for safety monitoring and abuse detection. This is the
45    /// non-empty `safety_identifier` from the request, or the deprecated `user` value when
46    /// `safety_identifier` is omitted, null, or empty.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub safety_identifier: Option<String>,
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
52#[serde(tag = "type")]
53pub enum ImageGenStreamEvent {
54    /// Emitted when a partial image is available during image generation streaming.
55    #[serde(rename = "image_generation.partial_image")]
56    PartialImage(ImageGenPartialImageEvent),
57    /// Emitted when image generation has completed and the final image is available.
58    #[serde(rename = "image_generation.completed")]
59    Completed(ImageGenCompletedEvent),
60}
61
62/// Emitted when a partial image is available during image editing streaming.
63#[derive(Debug, Clone, Serialize, Deserialize, Default)]
64pub struct ImageEditPartialImageEvent {
65    /// Base64-encoded partial image data, suitable for rendering as an image.
66    pub b64_json: String,
67    /// The Unix timestamp when the event was created.
68    pub created_at: u64,
69    /// The image dimensions as a `WIDTHxHEIGHT` string, for example `1536x864`.
70    pub size: ImageSize,
71    /// The quality setting for the requested edited image.
72    pub quality: ImageQuality,
73    /// The background setting for the requested edited image.
74    pub background: ImageBackground,
75    /// The output format for the requested edited image.
76    pub output_format: ImageOutputFormat,
77    /// 0-based index for the partial image (streaming).
78    pub partial_image_index: u8,
79}
80
81/// Emitted when image editing has completed and the final image is available.
82#[derive(Debug, Clone, Serialize, Deserialize, Default)]
83pub struct ImageEditCompletedEvent {
84    /// Base64-encoded final image data, suitable for rendering as an image.
85    pub b64_json: String,
86    /// The Unix timestamp when the event was created.
87    pub created_at: u64,
88    /// The image dimensions as a `WIDTHxHEIGHT` string, for example `1536x864`.
89    pub size: ImageSize,
90    /// The quality setting for the edited image.
91    pub quality: ImageQuality,
92    /// The background setting for the edited image.
93    pub background: ImageBackground,
94    /// The output format for the edited image.
95    pub output_format: ImageOutputFormat,
96    /// Token usage information for the image edit.
97    pub usage: ImageGenUsage,
98
99    /// The effective safety identifier used for safety monitoring and abuse detection. This is the
100    /// non-empty `safety_identifier` from the request, or the deprecated `user` value when
101    /// `safety_identifier` is omitted, null, or empty.
102    #[serde(skip_serializing_if = "Option::is_none")]
103    pub safety_identifier: Option<String>,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize)]
107#[serde(tag = "type")]
108pub enum ImageEditStreamEvent {
109    /// Emitted when a partial image is available during image editing streaming.
110    #[serde(rename = "image_edit.partial_image")]
111    PartialImage(ImageEditPartialImageEvent),
112    /// Emitted when image editing has completed and the final image is available.
113    #[serde(rename = "image_edit.completed")]
114    Completed(ImageEditCompletedEvent),
115}
116
117#[cfg(feature = "_api")]
118pub type ImageEditStream = crate::types::stream::StreamResponse<ImageEditStreamEvent>;
119
120#[cfg(feature = "_api")]
121pub type ImageGenStream = crate::types::stream::StreamResponse<ImageGenStreamEvent>;
122
123#[cfg(feature = "_api")]
124macro_rules! impl_event_type {
125    ($($ty:ty => $event_type:expr),* $(,)?) => {
126        $(
127            impl crate::traits::EventType for $ty {
128                fn event_type(&self) -> &'static str {
129                    $event_type
130                }
131            }
132        )*
133    };
134}
135
136#[cfg(feature = "_api")]
137impl_event_type! {
138    ImageGenPartialImageEvent => "image_generation.partial_image",
139    ImageGenCompletedEvent => "image_generation.completed",
140    ImageEditPartialImageEvent => "image_edit.partial_image",
141    ImageEditCompletedEvent => "image_edit.completed",
142}
143
144#[cfg(feature = "_api")]
145impl crate::traits::EventType for ImageGenStreamEvent {
146    fn event_type(&self) -> &'static str {
147        match self {
148            ImageGenStreamEvent::PartialImage(event) => event.event_type(),
149            ImageGenStreamEvent::Completed(event) => event.event_type(),
150        }
151    }
152}
153
154#[cfg(feature = "_api")]
155impl crate::traits::EventType for ImageEditStreamEvent {
156    fn event_type(&self) -> &'static str {
157        match self {
158            ImageEditStreamEvent::PartialImage(event) => event.event_type(),
159            ImageEditStreamEvent::Completed(event) => event.event_type(),
160        }
161    }
162}