Bug description
When input text produces phonemes that are truncated to MAX_PHONEME_LENGTH (510), _create_audio raises an IndexError at line 108:
voice = voice[len(tokens)]
The voice array has shape (510, ...) (valid indices 0–509), but len(tokens) can be exactly 510 after truncation, causing an out-of-bounds access.
Steps to reproduce
from kokoro_onnx import Kokoro
kokoro = Kokoro("kokoro-v1.0.onnx", "voices-v1.0.bin")
# Any text long enough to produce ≥510 phonemes triggers the bug
long_text = "L'intelligenza artificiale sta trasformando radicalmente il modo in cui le aziende operano e competono sul mercato globale. " * 10
samples, sr = kokoro.create(long_text, voice="if_sara", speed=1.0, lang="it")
Error
WARNING [__init__.py:98] Phonemes are too long, truncating to 510 phonemes
Traceback (most recent call last):
...
File "kokoro_onnx/__init__.py", line 201, in create
audio_part, _ = self._create_audio(phonemes, voice, speed)
File "kokoro_onnx/__init__.py", line 108, in _create_audio
voice = voice[len(tokens)]
IndexError: index 510 is out of bounds for axis 0 with size 510
Root cause
In _create_audio (__init__.py):
- Line 101 truncates phonemes to 510 characters:
phonemes = phonemes[:MAX_PHONEME_LENGTH]
- Line 103 tokenizes →
len(tokens) can be exactly 510
- Line 104 asserts
len(tokens) <= MAX_PHONEME_LENGTH → passes (510 <= 510)
- Line 108 indexes
voice[len(tokens)] → voice[510] is out of bounds for an array of size 510
Suggested fix
Clamp the index to prevent out-of-bounds access:
- voice = voice[len(tokens)]
+ voice = voice[min(len(tokens), len(voice) - 1)]
Environment
- kokoro-onnx: 0.5.0
- Python: 3.12.12
- OS: macOS (Darwin 25.3.0)
Bug description
When input text produces phonemes that are truncated to
MAX_PHONEME_LENGTH(510),_create_audioraises anIndexErrorat line 108:The
voicearray has shape(510, ...)(valid indices 0–509), butlen(tokens)can be exactly 510 after truncation, causing an out-of-bounds access.Steps to reproduce
Error
Root cause
In
_create_audio(__init__.py):phonemes = phonemes[:MAX_PHONEME_LENGTH]len(tokens)can be exactly 510len(tokens) <= MAX_PHONEME_LENGTH→ passes (510 <= 510)voice[len(tokens)]→voice[510]is out of bounds for an array of size 510Suggested fix
Clamp the index to prevent out-of-bounds access:
Environment