serializer: allow deserialize() to return None
This commit is contained in:
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
### Changed
|
### 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.
|
- `daily_rest.DailyRoomProperties` now allows extra unknown parameters.
|
||||||
|
|
||||||
- Added `DeepgramSTTService`. This service has an ongoing websocket
|
- Added `DeepgramSTTService`. This service has an ongoing websocket
|
||||||
|
|||||||
@@ -16,5 +16,5 @@ class FrameSerializer(ABC):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def deserialize(self, data: bytes) -> Frame:
|
def deserialize(self, data: bytes) -> Frame | None:
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import pipecat.frames.protobufs.frames_pb2 as frame_protos
|
|||||||
from pipecat.frames.frames import AudioRawFrame, Frame, TextFrame, TranscriptionFrame
|
from pipecat.frames.frames import AudioRawFrame, Frame, TextFrame, TranscriptionFrame
|
||||||
from pipecat.serializers.base_serializer import FrameSerializer
|
from pipecat.serializers.base_serializer import FrameSerializer
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
|
||||||
class ProtobufFrameSerializer(FrameSerializer):
|
class ProtobufFrameSerializer(FrameSerializer):
|
||||||
SERIALIZABLE_TYPES = {
|
SERIALIZABLE_TYPES = {
|
||||||
@@ -39,7 +41,7 @@ class ProtobufFrameSerializer(FrameSerializer):
|
|||||||
result = proto_frame.SerializeToString()
|
result = proto_frame.SerializeToString()
|
||||||
return result
|
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
|
"""Returns a Frame object from a Frame protobuf. Used to convert frames
|
||||||
passed over the wire as protobufs to Frame objects used in pipelines
|
passed over the wire as protobufs to Frame objects used in pipelines
|
||||||
and frame processors.
|
and frame processors.
|
||||||
@@ -61,8 +63,8 @@ class ProtobufFrameSerializer(FrameSerializer):
|
|||||||
proto = frame_protos.Frame.FromString(data)
|
proto = frame_protos.Frame.FromString(data)
|
||||||
which = proto.WhichOneof("frame")
|
which = proto.WhichOneof("frame")
|
||||||
if which not in self.SERIALIZABLE_FIELDS:
|
if which not in self.SERIALIZABLE_FIELDS:
|
||||||
raise ValueError(
|
logger.error("Unable to deserialize a valid frame")
|
||||||
"Proto does not contain a valid frame. You may need to add a new case to ProtobufFrameSerializer.deserialize.")
|
return None
|
||||||
|
|
||||||
class_name = self.SERIALIZABLE_FIELDS[which]
|
class_name = self.SERIALIZABLE_FIELDS[which]
|
||||||
args = getattr(proto, which)
|
args = getattr(proto, which)
|
||||||
|
|||||||
@@ -82,6 +82,10 @@ class WebsocketServerInputTransport(BaseInputTransport):
|
|||||||
# Handle incoming messages
|
# Handle incoming messages
|
||||||
async for message in websocket:
|
async for message in websocket:
|
||||||
frame = self._params.serializer.deserialize(message)
|
frame = self._params.serializer.deserialize(message)
|
||||||
|
|
||||||
|
if not frame:
|
||||||
|
continue
|
||||||
|
|
||||||
if isinstance(frame, AudioRawFrame):
|
if isinstance(frame, AudioRawFrame):
|
||||||
self.push_audio_frame(frame)
|
self.push_audio_frame(frame)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user