Skip to main content

OpenAI OpenAI TTS

The OpenAITTSService provides high-quality, natural-sounding voice synthesis using OpenAI's state-of-the-art TTS models. It is seamlessly integrated with the OpenAI ecosystem, making it a perfect choice for agents already leveraging GPT models.

Installation

To use OpenAI TTS, install the required dependencies:

pip install "piopiy-ai[openai]"

Prerequisites

  • An OpenAI account and API key (Get yours here).
  • Set your API key in your environment:
    export OPENAI_API_KEY="your_api_key_here"

Configuration

OpenAITTSService Parameters

ParameterTypeDefaultDescription
api_keystrNoneYour OpenAI API key (defaults to env var).
base_urlstrNoneCustom base URL for OpenAI API.
voicestr"alloy"Voice ID (alloy, echo, fable, onyx, nova, shimmer).
modelstr"gpt-4o-mini-tts"TTS model ID.
sample_rateintNoneOutput audio sample rate (OpenAI defaults to 24kHz).
paramsInputParamsNoneAdvanced synthesis controls.

InputParams

ParameterTypeDefaultDescription
instructionsstrNoneInstructions to guide voice synthesis behavior.
speedfloatNoneVoice speed control (0.25 to 4.0).

Usage

Basic Setup

import os
from piopiy.services.openai.tts import OpenAITTSService

tts = OpenAITTSService(
api_key=os.getenv("OPENAI_API_KEY"),
voice="nova",
model="gpt-4o-mini-tts"
)

With Speed Control

from piopiy.services.openai.tts import OpenAITTSService

tts = OpenAITTSService(
voice="shimmer",
params=OpenAITTSService.InputParams(
speed=1.1
)
)

Notes

  • Sample Rate: OpenAI TTS always outputs at 24kHz. Piopiy handles any necessary resampling for your specific transport.
  • Latency: The gpt-4o-mini-tts model is highly optimized for performance, providing sub-second TTFB for real-time conversations.
  • Streaming: This service uses OpenAI's streaming response to provide audio units as they are generated.