serializer: allow deserialize() to return None

This commit is contained in:
Aleix Conchillo Flaqué
2024-06-12 12:09:22 -07:00
parent 5eb1b90a4b
commit e1b2da1ff0
4 changed files with 13 additions and 4 deletions

View File

@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- `FrameSerializer.deserialize()` can now return `None` in case it is not
possible to desearialize the given data.
- `daily_rest.DailyRoomProperties` now allows extra unknown parameters.
- Added `DeepgramSTTService`. This service has an ongoing websocket

View File

@@ -16,5 +16,5 @@ class FrameSerializer(ABC):
pass
@abstractmethod
def deserialize(self, data: bytes) -> Frame:
def deserialize(self, data: bytes) -> Frame | None:
pass

View File

@@ -11,6 +11,8 @@ import pipecat.frames.protobufs.frames_pb2 as frame_protos
from pipecat.frames.frames import AudioRawFrame, Frame, TextFrame, TranscriptionFrame
from pipecat.serializers.base_serializer import FrameSerializer
from loguru import logger
class ProtobufFrameSerializer(FrameSerializer):
SERIALIZABLE_TYPES = {
@@ -39,7 +41,7 @@ class ProtobufFrameSerializer(FrameSerializer):
result = proto_frame.SerializeToString()
return result
def deserialize(self, data: bytes) -> Frame:
def deserialize(self, data: bytes) -> Frame | None:
"""Returns a Frame object from a Frame protobuf. Used to convert frames
passed over the wire as protobufs to Frame objects used in pipelines
and frame processors.
@@ -61,8 +63,8 @@ class ProtobufFrameSerializer(FrameSerializer):
proto = frame_protos.Frame.FromString(data)
which = proto.WhichOneof("frame")
if which not in self.SERIALIZABLE_FIELDS:
raise ValueError(
"Proto does not contain a valid frame. You may need to add a new case to ProtobufFrameSerializer.deserialize.")
logger.error("Unable to deserialize a valid frame")
return None
class_name = self.SERIALIZABLE_FIELDS[which]
args = getattr(proto, which)

View File

@@ -82,6 +82,10 @@ class WebsocketServerInputTransport(BaseInputTransport):
# Handle incoming messages
async for message in websocket:
frame = self._params.serializer.deserialize(message)
if not frame:
continue
if isinstance(frame, AudioRawFrame):
self.push_audio_frame(frame)
else: