ElevenLabs Python SDK
The `elevenlabs` Python SDK is the official client library for the ElevenLabs API, enabling developers to integrate advanced AI voice capabilities into their applications. It supports a wide range of features including text-to-speech, voice cloning, speech-to-text, and conversational AI. The library is actively maintained with very frequent releases (often multiple times a week), typically driven by 'Fern Regeneration' to reflect the latest API schema changes.
Warnings
- breaking Version 2 (v2) of the SDK introduced significant breaking changes, including renaming of many methods and simplification of the API surface. Code written for v1 will likely not work with v2 without modifications.
- gotcha Directly embedding your ElevenLabs API key in source code is a significant security risk. Anyone with access to your code could use your key, potentially incurring unexpected costs or unauthorized access.
- gotcha The `play()` function, used to play audio directly, requires the optional `pyaudio` dependency. If not installed, calling `play()` will result in an error or silence.
- deprecated The top-level `generate` function (e.g., `from elevenlabs import generate`) has been removed or deprecated. Attempts to import or use it will fail.
- gotcha Frequent API schema updates, often indicated by 'Fern Regeneration' in release notes, can introduce subtle breaking changes even in minor SDK versions if your code relies on specific response structures or optional parameters becoming required.
- gotcha Exceeding character quotas, concurrency limits, or using an invalid API key are common reasons for API errors (e.g., HTTP 400, 401, 429).
- gotcha Prior to v2.41.0, the `audio_interface` parameter for `Conversation` class in `conversational_ai` might have been implicitly required or caused runtime errors in text-only chat modes.
Install
-
pip install elevenlabs -
pip install elevenlabs[pyaudio]
Imports
- ElevenLabs
from elevenlabs.client import ElevenLabs
- play
from elevenlabs.play import play
- generate
Quickstart
import os
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
# Initialize the client. It automatically picks up ELEVENLABS_API_KEY from environment variables.
# You can also pass it explicitly: ElevenLabs(api_key="YOUR_API_KEY")
elevenlabs = ElevenLabs(api_key=os.environ.get("ELEVENLABS_API_KEY", ""))
if not elevenlabs.api_key:
print("Error: ELEVENLABS_API_KEY environment variable not set.")
print("Please set your ElevenLabs API key before running this example.")
else:
print("Generating speech...")
try:
audio = elevenlabs.text_to_speech.convert(
text="The quick brown fox jumps over the lazy dog.",
voice_id="21m00Tz activations", # A common pre-made voice ID (e.g., 'Rachel')
model_id="eleven_v3", # Recommended model, or "eleven_multilingual_v2", "eleven_flash_v2.5", etc.
output_format="mp3_44100_128",
)
print("Speech generated. Playing audio (requires pyaudio installed)...")
play(audio)
print("Audio played.")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure 'elevenlabs[pyaudio]' is installed if you want to play audio directly.")
print("Also check your API key and subscription plan for model/voice access.")