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", "twi")
    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", "twi")
        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}")

Use it from Claude, Cursor, or any MCP client

pip install khaya[mcp]
{
  "mcpServers": {
    "khaya": {
      "command": "khaya-mcp",
      "env": { "KHAYA_API_KEY": "your_api_key_here" }
    }
  }
}

Your assistant can then translate, transcribe and synthesize African languages directly. See the MCP guide.

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.3.0.tar.gz (219.6 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.3.0-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for khaya-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b70b83c2bee2c6f7aa6b954241b0ada3c54c84c96821a09a7d0a057638fab51f
MD5 84d0d6235da7b293e2fb7ce4cfac077e
BLAKE2b-256 edd5342b7559351a6ded916e63fff9d1bc633ef1a6831b32302de898fddb24cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for khaya-0.3.0.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.3.0-py3-none-any.whl.

File metadata

  • Download URL: khaya-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 23.7 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e46eb3a9edec163664409d07c6910554d22a0ebf0df9cd36a92f145c378f3194
MD5 fb1494cf06e7d85c06b043ba85a214e7
BLAKE2b-256 30cefedcce1502cc64b19f2108791198825046fd9e211640c537d74e214bc9b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for khaya-0.3.0-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