Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Bakbak Python SDK

The official Python client for the Bakbak text-to-speech API by Raya: natural speech in Hindi, English and other Indian languages, as whole files, as a stream, or over a WebSocket built for voice agents.

  • Sync (Bakbak) and asyncio (AsyncBakbak) clients with identical surfaces
  • Whole files, Server-Sent Events streams, and a multiplexed WebSocket
  • Sentence segmentation for LLM token streams, built in
  • Automatic, safe retries: never after audio has been delivered
  • Typed throughout (py.typed), with three runtime dependencies: httpx, websockets, typing-extensions

Documentation: Python SDK guide · WebSocket API reference · Errors

Install

pip install --pre bakbak-python     # 1.0 is in release candidates; drop --pre after 1.0.0

The package is bakbak-python on PyPI; you import it as bakbak.

Python 3.10 or newer.

Quickstart

from bakbak import Bakbak

client = Bakbak()  # reads BAKBAK_API_KEY

audio = client.tts.generate(
    text="नमस्ते, आप कैसे हैं?",
    voice_id="009ae8b5-4a03-492d-af1c-3055989ac734",  # Anika M1
    language="hi",
    model="m1",
)
audio.save("hello.wav")

The same with asyncio:

import asyncio
from bakbak import AsyncBakbak


async def main() -> None:
    async with AsyncBakbak() as client:
        audio = await client.tts.generate(text="नमस्ते!", voice_id="…", language="hi", model="m1")
        audio.save("hello.wav")


asyncio.run(main())

Every example in examples/ runs as is with BAKBAK_API_KEY set.

Configuration

from bakbak import Bakbak, Timeout

client = Bakbak(
    api_key="…",  # default: BAKBAK_API_KEY
    base_url="https://hub.getraya.app",  # default: BAKBAK_BASE_URL, then this
    timeout=Timeout(connect=5, read=60, total=None),
    max_retries=2,
    default_headers={"X-Team": "voice"},
    http_client=None,  # your own httpx.Client, e.g. with a proxy
)
Option Default Meaning
api_key BAKBAK_API_KEY Sent as X-API-Key. Environment variables are read when the client is constructed, never at import.
base_url BAKBAK_BASE_URL, then https://hub.getraya.app The WebSocket URL is derived from it (https → wss).
timeout 5 s connect, 60 s read A Timeout(connect, read, total); a number, which sets the read timeout (and caps connect at it); or None for none. The read timeout includes the time to first audio. total limits a whole call, retries included.
max_retries 2 Retries of a failed request before any audio, so at most 3 attempts.
default_headers none Added to every request.
http_client a pooled httpx.Client Bring your own (httpx.AsyncClient for AsyncBakbak). The SDK does not close it.

Every HTTP method also takes timeout, max_retries, extra_headers and (for synthesis) extra_body. client.with_options(...) returns a copy with some options changed that shares the connection pool:

patient = client.with_options(timeout=120, max_retries=4)

Whole files

audio = client.tts.generate(
    text="आपका ऑर्डर कल पहुँचेगा।",
    voice_id=voice_id,
    language="hi",
    model="m1",
    speed=1.1,  # 0.5 to 1.5
    output_format={"container": "wav", "encoding": "pcm_s16le", "sample_rate": 24000},
)
audio.data  # bytes, exactly as received
audio.request_id  # quote it to support
audio.content_type  # audio/wav, audio/mpeg, audio/pcm, audio/basic, application/octet-stream
audio.encoding, audio.sample_rate  # from X-Audio-Format / X-Audio-Sample-Rate
audio.save("out.wav")
audio.to_wav()  # WAV bytes; raw PCM, mu-law and A-law get a header

output_format:

Field Values Notes
container raw, wav, mp3 Streams (SSE and WebSocket) accept only raw.
encoding pcm_f32le, pcm_s16le, pcm_mulaw, pcm_alaw Required for raw and wav; leave it out for mp3. Mono.
sample_rate 8000, 16000, 22050, 24000 Default 24000.

Without output_format, a whole file is 24 kHz WAV and a stream is 24 kHz 32-bit float PCM. Languages, models and encodings are typed as the known values plus any string, so a value the server adds later needs no SDK release.

Streaming (SSE)

with client.tts.stream(
    text="आपका पैकेज आज शाम तक पहुँच जाएगा।",
    voice_id=voice_id,
    language="hi",
    model="m1",
    output_format={"container": "raw", "encoding": "pcm_s16le", "sample_rate": 24000},
) as stream:
    for chunk in stream:  # AudioChunk(audio, step_time, request_id)
        player.write(chunk.audio)
print(stream.request_id, stream.audio_bytes)

The request is sent when the stream is entered or first iterated. A chunk always holds whole samples: if the server splits a sample across two events, the SDK carries the remainder over. A stream also has iter_audio() (bytes only), read() (all of it) and save(path). It can be read once. On AsyncBakbak it is async with / async for, and read(), save() and close() are awaitable.

WebSocket: many requests, one connection

