From 63083cd02157a59ab55be518c4fcd484b8f5bae6 Mon Sep 17 00:00:00 2001 From: James Hush Date: Thu, 5 Feb 2026 10:11:10 +0800 Subject: [PATCH] Add validation for num_10ms_chunks parameter Ensure num_10ms_chunks is a positive integer to prevent division by zero or invalid audio chunk sizes. Raises ValueError if value is less than 1. --- src/pipecat/transports/smallwebrtc/transport.py | 5 +++++ tests/test_smallwebrtc_transport.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/pipecat/transports/smallwebrtc/transport.py b/src/pipecat/transports/smallwebrtc/transport.py index 4740496ff..9712df3b7 100644 --- a/src/pipecat/transports/smallwebrtc/transport.py +++ b/src/pipecat/transports/smallwebrtc/transport.py @@ -84,7 +84,12 @@ class RawAudioTrack(AudioStreamTrack): Args: sample_rate: The audio sample rate in Hz. num_10ms_chunks: Number of 10ms chunks per output frame (default 1). + + Raises: + ValueError: If num_10ms_chunks is not a positive integer. """ + if num_10ms_chunks < 1: + raise ValueError(f"num_10ms_chunks must be a positive integer, got {num_10ms_chunks}") super().__init__() self._sample_rate = sample_rate self._num_10ms_chunks = num_10ms_chunks diff --git a/tests/test_smallwebrtc_transport.py b/tests/test_smallwebrtc_transport.py index f47d873d7..1712ea1af 100644 --- a/tests/test_smallwebrtc_transport.py +++ b/tests/test_smallwebrtc_transport.py @@ -137,3 +137,17 @@ class TestRawAudioTrack: for sample_rate, num_chunks, expected_bytes in test_cases: track = RawAudioTrack(sample_rate=sample_rate, num_10ms_chunks=num_chunks) assert track._bytes_per_chunk == expected_bytes + + def test_invalid_num_10ms_chunks_zero(self): + """Test that num_10ms_chunks=0 raises ValueError.""" + with pytest.raises(ValueError) as exc_info: + RawAudioTrack(sample_rate=16000, num_10ms_chunks=0) + + assert "positive integer" in str(exc_info.value) + + def test_invalid_num_10ms_chunks_negative(self): + """Test that negative num_10ms_chunks raises ValueError.""" + with pytest.raises(ValueError) as exc_info: + RawAudioTrack(sample_rate=16000, num_10ms_chunks=-1) + + assert "positive integer" in str(exc_info.value)