fix(vad): ignore empty audio chunks instead of crashing #854
Looks correct to me. The guard in VADHandler.process is in the right place, and the int2float change is harmless.
I ran this in Docker (python:3.11, CPU torch): tests/test_int2float_empty.py, tests/test_vad_iterator.py, tests/test_vad_session_reset.py, tests/test_vad_model_state.py, tests/test_vad_confident_turn_opening.py and tests/test_speculative_turns.py. All 139 pass on the PR head.
Two notes, neither blocking.
First, the size check at VAD/vad_handler.py:630 now covers the only caller of int2float. So the max(initial=0) change at utils/utils.py:133 is purely defensive. It doesn't hurt, but the PR text describes it as the fix.
Second, I couldn't find a path that sends an empty chunk today. The realtime transport splits audio in append_pcm (api/openai_realtime/handlers/audio.py:329-336), and that only forwards full 512-sample chunks. Everything shorter stays in audio_remainder. If you have a concrete trigger (a client, or the local audio streamer), naming it in the description would help a reviewer judge whether this is a bug fix or hardening. If the goal is hardening, an odd-length chunk is the next thing that would crash the same line: np.frombuffer(b"\x00", dtype=np.int16) raises ValueError. The realtime path never produces one, because its chunks are always CHUNK_SIZE_BYTES. Any other producer could.
Good changes, thank you!
<img src="https://static.klipy.com/ii/f87f46a2c5aeaeed4c68910815f73eaf/26/9d/0gsfWjN3.gif"/>
andimarafioti
CJstate
Hardening:
VADHandler.processnow drops a zero-length PCM chunk instead ofcrashing the VAD thread.
Before this change an empty chunk reached
int2float, whose.max()raisesValueError: zero-size array to reduction operation maximum which has no. No current producer sends one: every VAD input goes throughidentity
append_pcm(api/openai_realtime/handlers/audio.py), which only forwards full512-sample chunks. So this is defensive, not a fix for an observed crash.
Changes:
VAD/vad_handler.py: the fix. Return early on an empty chunk, before thechunk/sample counters and the VAD model call.
utils/utils.py: defence in depth.np.abs(sound).max(initial=0)makesint2floatreturn an empty float32 array for empty input. Non-empty input isunchanged (
abs_maxis still the true peak).tests/test_int2float_empty.py: covers both. Both tests fail onmainandpass with this change.
Out of scope: an odd-length buffer still raises in
np.frombuffer; no currentproducer sends one.
Validation:
pytest tests/test_int2float_empty.py tests/test_vad_iterator.py tests/test_vad_session_reset.py tests/test_vad_model_state.py tests/test_vad_confident_turn_opening.py tests/test_speculative_turns.py: 156 passed (PR merged with currentmain)