Skip to main content

openai_types/responses/
streaming.rs

1// MANUAL — hand-maintained. py2rust sync will not overwrite.
2// Streaming event types for the Responses API.
3
4use serde::{Deserialize, Serialize};
5
6use super::output::{OutputItem, ResponseOutputItem};
7use super::response::Response;
8
9/// Emitted when there is a text delta.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
12pub struct ResponseTextDeltaEvent {
13    /// Sequence number.
14    #[serde(default)]
15    pub sequence_number: i64,
16    /// The ID of the output item.
17    #[serde(default)]
18    pub item_id: String,
19    /// Index of the output item.
20    #[serde(default)]
21    pub output_index: i64,
22    /// Index of the content part.
23    #[serde(default)]
24    pub content_index: i64,
25    /// The text delta.
26    pub delta: String,
27    /// Log probabilities (if requested).
28    #[serde(default)]
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub logprobs: Option<serde_json::Value>,
31}
32
33/// Emitted when there is a reasoning text delta.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
36pub struct ResponseReasoningTextDeltaEvent {
37    /// Sequence number.
38    #[serde(default)]
39    pub sequence_number: i64,
40    /// The ID of the item.
41    #[serde(default)]
42    pub item_id: String,
43    /// Index of the output item.
44    #[serde(default)]
45    pub output_index: i64,
46    /// Index of the content part.
47    #[serde(default)]
48    pub content_index: i64,
49    /// The reasoning text delta.
50    pub delta: String,
51}
52
53/// Emitted when there is a reasoning summary text delta.
54#[derive(Debug, Clone, Serialize, Deserialize)]
55#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
56pub struct ResponseReasoningSummaryTextDeltaEvent {
57    /// Sequence number.
58    #[serde(default)]
59    pub sequence_number: i64,
60    /// The ID of the item.
61    #[serde(default)]
62    pub item_id: String,
63    /// Index of the output item.
64    #[serde(default)]
65    pub output_index: i64,
66    /// Index of the summary part.
67    #[serde(default)]
68    pub summary_index: i64,
69    /// The summary text delta.
70    pub delta: String,
71}
72
73/// Emitted when a new output item is added.
74#[derive(Debug, Clone, Serialize, Deserialize)]
75#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
76pub struct ResponseOutputItemAddedEvent {
77    /// Sequence number.
78    #[serde(default)]
79    pub sequence_number: i64,
80    /// Index of the output item.
81    #[serde(default)]
82    pub output_index: i64,
83    /// The output item.
84    pub item: OutputItem,
85}
86
87/// Emitted when there is a function call arguments delta.
88#[derive(Debug, Clone, Serialize, Deserialize)]
89#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
90pub struct ResponseFunctionCallArgumentsDeltaEvent {
91    /// Sequence number.
92    #[serde(default)]
93    pub sequence_number: i64,
94    /// The ID of the item.
95    #[serde(default)]
96    pub item_id: String,
97    /// Index of the output item.
98    #[serde(default)]
99    pub output_index: u32,
100    /// The arguments delta.
101    pub delta: String,
102}
103
104/// Emitted when function call arguments are finalized.
105#[derive(Debug, Clone, Serialize, Deserialize)]
106#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
107pub struct ResponseFunctionCallArgumentsDoneEvent {
108    /// Sequence number.
109    #[serde(default)]
110    pub sequence_number: i64,
111    /// The ID of the item.
112    #[serde(default)]
113    pub item_id: String,
114    /// Index of the output item.
115    #[serde(default)]
116    pub output_index: u32,
117    /// The complete arguments JSON.
118    pub arguments: String,
119    /// The function name.
120    #[serde(default)]
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub name: Option<String>,
123}
124
125/// Emitted when the response is complete.
126#[derive(Debug, Clone, Serialize, Deserialize)]
127#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
128pub struct ResponseCompletedEvent {
129    /// Sequence number.
130    #[serde(default)]
131    pub sequence_number: i64,
132    /// The completed response.
133    pub response: Response,
134}
135
136/// Emitted when the response fails.
137#[derive(Debug, Clone, Serialize, Deserialize)]
138#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
139pub struct ResponseFailedEvent {
140    /// Sequence number.
141    #[serde(default)]
142    pub sequence_number: i64,
143    /// The failed response.
144    pub response: Response,
145}
146
147/// Emitted when the response is incomplete.
148#[derive(Debug, Clone, Serialize, Deserialize)]
149#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
150pub struct ResponseIncompleteEvent {
151    /// Sequence number.
152    #[serde(default)]
153    pub sequence_number: i64,
154    /// The incomplete response.
155    pub response: Response,
156}
157
158/// Emitted when an error occurs during streaming.
159#[derive(Debug, Clone, Serialize, Deserialize)]
160#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
161pub struct ResponseErrorEvent {
162    /// Sequence number.
163    #[serde(default)]
164    pub sequence_number: i64,
165    /// Error message.
166    pub message: String,
167    /// Error code.
168    #[serde(default)]
169    #[serde(skip_serializing_if = "Option::is_none")]
170    pub code: Option<String>,
171    /// Error parameter.
172    #[serde(default)]
173    #[serde(skip_serializing_if = "Option::is_none")]
174    pub param: Option<String>,
175}
176
177/// A streaming event from the Responses API.
178///
179/// Uses `#[serde(tag = "type")]` for typed deserialization. Unknown event types
180/// fall through to the `Other` variant to ensure forward compatibility.
181#[derive(Debug, Clone, Deserialize, Serialize)]
182#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
183#[serde(tag = "type")]
184#[non_exhaustive]
185pub enum ResponseStreamEvent {
186    // -- Lifecycle events --
187    /// Response created.
188    #[serde(rename = "response.created")]
189    ResponseCreated {
190        /// The created response.
191        response: Response,
192    },
193    /// Response in progress.
194    #[serde(rename = "response.in_progress")]
195    ResponseInProgress {
196        /// The in-progress response.
197        response: Response,
198    },
199    /// Response completed.
200    #[serde(rename = "response.completed")]
201    ResponseCompleted(ResponseCompletedEvent),
202    /// Response failed.
203    #[serde(rename = "response.failed")]
204    ResponseFailed(ResponseFailedEvent),
205    /// Response incomplete.
206    #[serde(rename = "response.incomplete")]
207    ResponseIncomplete(ResponseIncompleteEvent),
208
209    // -- Output item events --
210    /// New output item added.
211    #[serde(rename = "response.output_item.added")]
212    ResponseOutputItemAdded(ResponseOutputItemAddedEvent),
213    /// Output item done.
214    #[serde(rename = "response.output_item.done")]
215    OutputItemDone {
216        /// Index of the output item.
217        output_index: i64,
218        /// The completed output item.
219        item: ResponseOutputItem,
220    },
221
222    // -- Content part events --
223    /// Content part added.
224    #[serde(rename = "response.content_part.added")]
225    ContentPartAdded {
226        /// Index of the output item.
227        output_index: i64,
228        /// Index of the content part.
229        content_index: i64,
230        /// The content part.
231        part: serde_json::Value,
232    },
233    /// Content part done.
234    #[serde(rename = "response.content_part.done")]
235    ContentPartDone {
236        /// Index of the output item.
237        output_index: i64,
238        /// Index of the content part.
239        content_index: i64,
240        /// The content part.
241        part: serde_json::Value,
242    },
243
244    // -- Text delta events --
245    /// Text output delta.
246    #[serde(rename = "response.output_text.delta")]
247    ResponseOutputTextDelta(ResponseTextDeltaEvent),
248    /// Text output done.
249    #[serde(rename = "response.output_text.done")]
250    OutputTextDone {
251        /// Index of the output item.
252        output_index: i64,
253        /// Index of the content part.
254        content_index: i64,
255        /// The complete text.
256        text: String,
257    },
258
259    // -- Function call events --
260    /// Function call arguments delta.
261    #[serde(rename = "response.function_call_arguments.delta")]
262    ResponseFunctionCallArgumentsDelta(ResponseFunctionCallArgumentsDeltaEvent),
263    /// Function call arguments done.
264    #[serde(rename = "response.function_call_arguments.done")]
265    ResponseFunctionCallArgumentsDone(ResponseFunctionCallArgumentsDoneEvent),
266
267    // -- Reasoning events --
268    /// Reasoning text delta.
269    #[serde(rename = "response.reasoning_text.delta")]
270    ResponseReasoningTextDelta(ResponseReasoningTextDeltaEvent),
271    /// Reasoning summary text delta.
272    #[serde(rename = "response.reasoning_summary_text.delta")]
273    ResponseReasoningSummaryTextDelta(ResponseReasoningSummaryTextDeltaEvent),
274    /// Reasoning summary text done.
275    #[serde(rename = "response.reasoning_summary_text.done")]
276    ReasoningSummaryTextDone {
277        /// Index of the output item.
278        output_index: i64,
279        /// Index of the summary part.
280        #[serde(default)]
281        summary_index: Option<i64>,
282        /// The complete summary text.
283        text: String,
284    },
285
286    // -- Error event --
287    /// Error event.
288    #[serde(rename = "error")]
289    ResponseError(ResponseErrorEvent),
290
291    // -- Catch-all for unknown/new event types --
292    /// Unknown event type. Contains the raw JSON data for forward compatibility.
293    #[serde(untagged)]
294    Other(serde_json::Value),
295}
296
297impl ResponseStreamEvent {
298    /// Get the event type string.
299    pub fn event_type(&self) -> &str {
300        match self {
301            Self::ResponseCreated { .. } => "response.created",
302            Self::ResponseInProgress { .. } => "response.in_progress",
303            Self::ResponseCompleted { .. } => "response.completed",
304            Self::ResponseFailed { .. } => "response.failed",
305            Self::ResponseIncomplete { .. } => "response.incomplete",
306            Self::ResponseOutputItemAdded { .. } => "response.output_item.added",
307            Self::OutputItemDone { .. } => "response.output_item.done",
308            Self::ContentPartAdded { .. } => "response.content_part.added",
309            Self::ContentPartDone { .. } => "response.content_part.done",
310            Self::ResponseOutputTextDelta { .. } => "response.output_text.delta",
311            Self::OutputTextDone { .. } => "response.output_text.done",
312            Self::ResponseFunctionCallArgumentsDelta { .. } => {
313                "response.function_call_arguments.delta"
314            }
315            Self::ResponseFunctionCallArgumentsDone { .. } => {
316                "response.function_call_arguments.done"
317            }
318            Self::ResponseReasoningTextDelta { .. } => "response.reasoning_text.delta",
319            Self::ResponseReasoningSummaryTextDelta { .. } => {
320                "response.reasoning_summary_text.delta"
321            }
322            Self::ReasoningSummaryTextDone { .. } => "response.reasoning_summary_text.done",
323            Self::ResponseError { .. } => "error",
324            Self::Other(v) => v.get("type").and_then(|t| t.as_str()).unwrap_or("unknown"),
325        }
326    }
327}