async_openai/types/responses/
stream.rs

1use futures::Stream;
2use serde::{Deserialize, Serialize};
3use std::pin::Pin;
4
5use crate::{
6    error::OpenAIError,
7    types::responses::{OutputContent, OutputItem, Response, ResponseLogProb, SummaryPart},
8};
9
10/// Stream of response events
11pub type ResponseStream =
12    Pin<Box<dyn Stream<Item = Result<ResponseStreamEvent, OpenAIError>> + Send>>;
13
14/// Event types for streaming responses from the Responses API
15#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
16#[serde(tag = "type")]
17pub enum ResponseStreamEvent {
18    /// An event that is emitted when a response is created.
19    #[serde(rename = "response.created")]
20    ResponseCreated(ResponseCreatedEvent),
21    /// Emitted when the response is in progress.
22    #[serde(rename = "response.in_progress")]
23    ResponseInProgress(ResponseInProgressEvent),
24    /// Emitted when the model response is complete.
25    #[serde(rename = "response.completed")]
26    ResponseCompleted(ResponseCompletedEvent),
27    /// An event that is emitted when a response fails.
28    #[serde(rename = "response.failed")]
29    ResponseFailed(ResponseFailedEvent),
30    /// An event that is emitted when a response finishes as incomplete.
31    #[serde(rename = "response.incomplete")]
32    ResponseIncomplete(ResponseIncompleteEvent),
33    /// Emitted when a new output item is added.
34    #[serde(rename = "response.output_item.added")]
35    ResponseOutputItemAdded(ResponseOutputItemAddedEvent),
36    /// Emitted when an output item is marked done.
37    #[serde(rename = "response.output_item.done")]
38    ResponseOutputItemDone(ResponseOutputItemDoneEvent),
39    /// Emitted when a new content part is added.
40    #[serde(rename = "response.content_part.added")]
41    ResponseContentPartAdded(ResponseContentPartAddedEvent),
42    /// Emitted when a content part is done.
43    #[serde(rename = "response.content_part.done")]
44    ResponseContentPartDone(ResponseContentPartDoneEvent),
45    /// Emitted when there is an additional text delta.
46    #[serde(rename = "response.output_text.delta")]
47    ResponseOutputTextDelta(ResponseTextDeltaEvent),
48    /// Emitted when text content is finalized.
49    #[serde(rename = "response.output_text.done")]
50    ResponseOutputTextDone(ResponseTextDoneEvent),
51    /// Emitted when there is a partial refusal text.
52    #[serde(rename = "response.refusal.delta")]
53    ResponseRefusalDelta(ResponseRefusalDeltaEvent),
54    #[serde(rename = "response.refusal.done")]
55    /// Emitted when refusal text is finalized.
56    ResponseRefusalDone(ResponseRefusalDoneEvent),
57    /// Emitted when there is a partial function-call arguments delta.
58    #[serde(rename = "response.function_call_arguments.delta")]
59    ResponseFunctionCallArgumentsDelta(ResponseFunctionCallArgumentsDeltaEvent),
60    /// Emitted when function-call arguments are finalized.
61    #[serde(rename = "response.function_call_arguments.done")]
62    ResponseFunctionCallArgumentsDone(ResponseFunctionCallArgumentsDoneEvent),
63    /// Emitted when a file search call is initiated.
64    #[serde(rename = "response.file_search_call.in_progress")]
65    ResponseFileSearchCallInProgress(ResponseFileSearchCallInProgressEvent),
66    /// Emitted when a file search is currently searching.
67    #[serde(rename = "response.file_search_call.searching")]
68    ResponseFileSearchCallSearching(ResponseFileSearchCallSearchingEvent),
69    /// Emitted when a file search call is completed (results found).
70    #[serde(rename = "response.file_search_call.completed")]
71    ResponseFileSearchCallCompleted(ResponseFileSearchCallCompletedEvent),
72    /// Emitted when a web search call is initiated.
73    #[serde(rename = "response.web_search_call.in_progress")]
74    ResponseWebSearchCallInProgress(ResponseWebSearchCallInProgressEvent),
75    /// Emitted when a web search call is executing.
76    #[serde(rename = "response.web_search_call.searching")]
77    ResponseWebSearchCallSearching(ResponseWebSearchCallSearchingEvent),
78    /// Emitted when a web search call is completed.
79    #[serde(rename = "response.web_search_call.completed")]
80    ResponseWebSearchCallCompleted(ResponseWebSearchCallCompletedEvent),
81    /// Emitted when a new reasoning summary part is added.
82    #[serde(rename = "response.reasoning_summary_part.added")]
83    ResponseReasoningSummaryPartAdded(ResponseReasoningSummaryPartAddedEvent),
84    /// Emitted when a reasoning summary part is completed.
85    #[serde(rename = "response.reasoning_summary_part.done")]
86    ResponseReasoningSummaryPartDone(ResponseReasoningSummaryPartDoneEvent),
87    /// Emitted when a delta is added to a reasoning summary text.
88    #[serde(rename = "response.reasoning_summary_text.delta")]
89    ResponseReasoningSummaryTextDelta(ResponseReasoningSummaryTextDeltaEvent),
90    /// Emitted when a reasoning summary text is completed.
91    #[serde(rename = "response.reasoning_summary_text.done")]
92    ResponseReasoningSummaryTextDone(ResponseReasoningSummaryTextDoneEvent),
93    /// Emitted when a delta is added to a reasoning text.
94    #[serde(rename = "response.reasoning_text.delta")]
95    ResponseReasoningTextDelta(ResponseReasoningTextDeltaEvent),
96    /// Emitted when a reasoning text is completed.
97    #[serde(rename = "response.reasoning_text.done")]
98    ResponseReasoningTextDone(ResponseReasoningTextDoneEvent),
99    /// Emitted when an image generation tool call has completed and the final image is available.
100    #[serde(rename = "response.image_generation_call.completed")]
101    ResponseImageGenerationCallCompleted(ResponseImageGenCallCompletedEvent),
102    /// Emitted when an image generation tool call is actively generating an image (intermediate state).
103    #[serde(rename = "response.image_generation_call.generating")]
104    ResponseImageGenerationCallGenerating(ResponseImageGenCallGeneratingEvent),
105    /// Emitted when an image generation tool call is in progress.
106    #[serde(rename = "response.image_generation_call.in_progress")]
107    ResponseImageGenerationCallInProgress(ResponseImageGenCallInProgressEvent),
108    /// Emitted when a partial image is available during image generation streaming.
109    #[serde(rename = "response.image_generation_call.partial_image")]
110    ResponseImageGenerationCallPartialImage(ResponseImageGenCallPartialImageEvent),
111    /// Emitted when there is a delta (partial update) to the arguments of an MCP tool call.
112    #[serde(rename = "response.mcp_call_arguments.delta")]
113    ResponseMCPCallArgumentsDelta(ResponseMCPCallArgumentsDeltaEvent),
114    /// Emitted when the arguments for an MCP tool call are finalized.
115    #[serde(rename = "response.mcp_call_arguments.done")]
116    ResponseMCPCallArgumentsDone(ResponseMCPCallArgumentsDoneEvent),
117    /// Emitted when an MCP tool call has completed successfully.
118    #[serde(rename = "response.mcp_call.completed")]
119    ResponseMCPCallCompleted(ResponseMCPCallCompletedEvent),
120    /// Emitted when an MCP tool call has failed.
121    #[serde(rename = "response.mcp_call.failed")]
122    ResponseMCPCallFailed(ResponseMCPCallFailedEvent),
123    /// Emitted when an MCP tool call is in progress.
124    #[serde(rename = "response.mcp_call.in_progress")]
125    ResponseMCPCallInProgress(ResponseMCPCallInProgressEvent),
126    /// Emitted when the list of available MCP tools has been successfully retrieved.
127    #[serde(rename = "response.mcp_list_tools.completed")]
128    ResponseMCPListToolsCompleted(ResponseMCPListToolsCompletedEvent),
129    /// Emitted when the attempt to list available MCP tools has failed.
130    #[serde(rename = "response.mcp_list_tools.failed")]
131    ResponseMCPListToolsFailed(ResponseMCPListToolsFailedEvent),
132    /// Emitted when the system is in the process of retrieving the list of available MCP tools.
133    #[serde(rename = "response.mcp_list_tools.in_progress")]
134    ResponseMCPListToolsInProgress(ResponseMCPListToolsInProgressEvent),
135    /// Emitted when a code interpreter call is in progress.
136    #[serde(rename = "response.code_interpreter_call.in_progress")]
137    ResponseCodeInterpreterCallInProgress(ResponseCodeInterpreterCallInProgressEvent),
138    /// Emitted when the code interpreter is actively interpreting the code snippet.
139    #[serde(rename = "response.code_interpreter_call.interpreting")]
140    ResponseCodeInterpreterCallInterpreting(ResponseCodeInterpreterCallInterpretingEvent),
141    /// Emitted when the code interpreter call is completed.
142    #[serde(rename = "response.code_interpreter_call.completed")]
143    ResponseCodeInterpreterCallCompleted(ResponseCodeInterpreterCallCompletedEvent),
144    /// Emitted when a partial code snippet is streamed by the code interpreter.
145    #[serde(rename = "response.code_interpreter_call_code.delta")]
146    ResponseCodeInterpreterCallCodeDelta(ResponseCodeInterpreterCallCodeDeltaEvent),
147    /// Emitted when the code snippet is finalized by the code interpreter.
148    #[serde(rename = "response.code_interpreter_call_code.done")]
149    ResponseCodeInterpreterCallCodeDone(ResponseCodeInterpreterCallCodeDoneEvent),
150    /// Emitted when an annotation is added to output text content.
151    #[serde(rename = "response.output_text.annotation.added")]
152    ResponseOutputTextAnnotationAdded(ResponseOutputTextAnnotationAddedEvent),
153    /// Emitted when a response is queued and waiting to be processed.
154    #[serde(rename = "response.queued")]
155    ResponseQueued(ResponseQueuedEvent),
156    /// Event representing a delta (partial update) to the input of a custom tool call.
157    #[serde(rename = "response.custom_tool_call_input.delta")]
158    ResponseCustomToolCallInputDelta(ResponseCustomToolCallInputDeltaEvent),
159    /// Event indicating that input for a custom tool call is complete.
160    #[serde(rename = "response.custom_tool_call_input.done")]
161    ResponseCustomToolCallInputDone(ResponseCustomToolCallInputDoneEvent),
162    /// Emitted when an error occurs.
163    #[serde(rename = "error")]
164    ResponseError(ResponseErrorEvent),
165}
166
167#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
168pub struct ResponseCreatedEvent {
169    pub sequence_number: u64,
170    pub response: Response,
171}
172
173#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
174pub struct ResponseInProgressEvent {
175    pub sequence_number: u64,
176    pub response: Response,
177}
178
179#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
180pub struct ResponseCompletedEvent {
181    pub sequence_number: u64,
182    pub response: Response,
183}
184
185#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
186pub struct ResponseFailedEvent {
187    pub sequence_number: u64,
188    pub response: Response,
189}
190
191#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
192pub struct ResponseIncompleteEvent {
193    pub sequence_number: u64,
194    pub response: Response,
195}
196
197#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
198pub struct ResponseOutputItemAddedEvent {
199    pub sequence_number: u64,
200    pub output_index: u32,
201    pub item: OutputItem,
202}
203
204#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
205pub struct ResponseOutputItemDoneEvent {
206    pub sequence_number: u64,
207    pub output_index: u32,
208    pub item: OutputItem,
209}
210
211#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
212pub struct ResponseContentPartAddedEvent {
213    pub sequence_number: u64,
214    pub item_id: String,
215    pub output_index: u32,
216    pub content_index: u32,
217    pub part: OutputContent,
218}
219
220#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
221pub struct ResponseContentPartDoneEvent {
222    pub sequence_number: u64,
223    pub item_id: String,
224    pub output_index: u32,
225    pub content_index: u32,
226    pub part: OutputContent,
227}
228
229#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
230pub struct ResponseTextDeltaEvent {
231    pub sequence_number: u64,
232    pub item_id: String,
233    pub output_index: u32,
234    pub content_index: u32,
235    pub delta: String,
236    #[serde(default, skip_serializing_if = "Option::is_none")]
237    pub logprobs: Option<Vec<ResponseLogProb>>,
238}
239
240#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
241pub struct ResponseTextDoneEvent {
242    pub sequence_number: u64,
243    pub item_id: String,
244    pub output_index: u32,
245    pub content_index: u32,
246    pub text: String,
247    pub logprobs: Option<Vec<ResponseLogProb>>,
248}
249
250#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
251pub struct ResponseRefusalDeltaEvent {
252    pub sequence_number: u64,
253    pub item_id: String,
254    pub output_index: u32,
255    pub content_index: u32,
256    pub delta: String,
257}
258
259#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
260pub struct ResponseRefusalDoneEvent {
261    pub sequence_number: u64,
262    pub item_id: String,
263    pub output_index: u32,
264    pub content_index: u32,
265    pub refusal: String,
266}
267
268#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
269pub struct ResponseFunctionCallArgumentsDeltaEvent {
270    pub sequence_number: u64,
271    pub item_id: String,
272    pub output_index: u32,
273    pub delta: String,
274}
275
276#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
277pub struct ResponseFunctionCallArgumentsDoneEvent {
278    pub name: String,
279    pub sequence_number: u64,
280    pub item_id: String,
281    pub output_index: u32,
282    pub arguments: String,
283}
284
285#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
286pub struct ResponseFileSearchCallInProgressEvent {
287    pub sequence_number: u64,
288    pub output_index: u32,
289    pub item_id: String,
290}
291
292#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
293pub struct ResponseFileSearchCallSearchingEvent {
294    pub sequence_number: u64,
295    pub output_index: u32,
296    pub item_id: String,
297}
298
299#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
300pub struct ResponseFileSearchCallCompletedEvent {
301    pub sequence_number: u64,
302    pub output_index: u32,
303    pub item_id: String,
304}
305
306#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
307pub struct ResponseWebSearchCallInProgressEvent {
308    pub sequence_number: u64,
309    pub output_index: u32,
310    pub item_id: String,
311}
312
313#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
314pub struct ResponseWebSearchCallSearchingEvent {
315    pub sequence_number: u64,
316    pub output_index: u32,
317    pub item_id: String,
318}
319
320#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
321pub struct ResponseWebSearchCallCompletedEvent {
322    pub sequence_number: u64,
323    pub output_index: u32,
324    pub item_id: String,
325}
326
327#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
328pub struct ResponseReasoningSummaryPartAddedEvent {
329    pub sequence_number: u64,
330    pub item_id: String,
331    pub output_index: u32,
332    pub summary_index: u32,
333    pub part: SummaryPart,
334}
335
336#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
337pub struct ResponseReasoningSummaryPartDoneEvent {
338    pub sequence_number: u64,
339    pub item_id: String,
340    pub output_index: u32,
341    pub summary_index: u32,
342    pub part: SummaryPart,
343}
344
345#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
346pub struct ResponseReasoningSummaryTextDeltaEvent {
347    pub sequence_number: u64,
348    pub item_id: String,
349    pub output_index: u32,
350    pub summary_index: u32,
351    pub delta: String,
352}
353
354#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
355pub struct ResponseReasoningSummaryTextDoneEvent {
356    pub sequence_number: u64,
357    pub item_id: String,
358    pub output_index: u32,
359    pub summary_index: u32,
360    pub text: String,
361}
362
363#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
364pub struct ResponseReasoningTextDeltaEvent {
365    pub sequence_number: u64,
366    pub item_id: String,
367    pub output_index: u32,
368    pub content_index: u32,
369    pub delta: String,
370}
371
372#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
373pub struct ResponseReasoningTextDoneEvent {
374    pub sequence_number: u64,
375    pub item_id: String,
376    pub output_index: u32,
377    pub content_index: u32,
378    pub text: String,
379}
380
381#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
382pub struct ResponseImageGenCallCompletedEvent {
383    pub sequence_number: u64,
384    pub output_index: u32,
385    pub item_id: String,
386}
387
388#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
389pub struct ResponseImageGenCallGeneratingEvent {
390    pub sequence_number: u64,
391    pub output_index: u32,
392    pub item_id: String,
393}
394
395#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
396pub struct ResponseImageGenCallInProgressEvent {
397    pub sequence_number: u64,
398    pub output_index: u32,
399    pub item_id: String,
400}
401
402#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
403pub struct ResponseImageGenCallPartialImageEvent {
404    pub sequence_number: u64,
405    pub output_index: u32,
406    pub item_id: String,
407    pub partial_image_index: u32,
408    pub partial_image_b64: String,
409}
410
411#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
412pub struct ResponseMCPCallArgumentsDeltaEvent {
413    pub sequence_number: u64,
414    pub output_index: u32,
415    pub item_id: String,
416    pub delta: String,
417}
418
419#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
420pub struct ResponseMCPCallArgumentsDoneEvent {
421    pub sequence_number: u64,
422    pub output_index: u32,
423    pub item_id: String,
424    pub arguments: String,
425}
426
427#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
428pub struct ResponseMCPCallCompletedEvent {
429    pub sequence_number: u64,
430    pub output_index: u32,
431    pub item_id: String,
432}
433
434#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
435pub struct ResponseMCPCallFailedEvent {
436    pub sequence_number: u64,
437    pub output_index: u32,
438    pub item_id: String,
439}
440
441#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
442pub struct ResponseMCPCallInProgressEvent {
443    pub sequence_number: u64,
444    pub output_index: u32,
445    pub item_id: String,
446}
447
448#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
449pub struct ResponseMCPListToolsCompletedEvent {
450    pub sequence_number: u64,
451    pub output_index: u32,
452    pub item_id: String,
453}
454
455#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
456pub struct ResponseMCPListToolsFailedEvent {
457    pub sequence_number: u64,
458    pub output_index: u32,
459    pub item_id: String,
460}
461
462#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
463pub struct ResponseMCPListToolsInProgressEvent {
464    pub sequence_number: u64,
465    pub output_index: u32,
466    pub item_id: String,
467}
468
469#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
470pub struct ResponseCodeInterpreterCallInProgressEvent {
471    pub sequence_number: u64,
472    pub output_index: u32,
473    pub item_id: String,
474}
475
476#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
477pub struct ResponseCodeInterpreterCallInterpretingEvent {
478    pub sequence_number: u64,
479    pub output_index: u32,
480    pub item_id: String,
481}
482
483#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
484pub struct ResponseCodeInterpreterCallCompletedEvent {
485    pub sequence_number: u64,
486    pub output_index: u32,
487    pub item_id: String,
488}
489
490#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
491pub struct ResponseCodeInterpreterCallCodeDeltaEvent {
492    pub sequence_number: u64,
493    pub output_index: u32,
494    pub item_id: String,
495    pub delta: String,
496}
497
498#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
499pub struct ResponseCodeInterpreterCallCodeDoneEvent {
500    pub sequence_number: u64,
501    pub output_index: u32,
502    pub item_id: String,
503    pub code: String,
504}
505
506#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
507pub struct ResponseOutputTextAnnotationAddedEvent {
508    pub sequence_number: u64,
509    pub output_index: u32,
510    pub content_index: u32,
511    pub annotation_index: u32,
512    pub item_id: String,
513    pub annotation: serde_json::Value,
514}
515
516#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
517pub struct ResponseQueuedEvent {
518    pub sequence_number: u64,
519    pub response: Response,
520}
521
522#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
523pub struct ResponseCustomToolCallInputDeltaEvent {
524    pub sequence_number: u64,
525    pub output_index: u32,
526    pub item_id: String,
527    pub delta: String,
528}
529
530#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
531pub struct ResponseCustomToolCallInputDoneEvent {
532    pub sequence_number: u64,
533    pub output_index: u32,
534    pub item_id: String,
535    pub input: String,
536}
537
538#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
539pub struct ResponseErrorEvent {
540    pub sequence_number: u64,
541    pub code: Option<String>,
542    pub message: String,
543    pub param: Option<String>,
544}