Skip to main content

Khaya AI

Khaya SDK

Translation, speech recognition, and text-to-speech for African languages.

PyPI Python versions CI Docs License

Python SDK for the Khaya AI API — translation, automatic speech recognition (ASR), and text-to-speech (TTS) across 30+ African languages, with sync and async clients, typed results, and automatic retries.

Documentation · Changelog · Contributing

Installation

pip install khaya

Authentication

Get an API key at https://translation.ghananlp.org and set it as an environment variable:

export KHAYA_API_KEY=your_api_key_here

Settings also reads KHAYA_BASE_URL, KHAYA_TIMEOUT, and KHAYA_RETRY_ATTEMPTS.

Quick Start

import os
from khaya import KhayaClient

with KhayaClient(os.environ["KHAYA_API_KEY"]) as khaya:
    # Translate text from English to Twi
    result = khaya.translate("Hello, how are you?", "en-tw")
    print(result.text)

    # Transcribe a Twi audio file
    result = khaya.transcribe("path/to/audio.wav", "tw")
    print(result.text)

    # Synthesize speech in Twi, optionally choosing a voice
    result = khaya.synthesize("Me ho yɛ", "twi", speaker="female")
    result.save("output.wav")

Each method returns a typed result object rather than a raw HTTP response: TranslationResult and TranscriptionResult expose .text, and SynthesisResult exposes .audio bytes plus a .save(path) helper.

Async Usage

import asyncio
import os
from khaya import KhayaClient

async def main():
    async with KhayaClient(os.environ["KHAYA_API_KEY"]) as khaya:
        result = await khaya.atranslate("Hello", "en-tw")
        print(result.text)

        result = await khaya.atranscribe("path/to/audio.wav", "tw")
        print(result.text)

        result = await khaya.asynthesize("Me ho yɛ", "twi")
        result.save("output.wav")

asyncio.run(main())

Error Handling

All errors raise exceptions — never return error dicts. Catch the appropriate exception:

from khaya import KhayaClient
from khaya.exceptions import (
    AuthenticationError,
    RateLimitError,
    TranslationError,
    APIError,
)

khaya = KhayaClient(api_key="your-key")

try:
    result = khaya.translate("Hello", "en-tw")
    print(result.text)
except AuthenticationError:
    print("Invalid API key. Check your KHAYA_API_KEY.")
except RateLimitError as e:
    print(f"Rate limit hit: {e.message}")
except TranslationError as e:
    print(f"Translation failed ({e.status_code}): {e.message}")
except APIError as e:
    print(f"API error ({e.status_code}): {e.message}")

Supported languages

Service Languages
Translation 12, paired with English (22 pairs)
Speech recognition 34
Text-to-speech 32, with 3 speaker voices

Full tables, and the difference between legacy and ISO 639-3 codes, are in the language reference.

The SDK does not validate language codes — it sends what you pass and lets the API decide, so you can use any code the API supports without waiting for an SDK release.

Configuration

from khaya import KhayaClient
from khaya.config import Settings

config = Settings(
    api_key="your-key",
    timeout=60,          # seconds (default: 30)
    retry_attempts=5,    # retries on transient failures (default: 3)
)
khaya = KhayaClient(api_key="your-key", config=config)

Development

# Install all dependency groups
uv sync --extra test --extra dev

# Run unit tests (no API key required)
uv run pytest -m "not integration"

# Run with coverage
uv run pytest -m "not integration" --cov=src/khaya --cov-report=term-missing

# Lint and type-check
uv run ruff check src/khaya
uv run mypy src/khaya

# Install pre-commit hooks
uv run pre-commit install

License

MIT

Download files

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

Source Distribution

khaya-0.2.1.tar.gz (168.0 kB view details)

Uploaded Source

Built Distribution

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

khaya-0.2.1-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file khaya-0.2.1.tar.gz.

File metadata

  • Download URL: khaya-0.2.1.tar.gz
  • Upload date:
  • Size: 168.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for khaya-0.2.1.tar.gz
Algorithm Hash digest
SHA256 56441a531539f9c896fb48ac1c5c762b2a67e5b0abf57d95cc937960b480cdad
MD5 446caa54913dc2f89e2e1a375ea7caf2
BLAKE2b-256 a6cef6590c0b6fd7cdd68df9c5872805c3bc4565a75b18126d20bd447eb516cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for khaya-0.2.1.tar.gz:

Publisher: release.yml on Khaya-AI/khaya-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file khaya-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: khaya-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for khaya-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 56bf6a8fafb10c1d57b6807f5283f3958ffc9ab592b059acad4e562c00a9ed89
MD5 0e1bc16de837b803106819ac18b5eb52
BLAKE2b-256 3b90d0f0bb89546418062012b192bb34401cdd9e2afd024f55f7e6514910c4b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for khaya-0.2.1-py3-none-any.whl:

Publisher: release.yml on Khaya-AI/khaya-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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