Skip to main content
ElevenLabs Documentation Docs

Search documentation

Type to search this documentation.

On this pageOverview

Python SDK

Install the elevenlabs Python package in your project:

Shell
pip install elevenlabs
# or
poetry add elevenlabs

If you want to use the default implementation of audio input/output you will also need the pyaudio extra:

Shell
pip install "elevenlabs[pyaudio]"
# or
poetry add "elevenlabs[pyaudio]"

In this example we will create a simple script that runs a conversation with the ElevenLabs Agents agent.

First import the necessary dependencies:

Python
import os
import signal

from elevenlabs.client import ElevenLabs
from elevenlabs.conversational_ai.conversation import Conversation
from elevenlabs.conversational_ai.default_audio_interface import DefaultAudioInterface

Next load the agent ID and API key from environment variables:

Python
agent_id = os.getenv("AGENT_ID")
api_key = os.getenv("ELEVENLABS_API_KEY")

The API key is only required for non-public agents that have authentication enabled. You don't have to set it for public agents and the code will work fine without it.

Then create the ElevenLabs client instance:

Python
elevenlabs = ElevenLabs(api_key=api_key)

Now we initialize the Conversation instance:

Python
conversation = Conversation(
    # API client and agent ID.
    elevenlabs,
    agent_id,

    # Assume auth is required when API_KEY is set.
    requires_auth=bool(api_key),

    # Use the default audio interface.
    audio_interface=DefaultAudioInterface(),

    # Simple callbacks that print the conversation to the console.
    callback_agent_response=lambda response: print(f"Agent: {response}"),
    callback_agent_response_correction=lambda original, corrected: print(f"Agent: {original} -> {corrected}"),
    callback_user_transcript=lambda transcript: print(f"User: {transcript}"),

    # Uncomment if you want to see latency measurements.
    # callback_latency_measurement=lambda latency: print(f"Latency: {latency}ms"),

    # Uncomment if you want to receive audio alignment data with character-level timing.
    # callback_audio_alignment=lambda alignment: print(f"Alignment: {alignment.chars}"),
)

We are using the DefaultAudioInterface which uses the default system audio input/output devices for the conversation. You can also implement your own audio interface by subclassing elevenlabs.conversational_ai.conversation.AudioInterface.

Now we can start the conversation. Optionally, we recommended passing in your own end user IDs to map conversations to your users.

Python
conversation.start_session(
    user_id=user_id # optional field
)

To get a clean shutdown when the user presses Ctrl+C we can add a signal handler which will call end_session():

Python
signal.signal(signal.SIGINT, lambda sig, frame: conversation.end_session())

And lastly we wait for the conversation to end and print out the conversation ID (which can be used for reviewing the conversation history and debugging):

Python
conversation_id = conversation.wait_for_session_end()
print(f"Conversation ID: {conversation_id}")

All that is left is to run the script and start talking to the agent:

Shell
# For public agents:
AGENT_ID=youragentid python demo.py

# For private agents:
AGENT_ID=youragentid ELEVENLABS_API_KEY=yourapikey python demo.py
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu