Skip to main content

PyKokoro

PyKokoro is a Kokoro speech-synthesis engine. It accepts prepared speech text and explicit pronunciation context, uses KokoroG2P for model-compatible phonemization, and uses OnnxVoice for Kokoro ONNX inference. Document parsing, SSMD interpretation, speech planning, timeline composition, embedded audio, and final-output mastering are intentionally outside PyKokoro.

Quick start

Install one ONNX Runtime provider, then synthesize a prepared string:

pip install "pykokoro[cpu]"
from pykokoro import GenerationConfig, KokoroSynthesizer, SynthesisConfig

config = SynthesisConfig(
    voice="af_sarah",
    generation=GenerationConfig(lang="en-us"),
)
with KokoroSynthesizer(config) as synthesizer:
    rendered = synthesizer.synthesize_text(
        "Hello, world.",
        language="en-us",
        voice="af_sarah",
    )

rendered.save_wav("hello.wav")

The WAV writer stores mono float32 audio. Model assets are provisioned lazily on first use.

Prepared requests and pronunciation context

For orchestration, create a SynthesisSegment with an opaque request ID, prepared text, an explicit pronunciation language, and a supported static or enrolled reference voice. Optional source-aligned pronunciation instructions and linguistic tokens use offsets into that exact text:

from pykokoro import (
    LinguisticToken,
    PronunciationOverride,
    SynthesisSegment,
)

request = SynthesisSegment(
    id="line-001",
    text="Hello Welt.",
    language="en-us",
    voice="af_sarah",
    pronunciation_overrides=(PronunciationOverride(6, 10, language="de"),),
    tokens=(LinguisticToken(0, 5, text="Hello", pos="INTJ"),),
)

Use synthesize() for one request or synthesize_segments() for an ordered iterable of independent requests. Each request yields its own RenderedSegment; PyKokoro does not join batch results or insert cross-request silence. The caller can save, play, or pass each waveform to a separate composition system.

Longer requests and optional discovery

long_text_split="none" is the default and raises SynthesisInputTooLongError if a prepared request exceeds model capacity. Opt into lazy sentence splitting for oversized requests; the engine still returns one result for the original request:

from pykokoro import GenerationConfig, KokoroSynthesizer, SynthesisConfig


long_text = "A prepared request with several sentences. " * 100
config = SynthesisConfig(
    voice="af_sarah",
    generation=GenerationConfig(lang="en-us"),
    long_text_split="sentence",
    long_text_use_spacy=False,
)
with KokoroSynthesizer(config) as synthesizer:
    rendered = synthesizer.synthesize_text(long_text, language="en-us")

To inspect installed/runtime model profiles without loading weights, use discover_models(offline=True). Profile data describes language/voice combinations; G2P language availability alone does not guarantee that every acoustic profile supports it.

Input text is prepared speech, not a document markup language. PyKokoro does not parse SSMD, YAML front matter, or say-as/voice/pause directives. GenerationConfig.speed is a Kokoro acoustic inference control, not editorial timeline-rate policy.

Engine behavior

The engine owns Kokoro-specific G2P integration, request-local voice/model/style selection, voice blends, token-capacity validation, explicitly configured short-sentence handling, inference, timing reconstruction, waveform validation, tracing, and optional voice-level calibration. discover_models() and discover_lexicons() inspect supported runtime capabilities and lexicon metadata without loading a synthesis session.

Each call returns one request-local RenderedSegment. By default, long_text_split="none" keeps the exact-text path and raises SynthesisInputTooLongError when the prepared text exceeds the model token limit. Set SynthesisConfig.long_text_split="sentence" to enable internal model-safe splitting only when the request is oversized. PhraseSplit is imported lazily for that path; it splits on sentence boundaries, then clauses or safe word boundaries as needed, and joins the audio chunks into one result while preserving the original request text and source-aligned context. long_text_use_spacy=False is the default, so spaCy is not required. Separate caller requests remain separate results, and cross-request composition stays with the caller. See long-text configuration for details.

Structured error diagnostics

Catch typed exceptions for normal control flow. For unexpected backend or enrollment failures, pykokoro.error_diagnostic(exc) provides a JSON-safe cause chain with a stable stage and native error reason:

import json
import pykokoro

with pykokoro.KokoroSynthesizer() as synthesizer:
    try:
        rendered = synthesizer.synthesize_text("Hello.", language="en-us")
    except pykokoro.PyKokoroError as exc:
        print(json.dumps(pykokoro.error_diagnostic(exc), indent=2))

Diagnostics omit raw request text, reference transcripts, audio arrays, and token arrays. See the troubleshooting guide.

Installation

Python 3.10 or newer is required. Choose one ONNX Runtime provider extra per environment:

pip install "pykokoro[cpu]"        # CPU
pip install "pykokoro[gpu]"        # NVIDIA CUDA
pip install "pykokoro[openvino]"   # OpenVINO
pip install "pykokoro[directml]"  # DirectML
pip install "pykokoro[coreml]"     # Apple CoreML

For optional direct playback, install pykokoro[cpu,playback]. PyKokoro writes WAV files through soundfile; RenderedSegment.play() uses the optional sounddevice dependency. Install espeak-ng when selecting an eSpeak frontend or fallback.

Feature guides

Documentation and examples

Run the request-centric examples from the repository root with python examples/run_all.py.

Development

pip install -e ".[dev,cpu]"
python -m pytest

See AGENTS.md for the repository's test, lint, and type-check commands.

Metadata

Release files for pykokoro 0.10.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pykokoro 0.10.9
File Size Uploaded Provenance Actions
pykokoro-0.10.9.tar.gz 2.1 MB Without provenance Details for pykokoro-0.10.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pykokoro 0.10.9
File Interpreter ABI Platform Actions
pykokoro-0.10.9-py3-none-any.whl Python 3 none any Details for pykokoro-0.10.9-py3-none-any.whl

Total release size: 2.2 MB

Release files / pykokoro-0.10.9.tar.gz

Download URL pykokoro-0.10.9.tar.gz
Size 2.1 MB
Tags Source
SHA-256 checksum
How to use checksums
b80414d42bc98d22aab784cfb51cef9ba6a17ac10073e0ab1e9998282203a619
BLAKE2b-256 checksum
How to use checksums
fcd9c3905202d36fe22cc3d7c2be345344a22c967b05595428b151b30ecedaf2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / pykokoro-0.10.9-py3-none-any.whl

Download URL pykokoro-0.10.9-py3-none-any.whl
Size 152.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4305144386de615d538e87ff9ebf4129391a6f3d609f93b97c2726a16778a35
BLAKE2b-256 checksum
How to use checksums
aecc10b7577e0bfc2bc707e5f7c0b991545d2bc03567f3ccb3cffad31420b7a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Security for pykokoro 0.10.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10.9 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page