Skip to main content

The official Python SDK for the Soniox API (STT, REST)

Project description

Soniox Python SDK

The SDK exposes two clients: SonioxClient (sync) and AsyncSonioxClient (async). Each client supports:

  • STT over REST (client.stt) and realtime WebSocket (client.realtime.stt)
  • TTS over REST (client.tts) and realtime WebSocket (client.realtime.tts)
  • auth, file uploads, model listing, webhooks, and typed request/response models

Install

pip install soniox
# or if using uv
uv add soniox
export SONIOX_API_KEY=<your-key>

Get your API key from the Soniox Console and inject it once per shell session. Both clients read SONIOX_API_KEY by default, but you can override it per-client if needed.

Avoid Python 3.13.6 - it has a regression in ssl that hangs realtime STT/TTS (CPython issue #137583). Use any other 3.10-3.13.x.

Quick run (STT + TTS, REST + realtime)

  1. REST STT transcription: transcribe a local file end-to-end in one call. Full example: examples/soniox_client/api_example.py.
from soniox import SonioxClient

client = SonioxClient()
transcript = client.stt.transcribe_and_wait_with_tokens(
    file="path/to/audio.mp3",                # local file
    # audio_url="https://example.com/audio.mp3",  # or remote URL
    delete_after=True,                        # auto-cleanup file + transcription
)
print(transcript.text)
client.close()
  1. REST TTS generation: convert text to an audio file.
from soniox import SonioxClient
from soniox.utils import output_file_for_audio_format

client = SonioxClient()
output_file = output_file_for_audio_format("wav", "tts_sync_output")
written = client.tts.generate_to_file(
    output_file,
    text="Hello from Soniox Python SDK Text-to-Speech.",
    model="tts-rt-v1",
    language="en",
    voice="Adrian",
    audio_format="wav",
)
print(f"Wrote {written} bytes to {output_file.resolve()}")
client.close()

Run the full example at examples/soniox_client/tts_api_example.py or async version at examples/async_soniox_client/tts_api_example.py.

  1. Realtime STT streaming: open client.realtime.stt.connect, call session.send_byte_chunk or session.send_bytes, then iterate session.receive_events() to render tokens:
from soniox import SonioxClient
from soniox.types import RealtimeSTTConfig, Token
from soniox.utils import render_tokens, throttle_audio, start_audio_thread

DEMO_FILE = "path_to_your_audio_file"

client = SonioxClient()
config = RealtimeSTTConfig(model="stt-rt-v4", audio_format="mp3")
final_tokens: list[Token] = []
non_final_tokens: list[Token] = []

def realtime():
    with client.realtime.stt.connect(config=config) as session:
        start_audio_thread(session, throttle_audio(DEMO_FILE, delay_seconds=0.1))
        for event in session.receive_events():
            for token in event.tokens:
                if token.is_final:
                    final_tokens.append(token)
                else:
                    non_final_tokens.append(token)
            print(render_tokens(final_tokens, non_final_tokens))
            non_final_tokens.clear()

realtime()
client.close()

See examples/soniox_client/realtime_example.py for the full flow.

  1. Realtime TTS streaming: send text chunks and write audio to a file as it arrives.
from uuid import uuid4

from soniox import SonioxClient
from soniox.types import RealtimeTTSConfig
from soniox.utils import output_file_for_audio_format

client = SonioxClient()
config = RealtimeTTSConfig(
    stream_id=f"sync-{uuid4()}",
    model="tts-rt-v1",
    language="en",
    voice="Adrian",
    audio_format="wav",
)

output_file = output_file_for_audio_format("wav", "tts_realtime_output")
bytes_written = 0
with client.realtime.tts.connect(config=config) as session, output_file.open("wb") as f:
    session.send_text_chunks(
        ["Hello from realtime TTS. ", "This is the final chunk."],
        text_end=True,
    )
    for chunk in session.receive_audio_chunks():
        f.write(chunk)
        bytes_written += len(chunk)

print(f"Wrote {bytes_written} bytes to {output_file.resolve()}")

Run the full example at examples/soniox_client/tts_realtime_example.py or async version at examples/async_soniox_client/tts_realtime_example.py.

Repository layout

  • src/soniox/ – sdk code (clients, http namespaces, real-time/session helpers, types, utils).
  • examples/soniox_client & examples/async_soniox_client – runnable STT and TTS examples for sync and async clients.
  • docs/ – markdown outputs (e.g., docs/python-sdk.md) that come from pydoc-markdown.
  • assets/ – sample audio referenced by the examples.

Development

uv install --with dev

This pulls in ruff, pyright, pytest, etc., so you can lint, type-check, test, and regenerate docs locally.

Docs

source .venv/bin/activate
python3 scripts/generate_docs.py

Docs are output to /docs directory.

Resources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

soniox-2.5.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

soniox-2.5.0-py3-none-any.whl (62.2 kB view details)

Uploaded Python 3

File details

Details for the file soniox-2.5.0.tar.gz.

File metadata

  • Download URL: soniox-2.5.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.9

File hashes

Hashes for soniox-2.5.0.tar.gz
Algorithm Hash digest
SHA256 a7436de7e87aa712bfa988a96986a1dcd887dd01af17a7941b173b3cb1a15582
MD5 0bc47a34d34d7d2633d8dcedd7288c9d
BLAKE2b-256 bbe3f58ba67316db5fda303bf7a86824b468fb06478989c396d8ca083e88ad6b

See more details on using hashes here.

File details

Details for the file soniox-2.5.0-py3-none-any.whl.

File metadata

  • Download URL: soniox-2.5.0-py3-none-any.whl
  • Upload date:
  • Size: 62.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.9

File hashes

Hashes for soniox-2.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe66de4df446b05a1fc2195c82b71d5cac0a21d98b371234066c64e6bd849478
MD5 7c0f81b91a4fd5d2cede03719c96a4fa
BLAKE2b-256 28ef6f8051a33733e3bf56d0d808fe20c706e55239b8f4704f1b2686471fee94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page