From 8cac581a904eab65b9036674d8b09925236e8072 Mon Sep 17 00:00:00 2001 From: Henry Su Date: Wed, 12 Aug 2026 22:17:34 -0500 Subject: [PATCH] fix(voice): reject non-positive audio frame rates _buffer_to_audio_file validates sample width, dtype, channel count, and frame completeness, but not the frame rate. A frame rate of 0 or a negative value reached wave.setframerate and surfaced as a low-level wave.Error rather than the UserError contract the other input validations raise. Reject a non-positive frame rate up front so the failure is consistent and actionable. --- src/agents/voice/input.py | 3 +++ tests/voice/test_input.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/agents/voice/input.py b/src/agents/voice/input.py index 63f2313d1b..2ecfb67c83 100644 --- a/src/agents/voice/input.py +++ b/src/agents/voice/input.py @@ -22,6 +22,9 @@ def _buffer_to_audio_file( if sample_width not in {1, 2, 3, 4}: raise UserError("Sample width must be between 1 and 4 bytes") + if frame_rate <= 0: + raise UserError("Frame rate must be greater than zero") + if buffer.dtype not in (np.int16, np.float32): raise UserError("Buffer must be a numpy array of int16 or float32") diff --git a/tests/voice/test_input.py b/tests/voice/test_input.py index 06acf2b4bf..3868931fe1 100644 --- a/tests/voice/test_input.py +++ b/tests/voice/test_input.py @@ -116,6 +116,14 @@ def test_audio_input_rejects_non_positive_channels(channels): audio_input.to_audio_file() +@pytest.mark.parametrize("frame_rate", [0, -8000]) +def test_audio_input_rejects_non_positive_frame_rate(frame_rate): + audio_input = AudioInput(buffer=np.zeros(4, dtype=np.int16), frame_rate=frame_rate) + + with pytest.raises(UserError, match="Frame rate must be greater than zero"): + audio_input.to_audio_file() + + def test_buffer_to_audio_file_invalid_dtype(): # Create a buffer with invalid dtype (float64) buffer = np.array([1.0, 2.0, 3.0], dtype=np.float64)