The WebSocket carries any number of streaming requests over one connection, and saves the per-request connection and gateway cost. It is the fastest path to first audio and the one to use for voice agents.

async with client.tts.websocket(
    voice_id=voice_id,
    language="hi",
    model="m1",
    output_format={"container": "raw", "encoding": "pcm_s16le", "sample_rate": 16000},
) as ws:  # opens the socket now (prewarm)
    ctx = ws.context()  # requests on a context play in order
    await ctx.send(text="पहला वाक्य।")
    await ctx.send(text="दूसरा वाक्य।")
    ctx.end_input()
    async for audio in ctx.audio():  # bytes, in order; raises the first failure
        player.write(audio)
  • Contexts. ws.context(context_id=None, **defaults) returns a context; the id defaults to a random ctx_…. Requests on one context run one after another; different contexts run at the same time. Fields left out of send() come from the context's defaults, then the WebSocket's.
  • Events. ctx.events() yields Chunk(audio, request), Done(request), Failed(request, error) and Cancelled(request) in order, and ends after end_input() once every request has an outcome. An error ends its own request only. ctx.audio() yields just the bytes.
  • Request handles. send() returns a handle: await handle gives RequestResult(request_id, audio_bytes, cancelled) or raises the request's error, and async for audio in handle yields that request's audio.
  • Cancel. await ctx.cancel() stops the context at once: unfinished requests end as Cancelled, all of its audio not yet handed to you is dropped, and a send_stream feeding it stops sending and returns. The cancel still reaches the server if the task calling it is itself cancelled. The context stays usable.
  • Connection management. The first send opens the socket if you did not. Server restarts (close 1001, or a draining refusal) are handled by resending on a new connection; a dropped connection fails only the requests that already had audio (StreamInterruptedError) and resends the rest. Every unfinished request always gets an outcome, and cancelling the task awaiting send() never leaves a half-sent request behind. send() waits only if the connection's limits (64 unfinished requests, 16 busy contexts) are reached; open more WebSockets for more parallelism. A WebSocket can live for days: finished contexts are released, so its memory does not grow with the requests it serves.
  • Connection-level errors. Errors about no request in particular go to client.tts.websocket(on_error=callback).

The sync client has the same API; it reads the socket on a background thread, so read ctx.audio() on one thread while another sends:

with client.tts.websocket(voice_id=voice_id, language="hi", model="m1") as ws:
    ctx = ws.context()
    handle = ctx.send(text="नमस्ते।")
    ctx.end_input()
    audio = b"".join(ctx.audio())
    print(handle.result().request_id)

Speaking an LLM's reply as it is written

ctx.send_stream(tokens) splits text that arrives in pieces into sentences and sends each one the moment it is complete, then calls end_input(). It takes strings, or the stream events of OpenAI-, Anthropic- and Gemini-style SDKs directly: the text delta is found by duck typing (choices[0].delta.content, content_block_delta → delta.text, .text), and tool calls or reasoning deltas are skipped. No LLM SDK is imported.

from openai import AsyncOpenAI

llm = AsyncOpenAI()
tokens = await llm.chat.completions.create(model="…", messages=messages, stream=True)

ctx = ws.context()
sender = asyncio.create_task(ctx.send_stream(tokens))
async for audio in ctx.audio():
    player.write(audio)
await sender

See examples/llm_streaming.py.

Voices

voices = client.voices.list(model="m1", language="hi")  # filtered by the SDK
for voice in voices:
    print(voice.id, voice.name, voice.language, voice.model)

A voice belongs to one model; m1 voices use en for English.

Sentence segmentation

The splitter send_stream uses is public:

from bakbak.text import Segmenter, split_sentences

split_sentences("Dr. Rao will call at 5 p.m. today. Please stay on the line.")
# ['Dr. Rao will call at 5 p.m. today.', 'Please stay on the line.']

segmenter = Segmenter(eager_first_clause=True)  # first audio sooner on a long first sentence
for token in tokens:
    for sentence in segmenter.push(token):
        ...
remaining = segmenter.flush()

It understands the danda (।, ॥), titles and initials (Dr., डॉ., A. P. J.), decimals and domains (3.14, getraya.app), list markers and abbreviations, and never splits inside an Indic conjunct. Options: min_chars (20), first_min_chars (1), max_chars (400, at most 5,000), eager_first_clause (False), first_clause_min_chars (24). Lengths are counted in code points. The JavaScript SDK and the LiveKit plugin split every text exactly the same way.

Audio helpers

from bakbak.audio import to_wav

wav = to_wav(raw_bytes, "pcm_mulaw", 8000)

pcm_s16le gets the canonical 44-byte header; pcm_f32le, pcm_alaw and pcm_mulaw get the 58-byte header (with a fact chunk) that their WAV formats require. There are no playback helpers: hand the bytes to your audio library.

Errors

Every error derives from bakbak.BakbakError:

