Python SDK
The official Python client for Bakbak TTS: whole files, streaming, and a WebSocket built for voice agents.
bakbak-python is the official Python client for the Bakbak text-to-speech API. It has
sync (Bakbak) and asyncio (AsyncBakbak) clients with identical methods, safe
automatic retries, typed errors, and a WebSocket client that streams many
requests over one connection.
Install
The package is bakbak-python on PyPI; you import it as bakbak. Python 3.10 or newer. Three dependencies: httpx, websockets and typing-extensions.
Set your API key:
Quickstart
Request fields keep the API’s names (voice_id, output_format), so code and the
API reference read the same.
Whole files
See Output formats for every output_format.
Without one, the SDK reads encoding and sample_rate from the WAV file’s own
header. MP3 can’t be wrapped in WAV, so to_wav() raises ValueError for it:
decode it with an audio library, or ask for a wav container.
speed (0.5 to 1.5) changes standard voices only; m1 voices accept it and
speak at their own pace.
Streaming (SSE)
A stream also has iter_audio(), read() and save(path).
WebSocket for voice agents
The WebSocket carries many requests over one connection, which makes it the fastest path to first audio. Requests on a context play in order; different contexts run at the same time.
The sync Bakbak client has the same WebSocket API without await.
Speak an LLM’s reply as it is written
ctx.send_stream() splits text that arrives token by token into sentences and
sends each one the moment it is complete. It accepts strings, or the stream
events of OpenAI-, Anthropic- and Gemini-style SDKs directly, including Gemini’s
raw REST JSON. If the stream yields events it can’t read and no text at all, it
raises TypeError instead of sending nothing; map such events to str first.
Interruptions
stops the context at once: the running request stops, queued ones are dropped,
any audio not yet played is discarded, and a send_stream feeding the context
stops. The context stays usable for the next turn.
Reconnects are handled for you: when the service restarts, requests that had
not started are resent on a new connection, in order. A request whose audio had
already started fails with StreamInterruptedError rather than being played twice.
Voices
Errors
Every error is a bakbak.BakbakError. Errors from the service carry status,
error_code, message, retryable and request_id (see Errors).
Retries and timeouts
Failures before any audio are retried up to max_retries times (default 2),
honouring Retry-After. Once audio has been delivered, the SDK never retries:
the start would play twice and the request was already charged.
Latency tips
- Reuse one client, and open the WebSocket before the first turn.
- Stream sentence by sentence with
send_stream. - Ask for the format you play: for phone calls,
{"container": "raw", "encoding": "pcm_mulaw", "sample_rate": 8000}.
The README covers every option, the sync WebSocket client, raw responses and the sentence segmenter.

