Skip to content

kokoro_tts

kokoro_tts

Kokoro TTS backend — fully open-source, runs locally.

Requires the kokoro package: pip install kokoro Falls back gracefully if not installed.

Kokoro v1.x supports multiple languages. Each language has its own KPipeline (the model loads language-specific G2P resources at init). This backend lazily creates one pipeline per language code, derived from the voice ID prefix per Kokoro's naming convention {lang_prefix}{gender}_{name} — e.g. zf_xiaoxiao is Mandarin female (prefix zlang_code="z").

Classes

KokoroTTSBackend

KokoroTTSBackend(*, model_path: str = '', device: str = 'auto', max_cached_pipelines: int = 4)

Bases: TTSBackend

Kokoro TTS — local open-source voice synthesis, multilingual.

Source code in src/openjarvis/speech/kokoro_tts.py
def __init__(
    self,
    *,
    model_path: str = "",
    device: str = "auto",
    max_cached_pipelines: int = 4,
) -> None:
    if max_cached_pipelines < 1:
        raise ValueError("max_cached_pipelines must be at least 1")
    self._model_path = model_path
    self._device = device
    self._max_cached_pipelines = max_cached_pipelines
    self._model: Any | None = None
    self._pipelines: OrderedDict[str, Any] = OrderedDict()
    # Pipeline creation and inference are both guarded: KPipeline is not
    # documented as thread-safe, and an LRU entry must not be evicted and
    # closed while another thread is still synthesizing with it.
    self._pipeline_lock = threading.RLock()
Functions
close
close() -> None

Release every cached pipeline and clear the cache.

Source code in src/openjarvis/speech/kokoro_tts.py
def close(self) -> None:
    """Release every cached pipeline and clear the cache."""
    with self._pipeline_lock:
        pipelines = list(self._pipelines.values())
        self._pipelines.clear()
        for pipeline in pipelines:
            self._cleanup_pipeline(pipeline)
        self._model = None