> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pipecat.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Text-to-Speech

> OpenAITTSService provides TTS through OpenAI's speech API, supporting both traditional and GPT-4o family voice models.

## Overview

`OpenAITTSService` provides high-quality text-to-speech synthesis using OpenAI's TTS API with multiple voice models including traditional TTS models and advanced GPT-based models. The service outputs 24kHz PCM audio with streaming capabilities for real-time applications.

> [card] [OpenAI TTS API Reference](https://reference-server.pipecat.ai/en/latest/api/pipecat.services.openai.tts.html)
>
> Pipecat's API methods for OpenAI TTS integration

> [card] [Example Implementation](https://github.com/pipecat-ai/pipecat/blob/main/examples/voice/voice-openai.py)
>
> Complete example with voice customization

> [card] [OpenAI Documentation](https://platform.openai.com/docs/api-reference/audio/createSpeech)
>
> Official OpenAI TTS API documentation

> [card] [Voice Samples](https://developers.openai.com/api/docs/guides/text-to-speech)
>
> Listen to available voice options

## Installation

To use OpenAI services, install the required dependencies:

```bash theme={null}
uv add "pipecat-ai[openai]"
```

## Prerequisites

### OpenAI Account Setup

Before using OpenAI TTS services, you need:

1. **OpenAI Account**: Sign up at [OpenAI Platform](https://platform.openai.com/)
2. **API Key**: Generate an API key from your [API keys page](https://platform.openai.com/api-keys)
3. **Voice Selection**: Choose from available voice options (alloy, ash, ballad, cedar, coral, echo, fable, marin, nova, onyx, sage, shimmer, verse)

### Required Environment Variables

* `OPENAI_API_KEY`: Your OpenAI API key for authentication

## Configuration

### OpenAITTSService

**api\_key** `str` (default: None)

OpenAI API key for authentication. If `None`, uses the `OPENAI_API_KEY`
environment variable.

**base\_url** `str` (default: None)

Custom base URL for OpenAI API. If `None`, uses the default OpenAI endpoint.

**http\_client** `AsyncHTTPClient` (default: None)

Custom async HTTP client for API requests, e.g. one with a longer request
timeout. Prefer `openai.DefaultAsyncHttpxClient`, which keeps the SDK's
connection limits and redirect handling; a bare client uses its own defaults
instead. The SDK's HTTP client family follows the installed `openai` version,
so build any `Timeout` you pass it from that same family. Defaults to `None`,
which lets the SDK build its own client.

**voice** `str` *deprecated* (default: None)

*Deprecated since 0.0.105, removed in 2.0.0.* Use
`settings=OpenAITTSService.Settings(...)` instead.

**model** `str` *deprecated* (default: None)

*Deprecated since 0.0.105, removed in 2.0.0.* Use
`settings=OpenAITTSService.Settings(...)` instead.

**sample\_rate** `int` (default: None)

Output audio sample rate in Hz. If `None`, uses the pipeline's output sample
rate. OpenAI TTS only produces 24kHz audio and the service doesn't resample
it, so any other rate logs a warning and the audio plays at the wrong speed.

**instructions** `str` *deprecated* (default: None)

*Deprecated since 0.0.105, removed in 2.0.0.* Use
`settings=OpenAITTSService.Settings(instructions=...)` instead.

**speed** `float` *deprecated* (default: None)

*Deprecated since 0.0.105, removed in 2.0.0.* Use
`settings=OpenAITTSService.Settings(speed=...)` instead.

**params** `InputParams` *deprecated* (default: None)

*Deprecated since 0.0.105, removed in 2.0.0.* Use
`settings=OpenAITTSService.Settings(...)` instead.

**settings** `OpenAITTSService.Settings` (default: None)

Runtime-configurable settings. See [Settings](#settings) below.

### Settings

Runtime-configurable settings passed via the `settings` constructor argument using `OpenAITTSService.Settings(...)`. These can be updated mid-conversation with `TTSUpdateSettingsFrame`. See [Service Settings](https://docs.pipecat.ai/pipecat/fundamentals/service-settings.md) for details.

| Parameter | Type | Default | Description |
| - | - | - | - |
| `model` | `str` | `"gpt-4o-mini-tts"` | TTS model identifier. *(Inherited from base settings.)* |
| `voice` | `str` | `"alloy"` | Voice identifier: one of the voices listed under Prerequisites. Any other value yields an `ErrorFrame` and no audio. *(Inherited from base settings.)* |
| `language` | `Language \| str` | `None` | Language used to split text into sentences; not sent to OpenAI. *(Inherited from base settings.)* |
| `instructions` | `str` | `None` | Instructions to guide voice synthesis behavior (e.g. affect, tone, pacing). |
| `speed` | `float` | `None` | Voice speed control (0.25 to 4.0). |

## Usage

### Basic Setup

```python theme={null}
import os

from pipecat.services.openai.tts import OpenAITTSService

tts = OpenAITTSService(
    api_key=os.getenv("OPENAI_API_KEY"),
    settings=OpenAITTSService.Settings(
        voice="nova",
    ),
)
```

### With Voice Customization

```python theme={null}
from pipecat.services.openai.tts import OpenAITTSService

tts = OpenAITTSService(
    api_key=os.getenv("OPENAI_API_KEY"),
    settings=OpenAITTSService.Settings(
        voice="coral",
        model="gpt-4o-mini-tts",
        instructions="Speak in a warm, friendly tone with moderate pacing.",
        speed=1.1,
    ),
)
```

### Updating Settings at Runtime

Voice settings can be changed mid-conversation using `TTSUpdateSettingsFrame`:

```python theme={null}
from pipecat.frames.frames import TTSUpdateSettingsFrame
from pipecat.services.openai.tts import OpenAITTSSettings

await worker.queue_frame(
    TTSUpdateSettingsFrame(
        delta=OpenAITTSSettings(
            instructions="Now speak more formally.",
            speed=0.9,
        )
    )
)
```

## Notes

* **Model selection**: The `gpt-4o-mini-tts` model supports the `instructions` parameter for controlling voice affect and tone, which traditional TTS models do not support.
* **HTTP-based service**: OpenAI TTS uses HTTP streaming, so it does not have WebSocket connection events.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.