You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
272
+
273
+
Speech Engine is async-only and available on `AsyncElevenLabs`.
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.
355
+
356
+
### Custom Server Integration (FastAPI, Starlette)
357
+
358
+
For integrating with an existing web server, use `create_session()` instead of `serve()`:
359
+
360
+
```python
361
+
from fastapi import FastAPI, WebSocket
362
+
363
+
app = FastAPI()
364
+
engine =...# SpeechEngineResource from await client.speech_engine.get(...)
365
+
366
+
@app.websocket("/api/speech-engine/ws")
367
+
asyncdefspeech_engine_ws(ws: WebSocket):
368
+
await ws.accept()
369
+
session = engine.create_session(ws, debug=True)
370
+
session.on("user_transcript", handle_transcript)
371
+
await session.run()
372
+
```
373
+
374
+
When using `session.on()` directly, handlers receive just the event data (no `session` argument, since you already have the reference):
from .speech_engine.resourceimportSpeechEngineResource# noqa: E402
78
+
79
+
returnSpeechEngineResource(
80
+
engine_id=engine_id,
81
+
client_options=self._client_wrapper,
82
+
)
83
+
84
+
67
85
classAsyncElevenLabs(AsyncBaseElevenLabs):
68
86
"""
69
87
Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propogate to these functions.
0 commit comments