File size: 13,918 Bytes
9410704 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | # ElevenLabs Python Library

[](https://buildwithfern.com/?utm_source=fern-elevenlabs/elevenlabs-python/readme)
[](https://discord.gg/elevenlabs)
[](https://twitter.com/elevenlabsio)
[](https://pypi.org/project/elevenlabs/)
[](https://pepy.tech/project/elevenlabs)
The official Python SDK for [ElevenLabs](https://elevenlabs.io/). ElevenLabs brings the most compelling, rich and lifelike voices to creators and developers in just a few lines of code.
## 📖 API & Docs
Check out the [HTTP API documentation](https://elevenlabs.io/docs/api-reference).
## Install
```bash
pip install elevenlabs
```
## Usage
### Main Models
1. **Eleven v3** (`eleven_v3`)
- Dramatic delivery and performances
- 70+ languages supported
- Supported for natural multi-speaker dialogue
2. **Eleven Multilingual v2** (`eleven_multilingual_v2`)
- Excels in stability, language diversity, and accent accuracy
- Supports 29 languages
- Recommended for most use cases
3. **Eleven Flash v2.5** (`eleven_flash_v2_5`)
- Ultra-low latency
- Supports 32 languages
- Faster model, 50% lower price per character
4. **Eleven Turbo v2.5** (`eleven_turbo_v2_5`)
- Good balance of quality and latency
- Ideal for developer use cases where speed is crucial
- Supports 32 languages
For more detailed information about these models and others, visit the [ElevenLabs Models documentation](https://elevenlabs.io/docs/models).
```py
from dotenv import load_dotenv
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
load_dotenv()
elevenlabs = ElevenLabs()
audio = elevenlabs.text_to_speech.convert(
text="The first move is what sets everything in motion.",
voice_id="JBFqnCBsd6RMkjVDRZzb",
model_id="eleven_v3",
output_format="mp3_44100_128",
)
play(audio)
```
<details> <summary> Play </summary>
🎧 **Try it out!** Want to hear our voices in action? Visit the [ElevenLabs Voice Lab](https://elevenlabs.io/voice-lab) to experiment with different voices, languages, and settings.
</details>
## Voices
List all your available voices with `search()`.
```py
from elevenlabs.client import ElevenLabs
elevenlabs = ElevenLabs(
api_key="YOUR_API_KEY",
)
response = elevenlabs.voices.search()
print(response.voices)
```
For information about the structure of the voices output, please refer to the [official ElevenLabs API documentation for Get Voices](https://elevenlabs.io/docs/api-reference/get-voices).
Build a voice object with custom settings to personalize the voice style, or call
`elevenlabs.voices.settings.get("your-voice-id")` to get the default settings for the voice.
</details>
## Clone Voice
Clone your voice in an instant. Note that voice cloning requires an API key, see below.
```py
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
elevenlabs = ElevenLabs(
api_key="YOUR_API_KEY",
)
voice = elevenlabs.voices.ivc.create(
name="Alex",
description="An old American male voice with a slight hoarseness in his throat. Perfect for news", # Optional
files=["./sample_0.mp3", "./sample_1.mp3", "./sample_2.mp3"],
)
```
## Streaming
Stream audio in real-time, as it's being generated.
```py
from elevenlabs import stream
from elevenlabs.client import ElevenLabs
elevenlabs = ElevenLabs(
api_key="YOUR_API_KEY",
)
audio_stream = elevenlabs.text_to_speech.stream(
text="This is a test",
voice_id="JBFqnCBsd6RMkjVDRZzb",
model_id="eleven_multilingual_v2"
)
# option 1: play the streamed audio locally
stream(audio_stream)
# option 2: process the audio bytes manually
for chunk in audio_stream:
if isinstance(chunk, bytes):
print(chunk)
```
## Async Client
Use `AsyncElevenLabs` if you want to make API calls asynchronously.
```python
import asyncio
from elevenlabs.client import AsyncElevenLabs
elevenlabs = AsyncElevenLabs(
api_key="MY_API_KEY"
)
async def print_models() -> None:
models = await elevenlabs.models.list()
print(models)
asyncio.run(print_models())
```
## ElevenAgents
Build interactive AI agents with real-time audio capabilities using ElevenAgents.
### Basic Usage
```python
from elevenlabs.client import ElevenLabs
from elevenlabs.conversational_ai.conversation import Conversation, ClientTools
from elevenlabs.conversational_ai.default_audio_interface import DefaultAudioInterface
elevenlabs = ElevenLabs(
api_key="YOUR_API_KEY",
)
# Create audio interface for real-time audio input/output
audio_interface = DefaultAudioInterface()
# Create conversation
conversation = Conversation(
client=elevenlabs,
agent_id="your-agent-id",
requires_auth=True,
audio_interface=audio_interface,
)
# Start the conversation
conversation.start_session()
# The conversation runs in background until you call:
conversation.end_session()
```
### Custom Event Loop Support
For advanced use cases involving context propagation, resource reuse, or specific event loop management, `ClientTools` supports custom asyncio event loops:
```python
import asyncio
from elevenlabs.conversational_ai.conversation import ClientTools
elevenlabs = ElevenLabs(
api_key="YOUR_API_KEY",
)
async def main():
# Get the current event loop
custom_loop = asyncio.get_running_loop()
# Create ClientTools with custom loop to prevent "different event loop" errors
client_tools = ClientTools(loop=custom_loop)
# Register your tools
async def get_weather(params):
location = params.get("location", "Unknown")
# Your async logic here
return f"Weather in {location}: Sunny, 72°F"
client_tools.register("get_weather", get_weather, is_async=True)
# Use with conversation
conversation = Conversation(
client=elevenlabs,
agent_id="your-agent-id",
requires_auth=True,
audio_interface=audio_interface,
client_tools=client_tools
)
asyncio.run(main())
```
**Benefits of Custom Event Loop:**
- **Context Propagation**: Maintain request-scoped state across async operations
- **Resource Reuse**: Share existing async resources like HTTP sessions or database pools
- **Loop Management**: Prevent "Task got Future attached to a different event loop" errors
- **Performance**: Better control over async task scheduling and execution
**Important:** When using a custom loop, you're responsible for its lifecycle
Don't close the loop while ClientTools are still using it.
### Tool Registration
Register custom tools that the AI agent can call during conversations:
```python
client_tools = ClientTools()
# Sync tool
def calculate_sum(params):
numbers = params.get("numbers", [])
return sum(numbers)
# Async tool
async def fetch_data(params):
url = params.get("url")
# Your async HTTP request logic
return {"data": "fetched"}
client_tools.register("calculate_sum", calculate_sum, is_async=False)
client_tools.register("fetch_data", fetch_data, is_async=True)
```
## Speech Engine
Speech Engine lets you build server-side voice agents that receive real-time transcripts from the ElevenLabs API and stream LLM responses back for text-to-speech synthesis. Your server acts as a WebSocket endpoint — ElevenLabs connects to it, sends user transcripts, and your code decides how to respond.
Speech Engine is async-only and available on `AsyncElevenLabs`.
### Quick Start
```python
import asyncio
from openai import AsyncOpenAI
from elevenlabs import AsyncElevenLabs
openai_client = AsyncOpenAI()
elevenlabs = AsyncElevenLabs()
async def main():
engine = await elevenlabs.speech_engine.get("seng_123")
async def on_transcript(transcript, session):
stream = await openai_client.responses.create(
model="gpt-4o",
input=[
{"role": "assistant" if m.role == "agent" else m.role, "content": m.content}
for m in transcript
],
stream=True,
)
await session.send_response(stream)
async def on_init(conversation_id, session):
print(f"Session started: {conversation_id}")
async def on_close(session):
print(f"Session ended: {session.conversation_id}")
async def on_error(err, session):
print(f"Error: {err}")
await engine.serve(
port=3001,
debug=True,
on_init=on_init,
on_transcript=on_transcript,
on_close=on_close,
on_error=on_error,
)
asyncio.run(main())
```
### How It Works
When `engine.serve()` starts, it opens a WebSocket server on the specified port. For each incoming connection from the ElevenLabs API:
1. An `init` message arrives with a `conversation_id`
2. As the user speaks, `user_transcript` messages arrive with the full conversation history
3. Your `on_transcript` handler generates a response (using any LLM) and calls `session.send_response()`
4. If the user interrupts (speaks again mid-response), the previous handler is automatically cancelled
### Sending Responses
`send_response()` accepts strings or async iterators. LLM stream formats from OpenAI, Anthropic, and Google Gemini are auto-detected:
```python
# Plain string
await session.send_response("Hello world")
# OpenAI stream (auto-parsed)
stream = await openai_client.responses.create(model="gpt-4o", ..., stream=True)
await session.send_response(stream)
# Anthropic stream (auto-parsed)
stream = anthropic_client.messages.stream(model="claude-sonnet-4-20250514", ...)
await session.send_response(stream)
# Any async iterator of strings
async def my_generator():
yield "Hello "
yield "world"
await session.send_response(my_generator())
```
### Interruption Handling
When a new transcript arrives while a previous response is still streaming, the previous handler's `asyncio.Task` is cancelled automatically. Any `await` in your handler (including LLM SDK calls) will raise `asyncio.CancelledError`, which cleanly aborts the in-flight request. No manual signal handling needed.
### Custom Server Integration (FastAPI, Starlette)
For integrating with an existing web server, use `create_session()` instead of `serve()`:
```python
from fastapi import FastAPI, WebSocket
app = FastAPI()
engine = ... # SpeechEngineResource from await client.speech_engine.get(...)
@app.websocket("/api/speech-engine/ws")
async def speech_engine_ws(ws: WebSocket):
await ws.accept()
session = engine.create_session(ws, debug=True)
session.on("user_transcript", handle_transcript)
await session.run()
```
When using `session.on()` directly, handlers receive just the event data (no `session` argument, since you already have the reference):
| Event | Handler signature |
|---|---|
| `"init"` | `async (conversation_id: str) -> None` |
| `"user_transcript"` | `async (transcript: list[ConversationMessage]) -> None` |
| `"close"` | `async () -> None` |
| `"disconnected"` | `async () -> None` |
| `"error"` | `async (error: Exception) -> None` |
### Standalone Server
For full control over the server lifecycle, use `SpeechEngineServer` directly:
```python
from elevenlabs.speech_engine import SpeechEngineServer
server = SpeechEngineServer(
port=3001,
debug=True,
on_transcript=handle_transcript,
)
# In one task:
await server.serve()
# In another task (e.g. signal handler):
await server.stop()
```
### Authentication
Both `engine.serve()` and `SpeechEngineServer` automatically verify the `X-Elevenlabs-Speech-Engine-Authorization` header on every incoming connection, rejecting any requests that were not signed by ElevenLabs. The API key is read from the `AsyncElevenLabs` client (or `api_key=` on `SpeechEngineServer`), falling back to the `ELEVENLABS_API_KEY` environment variable.
#### Disabling authentication
If your server sits behind an infrastructure layer that already restricts incoming traffic to ElevenLabs (typically an IP allowlist scoped to [ElevenLabs' egress ranges](https://elevenlabs.io/docs/overview/capabilities/speech-engine#ip-allowlisting)), you can skip JWT verification by passing `disable_auth=True`:
```python
# Via engine.serve() — no api_key required when disable_auth is True
await engine.serve(port=3001, disable_auth=True, on_transcript=on_transcript)
# Or directly on SpeechEngineServer
server = SpeechEngineServer(port=3001, disable_auth=True, on_transcript=on_transcript)
await server.serve()
```
When auth is disabled the server accepts any client that can reach it and emits a `UserWarning` on startup. **Only use this if you have an IP allowlist or equivalent network-level restriction in front of the server** — without one, anyone on the internet can open a session and consume your compute and downstream LLM quota.
## Languages Supported
Explore [all models & languages](https://elevenlabs.io/docs/models).
## Contributing
While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!
On the other hand, contributions to the README are always very welcome!
|