How to stream text to speech in Python over a WebSocket
Blog post from AssemblyAI
AssemblyAI’s Python text-to-speech tutorial explains how to progress from generating a one-off WAV file through an HTTP request to building a production-oriented streaming voice agent over a persistent WebSocket session. It presents the AssemblyAI-TTS-1 model, which supports six languages, configurable voices, raw PCM streaming, telephony formats, and character-based pricing, while emphasizing explicit voice and language settings and reading audio configuration from server responses. The tutorial demonstrates saving streamed PCM with a WAV header, playing it live through sounddevice, and connecting TTS to a streaming LLM by sending tokens incrementally, flushing at sentence boundaries, and retrieving audio concurrently. A central focus is barge-in handling: when a caller interrupts, the application should halt LLM generation, cancel queued and in-flight server synthesis, and clear locally buffered playback while keeping the session open for the next turn. It also covers session reliability, including keep-alive messages for two-minute idle timeouts, one-hour session lifetimes, text and control-frame limits, and manual raw-WebSocket protocol handling. The broader recommendation is to treat TTS as part of a coordinated real-time STT-LLM-TTS loop, where session reuse, low time to first audio, sentence-level flushing, interruption behavior, and connection management determine how natural a voice agent feels.
| Trend | Post Mentions | Total Month Mentions | Posts | Companies | MoM |
|---|---|---|---|---|---|
| LLM | 22 | 6,400 | 1,190 | 225 | +6% |
| Voice AI | 12 | 2,697 | 247 | 53 | -21% |
| Real-time | 10 | 4,050 | 1,056 | 232 | -16% |
| Universal-3.6 Pro Realtime | 2 | 239 | 24 | 2 | +1306% |
Use this post, company, and trend context to find content marketing opportunities, perform competitive analysis, or address product feature gaps via the Plushcap MCP server or the Plushcap API.