Skip to main content

async_openai/types/realtime/
response.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::types::{
6    realtime::{
7        MaxOutputTokens, RealtimeAudioFormats, RealtimeConversationItem, RealtimeTool,
8        RealtimeVoice, ToolChoice,
9    },
10    responses::Prompt,
11};
12
13#[derive(Debug, Serialize, Deserialize, Clone, Default)]
14pub struct RealtimeResponseUsage {
15    /// Details about the input tokens used in the Response. Cached tokens are tokens from previous
16    /// turns in the conversation that are included as context for the current response. Cached tokens
17    /// here are counted as a subset of input tokens, meaning input tokens will include cached and
18    /// uncached tokens.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub input_token_details: Option<InputTokenDetails>,
21
22    /// The number of input tokens used in the Response, including text and audio tokens.
23    pub input_tokens: u32,
24
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub output_token_details: Option<OutputTokenDetails>,
27
28    /// The number of output tokens sent in the Response, including text and audio tokens.
29    pub output_tokens: u32,
30
31    /// The total number of tokens in the Response including input and output text and audio tokens.
32    pub total_tokens: u32,
33}
34
35#[derive(Debug, Serialize, Deserialize, Clone, Default)]
36pub struct InputTokenDetails {
37    /// The number of audio tokens used as input for the Response.
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub audio_tokens: Option<u32>,
40    /// The number of cached tokens used as input for the Response.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub cached_tokens: Option<u32>,
43
44    /// Details about the cached tokens used as input for the Response.
45    pub cached_token_details: Option<CachedTokenDetails>,
46
47    /// The number of image tokens used as input for the Response.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub image_tokens: Option<u32>,
50
51    /// The number of text tokens used as input for the Response.
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub text_tokens: Option<u32>,
54}
55
56#[derive(Debug, Serialize, Deserialize, Clone, Default)]
57pub struct CachedTokenDetails {
58    /// The number of cached audio tokens used as input for the Response.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub audio_tokens: Option<u32>,
61
62    /// The number of cached image tokens used as input for the Response.
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub image_tokens: Option<u32>,
65
66    /// The number of cached text tokens used as input for the Response.
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub text_tokens: Option<u32>,
69}
70
71#[derive(Debug, Serialize, Deserialize, Clone, Default)]
72pub struct OutputTokenDetails {
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub text_tokens: Option<u32>,
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub audio_tokens: Option<u32>,
77}
78
79#[derive(Debug, Serialize, Deserialize, Clone)]
80#[serde(rename_all = "snake_case")]
81pub enum RealtimeResponseStatus {
82    InProgress,
83    Completed,
84    Cancelled,
85    Failed,
86    Incomplete,
87}
88
89#[derive(Debug, Serialize, Deserialize, Clone, Default)]
90pub struct Error {
91    pub code: String,
92    pub r#type: String,
93    /// A human-readable error message.
94    #[serde(skip_serializing_if = "Option::is_none")]
95    pub message: Option<String>,
96}
97
98#[derive(Debug, Serialize, Deserialize, Clone)]
99#[serde(rename_all = "lowercase")]
100pub enum RealtimeResponseStatusDetailType {
101    Completed,
102    Cancelled,
103    Incomplete,
104    Failed,
105}
106
107#[derive(Debug, Serialize, Deserialize, Clone)]
108#[serde(rename_all = "snake_case")]
109pub enum RealtimeResponseStatusDetailReason {
110    TurnDetected,
111    ClientCancelled,
112    MaxOutputTokens,
113    ContentFilter,
114}
115
116#[derive(Debug, Serialize, Deserialize, Clone)]
117pub struct RealtimeResponseStatusDetail {
118    /// A description of the error that caused the response to fail, populated when the status is failed.
119    pub error: Option<Error>,
120    /// The reason the Response did not complete. For a `cancelled` Response, one of `turn_detected`
121    /// (the server VAD detected a new start of speech) or `client_cancelled` (the client sent a cancel
122    /// event). For an incomplete Response, one of `max_output_tokens` or `content_filter` (the
123    ///  server-side safety filter activated and cut off the response).
124    pub reason: Option<RealtimeResponseStatusDetailReason>,
125    /// The type of error that caused the response to fail, corresponding with the `status`
126    /// field (`completed`, `cancelled`, `incomplete`, `failed`).
127    pub r#type: RealtimeResponseStatusDetailType,
128}
129
130#[derive(Debug, Serialize, Deserialize, Clone)]
131pub struct ResponseAudioOutput {
132    /// The format of the output audio.
133    pub format: RealtimeAudioFormats,
134
135    /// The voice the model uses to respond. Supported built-in voices are `alloy`, `ash`, `ballad`,
136    /// `coral`, `echo`, `sage`, `shimmer`, `verse`, `marin`, and `cedar`. Voice cannot be changed during
137    /// the session once the model has responded with audio at least once. We recommend `marin` and `cedar`
138    /// for best quality.
139    pub voice: RealtimeVoice,
140}
141
142#[derive(Debug, Serialize, Deserialize, Clone)]
143pub struct ResponseAudio {
144    /// Configuration for audio output.
145    pub output: ResponseAudioOutput,
146}
147
148#[derive(Debug, Serialize, Deserialize, Clone, Default)]
149#[serde(rename_all = "lowercase")]
150pub enum Conversation {
151    #[default]
152    Auto,
153    None,
154}
155
156/// The response resource.
157#[derive(Debug, Serialize, Deserialize, Clone)]
158pub struct RealtimeResponseCreateParams {
159    /// Configuration for audio input and output.
160    pub audio: ResponseAudio,
161
162    /// Controls which conversation the response is added to. Currently supports auto and none,
163    /// with auto as the default value. The auto value means that the contents of the response
164    /// will be added to the default conversation. Set this to none to create an out-of-band
165    /// response which will not add items to default conversation.
166    pub conversation: Conversation,
167
168    /// Input items to include in the prompt for the model. Using this field creates a new context
169    /// for this Response instead of using the default conversation. An empty array `[]` will clear
170    /// the context for this Response. Note that this can include references to items that
171    /// previously appeared in the session using their id.
172    pub input: Vec<crate::types::realtime::RealtimeResponseInputItem>,
173
174    /// The default system instructions (i.e. system message) prepended to model calls.
175    /// This field allows the client to guide the model on desired responses.
176    /// The model can be instructed on response content and format, (e.g. "be extremely succinct",
177    /// "act friendly", "here are examples of good responses") and on audio behavior
178    /// (e.g. "talk quickly", "inject emotion into your voice", "laugh frequently").
179    /// The instructions are not guaranteed to be followed by the model, but they provide
180    /// guidance to the model on the desired behavior. Note that the server sets default
181    /// instructions which will be used if this field is not set and are visible in
182    /// the `session.created` event at the start of the session.
183    pub instructions: String,
184
185    /// Maximum number of output tokens for a single assistant response, inclusive of tool calls.
186    /// Provide an integer between 1 and 4096 to limit output tokens, or inf for the maximum
187    /// available tokens for a given model. Defaults to `inf`.
188    pub max_output_tokens: MaxOutputTokens,
189
190    /// Set of 16 key-value pairs that can be attached to an object. This can be useful for
191    /// storing additional information about the object in a structured format, and querying
192    /// for objects via API or the dashboard.
193    ///
194    /// Keys are strings with a maximum length of 64 characters. Values are strings with a
195    /// maximum length of 512 characters.
196    #[serde(skip_serializing_if = "Option::is_none")]
197    pub metadata: Option<serde_json::Value>,
198
199    /// The set of modalities the model used to respond, currently the only possible values
200    /// are [\"audio\"], [\"text\"]. Audio output always include a text transcript.
201    /// Setting the output to mode `text` will disable audio output from the model.
202    pub output_modalities: Vec<String>,
203
204    /// Reference to a prompt template and its variables.
205    /// [Learn more](https://platform.openai.com/docs/guides/text?api-mode=responses#reusable-prompts).
206    #[serde(skip_serializing_if = "Option::is_none")]
207    pub prompt: Option<Prompt>,
208
209    /// How the model chooses tools. Provide one of the string modes or force a specific
210    /// function/MCP tool.
211    #[serde(skip_serializing_if = "Option::is_none")]
212    pub tool_choice: Option<ToolChoice>,
213
214    /// Tools available to the model.
215    #[serde(skip_serializing_if = "Option::is_none")]
216    pub tools: Option<Vec<RealtimeTool>>,
217
218    /// Whether the model may call multiple tools in parallel. Only honored for reasoning-capable
219    /// Realtime models such as `gpt-realtime-2`.
220    #[serde(skip_serializing_if = "Option::is_none")]
221    pub parallel_tool_calls: Option<bool>,
222
223    /// Configuration for reasoning-capable Realtime models such as `gpt-realtime-2`.
224    #[serde(skip_serializing_if = "Option::is_none")]
225    pub reasoning: Option<crate::types::realtime::RealtimeReasoning>,
226}
227
228#[derive(Debug, Serialize, Deserialize, Clone)]
229pub struct RealtimeResponse {
230    /// Configuration for audio output.
231    pub audio: Option<ResponseAudio>,
232
233    /// Which conversation the response is added to, determined by the `conversation` field in the
234    /// `response.create` event. If `auto`, the response will be added to the default conversation
235    /// and the value of `conversation_id` will be an id like `conv_1234`. If `none`, the response
236    /// will not be added to any conversation and the value of `conversation_id` will be `null`.
237    /// If responses are being triggered automatically by VAD the response will be added to the
238    /// default conversation.
239    #[serde(skip_serializing_if = "Option::is_none")]
240    pub conversation_id: Option<String>,
241
242    /// The unique ID of the response, will look like `resp_1234`.
243    pub id: String,
244
245    /// Maximum number of output tokens for a single assistant response, inclusive of tool calls,
246    /// that was used in this response.
247    pub max_output_tokens: MaxOutputTokens,
248
249    /// Set of 16 key-value pairs that can be attached to an object. This can be useful for
250    /// storing additional information about the object in a structured format, and querying
251    /// for objects via API or the dashboard.
252    ///
253    /// Keys are strings with a maximum length of 64 characters. Values are strings with a
254    /// maximum length of 512 characters.
255    #[serde(skip_serializing_if = "Option::is_none")]
256    pub metadata: Option<HashMap<String, String>>,
257
258    /// The object type, must be "realtime.response".
259    pub object: String,
260
261    /// The list of output items generated by the response.
262    pub output: Vec<RealtimeConversationItem>,
263
264    /// The set of modalities the model used to respond, currently the only possible values
265    /// are [\"audio\"], [\"text\"]. Audio output always include a text transcript.
266    /// Setting the output to mode `text` will disable audio output from the model.
267    pub output_modalities: Vec<String>,
268
269    /// The final status of the response (`completed`, `cancelled`, `failed`, or `incomplete`, `in_progress`).
270    pub status: RealtimeResponseStatus,
271
272    /// Additional details about the status.
273    pub status_details: Option<RealtimeResponseStatusDetail>,
274
275    /// Usage statistics for the Response, this will correspond to billing. A Realtime API session
276    /// will maintain a conversation context and append new Items to the Conversation, thus output
277    /// from previous turns (text and audio tokens) will become the input for later turns.
278    pub usage: Option<RealtimeResponseUsage>,
279}