From 8bc3c8914093fe22a6208938fb06d999f0162eec Mon Sep 17 00:00:00 2001 From: Paul Kompfner Date: Wed, 17 Sep 2025 16:09:18 -0400 Subject: [PATCH] Fix a bug preventing usage of multiple `ServiceSwitcher`s in a pipeline --- src/pipecat/pipeline/service_switcher.py | 11 ++++++----- tests/test_service_switcher.py | 13 ------------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/pipecat/pipeline/service_switcher.py b/src/pipecat/pipeline/service_switcher.py index c079bdab4..985f59b3a 100644 --- a/src/pipecat/pipeline/service_switcher.py +++ b/src/pipecat/pipeline/service_switcher.py @@ -61,20 +61,21 @@ class ServiceSwitcherStrategyManual(ServiceSwitcherStrategy): direction: The direction of the frame (upstream or downstream). """ if isinstance(frame, ManuallySwitchServiceFrame): - self._set_active(frame.service) + self._set_active_if_available(frame.service) else: raise ValueError(f"Unsupported frame type: {type(frame)}") - def _set_active(self, service: FrameProcessor): - """Set the active service to the given one. + def _set_active_if_available(self, service: FrameProcessor): + """Set the active service to the given one, if it is in the list of available services. + + If it's not in the list, the request is ignored, as it may have been + intended for another ServiceSwitcher in the pipeline. Args: service: The service to set as active. """ if service in self.services: self.active_service = service - else: - raise ValueError(f"Service {service} is not in the list of available services.") StrategyType = TypeVar("StrategyType", bound=ServiceSwitcherStrategy) diff --git a/tests/test_service_switcher.py b/tests/test_service_switcher.py index b69d1f023..bf80d842e 100644 --- a/tests/test_service_switcher.py +++ b/tests/test_service_switcher.py @@ -100,19 +100,6 @@ class TestServiceSwitcherStrategyManual(unittest.IsolatedAsyncioTestCase): self.assertNotEqual(strategy.active_service, self.service2) self.assertEqual(strategy.active_service, self.service3) - def test_handle_frame_invalid_service(self): - """Test that switching to an invalid service raises an error.""" - strategy = ServiceSwitcherStrategyManual(self.services) - invalid_service = MockFrameProcessor("invalid") - - switch_frame = ManuallySwitchServiceFrame(service=invalid_service) - - with self.assertRaises(ValueError) as context: - strategy.handle_frame(switch_frame, FrameDirection.DOWNSTREAM) - - self.assertIn("Service", str(context.exception)) - self.assertIn("is not in the list of available services", str(context.exception)) - def test_handle_frame_unsupported_frame_type(self): """Test that unsupported frame types raise an error.""" strategy = ServiceSwitcherStrategyManual(self.services)