Skip to main content

openai_interface/realtime/
transcription_sessions.rs

1//! Create a Realtime transcription session via
2//! `POST /realtime/transcription_sessions`.
3//!
4//! > ![warn] This module is untested!
5//! > If you encounter any issues, please report them on the repository.
6//!
7//! Transcription sessions are the speech-to-text variant of Realtime
8//! sessions: the resulting ephemeral token connects a client straight
9//! to the transcription WebSocket, with no model response generation.
10
11use serde::{Deserialize, Serialize};
12use url::Url;
13
14use crate::{
15    errors::OapiError,
16    realtime::RealtimeSession,
17    rest::post::{Post, PostNoStream},
18};
19
20/// Creates a Realtime transcription session.
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct CreateTranscriptionSessionRequest {
23    /// The session configuration, as raw JSON (e.g.
24    /// `{"input_audio_format": "pcm16",
25    /// "input_audio_transcription": {"model": "gpt-4o-transcribe"}}`).
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub session: Option<serde_json::Value>,
28    /// Additional JSON properties flattened into the request body, for
29    /// fields not covered by the typed struct.
30    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
31    pub extra_body_map: Option<serde_json::Map<String, serde_json::Value>>,
32}
33
34impl Post for CreateTranscriptionSessionRequest {
35    fn is_streaming(&self) -> bool {
36        false
37    }
38
39    /// Builds the URL for the request.
40    ///
41    /// `base_url` should be like <https://api.openai.com/v1>
42    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
43        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
44        url.path_segments_mut()
45            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
46            .push("realtime")
47            .push("transcription_sessions");
48        Ok(url.to_string())
49    }
50}
51
52impl PostNoStream for CreateTranscriptionSessionRequest {
53    type Response = RealtimeSession;
54}