diff --git a/src/pipecat/frames/frames.py b/src/pipecat/frames/frames.py index 17059700a..7b2dc8aaa 100644 --- a/src/pipecat/frames/frames.py +++ b/src/pipecat/frames/frames.py @@ -79,7 +79,10 @@ class AudioRawFrame: audio: bytes sample_rate: int num_channels: int - num_frames: int = field(init=False) + num_frames: int = field(default=0, init=False) + + def __post_init__(self): + self.num_frames = int(len(self.audio) / (self.num_channels * 2)) @dataclass diff --git a/src/pipecat/serializers/protobuf.py b/src/pipecat/serializers/protobuf.py index 2adf403a5..654e87b24 100644 --- a/src/pipecat/serializers/protobuf.py +++ b/src/pipecat/serializers/protobuf.py @@ -34,10 +34,11 @@ class ProtobufFrameSerializer(FrameSerializer): # ignoring linter errors; we check that type(frame) is in this dict above proto_optional_name = self.SERIALIZABLE_TYPES[type(frame)] # type: ignore + proto_attr = getattr(proto_frame, proto_optional_name) for field in dataclasses.fields(frame): # type: ignore value = getattr(frame, field.name) - if value: - setattr(getattr(proto_frame, proto_optional_name), field.name, value) + if value and hasattr(proto_attr, field.name): + setattr(proto_attr, field.name, value) result = proto_frame.SerializeToString() return result