Soniox
SDKsPythonText-to-Speech

REST speech generation with Python SDK

Convert text to speech with the Soniox Python SDK

Soniox Python SDK supports asynchronous Text-to-Speech generation with AsyncSonioxClient. You can generate speech directly to audio bytes or write output to a file.

Quickstart

The SDK provides a convenient generate_to_file method for writing audio output directly to disk.

import asyncio
from soniox import AsyncSonioxClient

async def main() -> None:
    client = AsyncSonioxClient()
    try:
        written = await client.tts.generate_to_file(
            "tts_async_output.wav",
            text="Hello from the Soniox Python SDK async TTS example.",
            model="tts-rt-v1-preview",
            language="en",
            voice="Adrian",
            audio_format="wav",
        )
        print(f"Wrote {written} bytes")
    finally:
        await client.aclose()

asyncio.run(main())

Generate to bytes

Use generate when you want audio bytes in memory (for custom storage, streaming, or post-processing).

import asyncio
from soniox import AsyncSonioxClient

async def main() -> None:
    client = AsyncSonioxClient()
    try:
        audio = await client.tts.generate(
            text="This response is generated in memory.",
            model="tts-rt-v1-preview",
            language="en",
            voice="Adrian",
            audio_format="wav",
        )
        print(f"Received {len(audio)} bytes")
    finally:
        await client.aclose()

asyncio.run(main())

Generate to file

Use generate_to_file when you want the SDK to write output for you.

import asyncio
from soniox import AsyncSonioxClient

async def main() -> None:
    client = AsyncSonioxClient()
    try:
        written = await client.tts.generate_to_file(
            "hello.pcm",
            text="Hello from Soniox async text to speech.",
            model="tts-rt-v1-preview",
            language="en",
            voice="Adrian",
            audio_format="pcm_s16le",
            sample_rate=24000,
        )
        print(f"Wrote {written} bytes")
    finally:
        await client.aclose()

asyncio.run(main())

Use typed config (CreateTtsConfig)

You can pass generation options through CreateTtsConfig.

import asyncio
from soniox import AsyncSonioxClient
from soniox.types import CreateTtsConfig

async def main() -> None:
    client = AsyncSonioxClient()
    try:
        written = await client.tts.generate_to_file(
            "typed-config.wav",
            text="Typed configuration example.",
            voice="Adrian",
            config=CreateTtsConfig(
                model="tts-rt-v1-preview",
                language="en",
                audio_format="wav",
                sample_rate=24000,
            ),
        )
        print(f"Wrote {written} bytes")
    finally:
        await client.aclose()

asyncio.run(main())

Error handling

Handle SonioxAPIError to inspect API-level failures.

For raw HTTP integration details, see TTS REST API error handling.

import asyncio
from soniox import AsyncSonioxClient
from soniox.errors import SonioxAPIError

async def main() -> None:
    client = AsyncSonioxClient()
    try:
        await client.tts.generate_to_file(
            "error-case.wav",
            text="Hello from Soniox!",
            voice="Adrian",
        )
    except SonioxAPIError as exc:
        print("Soniox API error:", exc)
        if exc.request_id:
            print("request_id:", exc.request_id)
    finally:
        await client.aclose()

asyncio.run(main())