BakbakError
├── APIError                        the service answered, or a stream broke
│   ├── APIStatusError              an HTTP status, or an in-band error before audio
│   │   ├── BadRequestError (400), AuthenticationError (401),
│   │   ├── InsufficientCreditsError (402), PermissionDeniedError (403),
│   │   ├── NotFoundError (404), RequestTooLargeError (413),
│   │   ├── UnprocessableEntityError (422), RateLimitError (429),
│   │   └── InternalServerError (≥ 500)
│   └── StreamInterruptedError      failed after audio was delivered
└── APIConnectionError              no usable answer: network, TLS, DNS
    ├── APITimeoutError
    └── WebSocketClosedError

APIError has status, error_code, message, retryable, request_id and body (the parsed JSON, so fields such as required_inr stay readable). StreamInterruptedError adds audio_bytes: how much audio you received.

import bakbak

try:
    audio = client.tts.generate(text=text, voice_id=voice_id, language="hi")
except bakbak.InsufficientCreditsError as e:
    print("Top up:", e.body["required_inr"])
except bakbak.APIStatusError as e:
    print(e.status, e.error_code, e.message, e.request_id)
except bakbak.APIConnectionError:
    print("Could not reach the API")

Errors never contain your API key or request headers, in any attribute, str(), repr() or chained cause. A key with whitespace, control or non-ASCII characters (a copy-paste slip) is refused when the client is created, without echoing it. The SDK never follows redirects, on HTTP or the WebSocket: a 3xx is an APIStatusError, so the key is never sent to another origin. See the error reference for every error_code.

Retries and timeouts

Before any audio arrives, the SDK retries connection errors, timeouts, and errors whose retryable is true (by default 408, 429, 500, 502, 503 and 504): up to max_retries times, waiting Retry-After when the server sends it (at most 60 s), otherwise 0.5 s, 1 s, 2 s … with jitter. Once audio has been delivered it never retries: the start would play twice, and the request has already been charged. You get StreamInterruptedError with the request_id and audio_bytes instead.

On the WebSocket, a retryable refusal is resent on its context only if no later request of that context was already sent; otherwise resending would break the order, and the error is delivered.

Raw responses

raw = client.tts.with_raw_response.generate(text=text, voice_id=voice_id, language="hi")
raw.headers, raw.status_code, raw.request_id, raw.retries_taken
audio = raw.parse()

Forward compatibility

Fields the SDK does not model yet go in extra_body (merged into the request body) and extra_headers. Unknown response fields and stream events are ignored. The SDK sends exactly the fields you give it; the server validates them.

Latency tips

  • Reuse one client. It keeps connections alive; client.prewarm() opens one ahead of the first request.
  • Use the WebSocket for agents. Open it early (await ws.connect(), or async with), so the first request pays no handshake. Measured against a staging deployment of the API (m1, 20 sequential requests each, kept-alive connections, examples/bench_ttfa.py): time to first audio p50 20.7 ms over the WebSocket versus 33.3 ms over SSE.
  • Stream sentence by sentence with send_stream, and consider Segmenter(eager_first_clause=True) when first sentences are long.
  • Ask for the format you play. For phone calls use {"container": "raw", "encoding": "pcm_mulaw", "sample_rate": 8000}, and skip resampling and transcoding (see examples/telephony.py).
  • Keep the event loop free. send_stream reads a sync iterable on the event loop; pass an async iterable from an async LLM client.

Logging

The SDK logs to logging.getLogger("bakbak") at DEBUG level only: request ids, statuses, timings and sizes. It never logs the API key or any text.

import logging

logging.basicConfig()
logging.getLogger("bakbak").setLevel(logging.DEBUG)

More

Licensed under the Apache License 2.0.

Metadata

Release files for bakbak-python 1.0.0rc2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bakbak-python 1.0.0rc2
File Size Uploaded Provenance Actions
bakbak_python-1.0.0rc2.tar.gz 316.2 kB With provenance Details for bakbak_python-1.0.0rc2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bakbak-python 1.0.0rc2
File Interpreter ABI Platform Actions
bakbak_python-1.0.0rc2-py3-none-any.whl Python 3 none any Details for bakbak_python-1.0.0rc2-py3-none-any.whl

Total release size: 391.2 kB

Release files / bakbak_python-1.0.0rc2.tar.gz

Download URL bakbak_python-1.0.0rc2.tar.gz
Size 316.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d0451ee0e4f48a3a41a6f89016e290d25085bf821de2b535fe4fe08f0a737be1
BLAKE2b-256 checksum
How to use checksums
85dfce59526fbcd00f6ce4af96e6d11ee86b3346a5c65f3b6eb1f2c44b9b42f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Release files / bakbak_python-1.0.0rc2-py3-none-any.whl

Download URL bakbak_python-1.0.0rc2-py3-none-any.whl
Size 75.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
caf417106f5f4b4dd0d54b421e67e0b71315a8c14138cada6102f87a5ccb0b33
BLAKE2b-256 checksum
How to use checksums
85e3712e4fa9e6db347df594ebe1de8b21c94cb5ad42a0102ce81fe23fe26cf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Security for bakbak-python 1.0.0rc2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bakbak_python-1.0.0rc2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page