oa.audio

OpenAI Audio tools

Functions

text_to_speech(text, *, input[, model, ...])

Convert text to speech using OpenAI's TTS API.

transcribe(audio, *, file[, model, ...])

Transcribe an audio or video file using OpenAI's Whisper API.

transcription_to_srt(transcription, *[, ...])

Convert a transcription dict to SRT format string.

tts(text, *, input[, model, voice, ...])

Convert text to speech using OpenAI's TTS API.

oa.audio.text_to_speech(text, *, input, model='tts-1', voice='alloy', response_format='mp3', client=None, instructions=<openai.Omit object>, speed=<openai.Omit object>, stream_format=<openai.Omit object>, extra_headers=None, extra_query=None, extra_body=None, timeout=NOT_GIVEN)[source]

Convert text to speech using OpenAI’s TTS API.

Parameters:
  • text (str) – Text to convert to speech

  • model (str) – TTS model to use

  • voice (str) – Voice to use (alloy, echo, fable, onyx, nova, shimmer)

  • response_format (str) – Audio format (‘mp3’, ‘opus’, ‘aac’, ‘flac’, ‘wav’, ‘pcm’)

  • client (Union[OpenAI, str, dict, None]) – OpenAI client instance, API key string, config dict, or None

  • kwargs – Additional parameters for the API

Returns:

Audio content as bytes

Note

Actual API calls require a valid API key

oa.audio.transcribe(audio, *, file, model='whisper-1', response_format='json', language=None, client=None, chunking_strategy=<openai.Omit object>, include=<openai.Omit object>, keywords=<openai.Omit object>, known_speaker_names=<openai.Omit object>, known_speaker_references=<openai.Omit object>, languages=<openai.Omit object>, prompt=<openai.Omit object>, stream=<openai.Omit object>, temperature=<openai.Omit object>, timestamp_granularities=<openai.Omit object>, extra_headers=None, extra_query=None, extra_body=None, timeout=NOT_GIVEN)[source]

Transcribe an audio or video file using OpenAI’s Whisper API.

Parameters:
  • audio_file_path – Path to the audio/video file to transcribe

  • model (str) – Transcription model to use

  • response_format (str) – Format for the response (‘json’, ‘text’, ‘srt’, ‘verbose_json’)

  • language (Optional[str]) – Optional ISO-639-1 language code (e.g., ‘en’, ‘fr’)

  • client (Union[OpenAI, str, dict, None]) – OpenAI client instance, API key string, config dict, or None

  • kwargs – Additional parameters for the API

Returns:

Dict with ‘text’, ‘segments’, ‘language’, and ‘raw_response’

Note

Actual API calls require a valid audio file and API key

oa.audio.transcription_to_srt(transcription, *, segment_duration=None)[source]

Convert a transcription dict to SRT format string.

Parameters:
  • transcription (dict) – Dict with ‘text’ and optionally ‘segments’

  • segment_duration (float) – If segments missing, duration per generated segment

Return type:

str

Returns:

SRT formatted string

>>> result = transcription_to_srt({'text': 'Hello world'})
>>> 'Hello world' in result
True
oa.audio.tts(text, *, input, model='tts-1', voice='alloy', response_format='mp3', client=None, instructions=<openai.Omit object>, speed=<openai.Omit object>, stream_format=<openai.Omit object>, extra_headers=None, extra_query=None, extra_body=None, timeout=NOT_GIVEN)

Convert text to speech using OpenAI’s TTS API.

Parameters:
  • text (str) – Text to convert to speech

  • model (str) – TTS model to use

  • voice (str) – Voice to use (alloy, echo, fable, onyx, nova, shimmer)

  • response_format (str) – Audio format (‘mp3’, ‘opus’, ‘aac’, ‘flac’, ‘wav’, ‘pcm’)

  • client (Union[OpenAI, str, dict, None]) – OpenAI client instance, API key string, config dict, or None

  • kwargs – Additional parameters for the API

Returns:

Audio content as bytes

Note

Actual API calls require a valid API key