Skip to main content

Overview

The Conversation class manages WebSocket-based real-time conversations with your AI agents. It handles audio streaming, message events, and the conversation lifecycle.

Basic Usage

Create and start a conversation:

Constructor Parameters

ElevenLabs
required
The ElevenLabs client instance
str
required
The ID of the agent to converse with
bool
required
Whether the agent requires authentication
AudioInterface
required
Audio interface for input/output handling
str
Optional user identifier for the conversation
ConversationInitiationData
Configuration options for the conversation
ClientTools
Custom tools the agent can call during conversation

Event Callbacks

Register callbacks to handle conversation events:

Available Callbacks

Callable[[str], None]
Called when the agent produces a complete response
Callable[[str, str], None]
Called when the agent corrects a previous response. First arg is original, second is corrected.
Callable[[str, AgentChatResponsePartType], None]
Called for streaming text response chunks. Part type is START, DELTA, or STOP.
Callable[[str], None]
Called when user speech is transcribed
Callable[[int], None]
Called with latency measurements in milliseconds
Callable[[AudioEventAlignment], None]
Called with character-level audio alignment data
Callable[[], None]
Called when the conversation session ends

Streaming Response Parts

Handle streaming text responses from the agent:

Audio Alignment

Get character-level timing information for agent audio:

Sending Messages

Send text messages to the agent programmatically:

Message Methods

method
Send a text message from the user to the agent
method
Send non-interrupting contextual information to update conversation state
method
Send a ping to prevent session timeout

Configuration Options

Customize conversation behavior with ConversationInitiationData:

Configuration Fields

dict
Additional custom parameters passed to the LLM
dict
Override default conversation configuration settings
dict
Dynamic variables accessible to the agent during conversation
str
Identifier for the user in this conversation

Session Management

Start Session

Starts the conversation in a background thread:

End Session

Ends the conversation and cleans up resources:

Wait for Session End

Blocks until the conversation completes:
Call end_session() before wait_for_session_end(), otherwise it will block indefinitely.

Async Conversations

Use AsyncConversation for async/await workflows:
All async callbacks must be async functions. Use AsyncAudioInterface instead of AudioInterface.

Error Handling

Handle connection and runtime errors:

Complete Example