Python SDK reference
This page documents the public API for the Speech Engine Python SDK (elevenlabs).
Getting a Speech Engine resource
Retrieve a SpeechEngineResource by its engine ID. The returned object provides methods to start a server, verify requests, or create individual sessions.
SpeechEngineResource
Properties
serve
Start a standalone WebSocket server. Blocks until stopped.
verify_request
Verify that an incoming request originates from the ElevenLabs Speech Engine API. Checks the X-Elevenlabs-Speech-Engine-Authorization header for a valid JWT signed with the SHA-256 hash of your API key.
Only needed when managing the WebSocket upgrade yourself. When using serve(), verification is handled automatically.
Returns: bool — True if the request is valid.
create_session
Wrap an accepted WebSocket in a SpeechEngineSession. Use this for custom server integration (e.g. FastAPI, Starlette, or manual WebSocket handling).
Returns: SpeechEngineSession
SpeechEngineSession
Wraps a single WebSocket connection. Each connection represents one conversation. The session emits events for transcripts and lifecycle changes, and provides methods to send LLM responses back.
When a new transcript arrives, the previous transcript handler is cancelled automatically, interrupting any in-flight LLM call.
Properties
on
Register a handler for an event. Returns the session for chaining.
off
Remove a previously registered handler.
once
Register a handler that fires once then removes itself.
send_response
Send an LLM response back to the Speech Engine API for text-to-speech synthesis. Must be called inside an on_transcript handler. Calling it outside of a handler emits a warning and returns without sending.
The SDK auto-detects and extracts text from the following LLM stream formats:
run
Run the receive loop until the WebSocket closes. This is the main entry point after constructing a session manually via create_session().
close
Close the session and the underlying WebSocket connection.
Callbacks
The keyword arguments passed to serve(). All callbacks are optional. Handlers can be synchronous or asynchronous (coroutine) functions.
Events
When using session.on() directly instead of callbacks, these are the event names and their handler signatures.
Event name constants are available for type-safe usage:
ConversationMessage
A single message in the conversation history. The full transcript is passed to on_transcript on every turn.
Wire protocol
For reference, these are the JSON messages exchanged over the WebSocket connection. The SDK handles serialization and deserialization automatically.