async_openai/types/audio/
stream.rs

1use serde::{Deserialize, Serialize};
2
3use crate::types::audio::{LogProbProperties, TranscriptTextUsageTokens};
4
5#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
6#[serde(tag = "type", rename_all = "snake_case")]
7pub enum CreateSpeechResponseStreamEvent {
8    /// Emitted for each chunk of audio data generated during speech synthesis.
9    #[serde(rename = "speech.audio.delta")]
10    SpeechAudioDelta(SpeechAudioDeltaEvent),
11    /// Emitted when the speech synthesis is complete and all audio has been streamed.
12    #[serde(rename = "speech.audio.done")]
13    SpeechAudioDone(SpeechAudioDoneEvent),
14}
15
16#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
17pub struct SpeechAudioDeltaEvent {
18    /// A chunk of Base64-encoded audio data.
19    pub audio: String,
20}
21
22#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
23pub struct SpeechUsage {
24    /// Number of input tokens in the prompt.
25    pub input_tokens: u32,
26    /// Number of output tokens generated.
27    pub output_tokens: u32,
28    /// Total number of tokens used (input + output).
29    pub total_tokens: u32,
30}
31
32#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
33pub struct SpeechAudioDoneEvent {
34    /// Token usage statistics for the request.
35    pub usage: SpeechUsage,
36}
37
38#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
39pub struct TranscriptionTextSegmentEvent {
40    /// Unique identifier for the segment.
41    pub id: String,
42    /// Start timestamp of the segment in seconds.
43    pub start: f32,
44    /// End timestamp of the segment in seconds.
45    pub end: f32,
46    /// Transcript text for this segment.
47    pub text: String,
48    /// Speaker label for this segment.
49    pub speaker: String,
50}
51
52#[derive(Debug, Serialize, Deserialize, Clone)]
53pub struct TranscriptionTextDeltaEvent {
54    /// The text delta that was additionally transcribed.
55    pub delta: String,
56    /// The log probabilities of the individual tokens in the transcription.
57    /// Only included if you [create a transcription](https://platform.openai.com/docs/api-reference/audio/create-transcription) with
58    /// the `include[]` parameter set to `logprobs`.
59    pub logprobs: Option<Vec<LogProbProperties>>,
60    /// Identifier of the diarized segment that this delta belongs to. Only present when using
61    /// `gpt-4o-transcribe-diarize`.
62    pub segment_id: Option<String>,
63}
64
65#[derive(Debug, Serialize, Deserialize, Clone)]
66pub struct TranscriptionTextDoneEvent {
67    /// The text that was transcribed.
68    pub text: String,
69    /// The log probabilities of the individual tokens in the transcription.
70    /// Only included if you [create a transcription](https://platform.openai.com/docs/api-reference/audio/create-transcription) with
71    /// the `include[]` parameter set to `logprobs`.
72    pub logprobs: Option<Vec<LogProbProperties>>,
73    /// Usage statistics for models billed by token usage.
74    pub usage: TranscriptTextUsageTokens,
75}
76
77#[derive(Debug, Serialize, Deserialize, Clone)]
78#[serde(tag = "type")]
79pub enum CreateTranscriptionResponseStreamEvent {
80    /// Emitted when a diarized transcription returns a completed segment with speaker information. Only
81    /// emitted when you [create a transcription](https://platform.openai.com/docs/api-reference/audio/create-transcription) with
82    /// `stream` set to `true` and `response_format` set to `diarized_json`.
83    #[serde(rename = "transcript.text.segment")]
84    TranscriptTextSegment(TranscriptionTextSegmentEvent),
85    #[serde(rename = "transcript.text.delta")]
86    TranscriptTextDelta(TranscriptionTextDeltaEvent),
87    /// Emitted when the transcription is complete. Contains the complete transcription text. Only emitted
88    /// when you [create a transcription](https://platform.openai.com/docs/api-reference/audio/create-transcription) with the
89    /// `Stream` parameter set to `true`.
90    #[serde(rename = "transcript.text.done")]
91    TranscriptTextDone(TranscriptionTextDoneEvent),
92}
93
94#[cfg(feature = "_api")]
95pub type TranscriptionResponseStream = std::pin::Pin<
96    Box<
97        dyn futures::Stream<
98                Item = Result<CreateTranscriptionResponseStreamEvent, crate::error::OpenAIError>,
99            > + Send,
100    >,
101>;
102
103/// Stream of response events
104#[cfg(feature = "_api")]
105pub type SpeechResponseStream = std::pin::Pin<
106    Box<
107        dyn futures::Stream<
108                Item = Result<CreateSpeechResponseStreamEvent, crate::error::OpenAIError>,
109            > + Send,
110    >,
111>;
112
113#[cfg(feature = "_api")]
114macro_rules! impl_event_type {
115    ($($ty:ty => $event_type:expr),* $(,)?) => {
116        $(
117            impl crate::traits::EventType for $ty {
118                fn event_type(&self) -> &'static str {
119                    $event_type
120                }
121            }
122        )*
123    };
124}
125
126#[cfg(feature = "_api")]
127impl_event_type! {
128    SpeechAudioDeltaEvent => "speech.audio.delta",
129    SpeechAudioDoneEvent => "speech.audio.done",
130    TranscriptionTextSegmentEvent => "transcript.text.segment",
131    TranscriptionTextDeltaEvent => "transcript.text.delta",
132    TranscriptionTextDoneEvent => "transcript.text.done",
133}
134
135#[cfg(feature = "_api")]
136impl crate::traits::EventType for CreateSpeechResponseStreamEvent {
137    fn event_type(&self) -> &'static str {
138        match self {
139            CreateSpeechResponseStreamEvent::SpeechAudioDelta(event) => event.event_type(),
140            CreateSpeechResponseStreamEvent::SpeechAudioDone(event) => event.event_type(),
141        }
142    }
143}
144
145#[cfg(feature = "_api")]
146impl crate::traits::EventType for CreateTranscriptionResponseStreamEvent {
147    fn event_type(&self) -> &'static str {
148        match self {
149            CreateTranscriptionResponseStreamEvent::TranscriptTextSegment(event) => {
150                event.event_type()
151            }
152            CreateTranscriptionResponseStreamEvent::TranscriptTextDelta(event) => {
153                event.event_type()
154            }
155            CreateTranscriptionResponseStreamEvent::TranscriptTextDone(event) => event.event_type(),
156        }
157    }
158}