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, Summary},
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)]
328#[serde(tag = "type", rename_all = "snake_case")]
329pub enum SummaryPart {
330    SummaryText(Summary),
331}
332
333#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
334pub struct ResponseReasoningSummaryPartAddedEvent {
335    pub sequence_number: u64,
336    pub item_id: String,
337    pub output_index: u32,
338    pub summary_index: u32,
339    pub part: SummaryPart,
340}
341
342#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
343pub struct ResponseReasoningSummaryPartDoneEvent {
344    pub sequence_number: u64,
345    pub item_id: String,
346    pub output_index: u32,
347    pub summary_index: u32,
348    pub part: SummaryPart,
349}
350
351#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
352pub struct ResponseReasoningSummaryTextDeltaEvent {
353    pub sequence_number: u64,
354    pub item_id: String,
355    pub output_index: u32,
356    pub summary_index: u32,
357    pub delta: String,
358}
359
360#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
361pub struct ResponseReasoningSummaryTextDoneEvent {
362    pub sequence_number: u64,
363    pub item_id: String,
364    pub output_index: u32,
365    pub summary_index: u32,
366    pub text: String,
367}
368
369#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
370pub struct ResponseReasoningTextDeltaEvent {
371    pub sequence_number: u64,
372    pub item_id: String,
373    pub output_index: u32,
374    pub content_index: u32,
375    pub delta: String,
376}
377
378#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
379pub struct ResponseReasoningTextDoneEvent {
380    pub sequence_number: u64,
381    pub item_id: String,
382    pub output_index: u32,
383    pub content_index: u32,
384    pub text: String,
385}
386
387#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
388pub struct ResponseImageGenCallCompletedEvent {
389    pub sequence_number: u64,
390    pub output_index: u32,
391    pub item_id: String,
392}
393
394#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
395pub struct ResponseImageGenCallGeneratingEvent {
396    pub sequence_number: u64,
397    pub output_index: u32,
398    pub item_id: String,
399}
400
401#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
402pub struct ResponseImageGenCallInProgressEvent {
403    pub sequence_number: u64,
404    pub output_index: u32,
405    pub item_id: String,
406}
407
408#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
409pub struct ResponseImageGenCallPartialImageEvent {
410    pub sequence_number: u64,
411    pub output_index: u32,
412    pub item_id: String,
413    pub partial_image_index: u32,
414    pub partial_image_b64: String,
415}
416
417#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
418pub struct ResponseMCPCallArgumentsDeltaEvent {
419    pub sequence_number: u64,
420    pub output_index: u32,
421    pub item_id: String,
422    pub delta: String,
423}
424
425#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
426pub struct ResponseMCPCallArgumentsDoneEvent {
427    pub sequence_number: u64,
428    pub output_index: u32,
429    pub item_id: String,
430    pub arguments: String,
431}
432
433#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
434pub struct ResponseMCPCallCompletedEvent {
435    pub sequence_number: u64,
436    pub output_index: u32,
437    pub item_id: String,
438}
439
440#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
441pub struct ResponseMCPCallFailedEvent {
442    pub sequence_number: u64,
443    pub output_index: u32,
444    pub item_id: String,
445}
446
447#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
448pub struct ResponseMCPCallInProgressEvent {
449    pub sequence_number: u64,
450    pub output_index: u32,
451    pub item_id: String,
452}
453
454#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
455pub struct ResponseMCPListToolsCompletedEvent {
456    pub sequence_number: u64,
457    pub output_index: u32,
458    pub item_id: String,
459}
460
461#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
462pub struct ResponseMCPListToolsFailedEvent {
463    pub sequence_number: u64,
464    pub output_index: u32,
465    pub item_id: String,
466}
467
468#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
469pub struct ResponseMCPListToolsInProgressEvent {
470    pub sequence_number: u64,
471    pub output_index: u32,
472    pub item_id: String,
473}
474
475#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
476pub struct ResponseCodeInterpreterCallInProgressEvent {
477    pub sequence_number: u64,
478    pub output_index: u32,
479    pub item_id: String,
480}
481
482#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
483pub struct ResponseCodeInterpreterCallInterpretingEvent {
484    pub sequence_number: u64,
485    pub output_index: u32,
486    pub item_id: String,
487}
488
489#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
490pub struct ResponseCodeInterpreterCallCompletedEvent {
491    pub sequence_number: u64,
492    pub output_index: u32,
493    pub item_id: String,
494}
495
496#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
497pub struct ResponseCodeInterpreterCallCodeDeltaEvent {
498    pub sequence_number: u64,
499    pub output_index: u32,
500    pub item_id: String,
501    pub delta: String,
502}
503
504#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
505pub struct ResponseCodeInterpreterCallCodeDoneEvent {
506    pub sequence_number: u64,
507    pub output_index: u32,
508    pub item_id: String,
509    pub code: String,
510}
511
512#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
513pub struct ResponseOutputTextAnnotationAddedEvent {
514    pub sequence_number: u64,
515    pub output_index: u32,
516    pub content_index: u32,
517    pub annotation_index: u32,
518    pub item_id: String,
519    pub annotation: serde_json::Value,
520}
521
522#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
523pub struct ResponseQueuedEvent {
524    pub sequence_number: u64,
525    pub response: Response,
526}
527
528#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
529pub struct ResponseCustomToolCallInputDeltaEvent {
530    pub sequence_number: u64,
531    pub output_index: u32,
532    pub item_id: String,
533    pub delta: String,
534}
535
536#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
537pub struct ResponseCustomToolCallInputDoneEvent {
538    pub sequence_number: u64,
539    pub output_index: u32,
540    pub item_id: String,
541    pub input: String,
542}
543
544#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
545pub struct ResponseErrorEvent {
546    pub sequence_number: u64,
547    pub code: Option<String>,
548    pub message: String,
549    pub param: Option<String>,
550}