{"id":2411,"library":"betterproto-rust-codec","title":"betterproto-rust-codec (Rust-backed Protobuf Codec)","description":"betterproto-rust-codec provides a high-performance codec for `betterproto` messages, leveraging a Rust implementation to serialize and deserialize Protobuf wire format. It significantly speeds up conversion compared to Python-native alternatives, especially for large or frequently processed messages. The current version is 0.1.1, and releases are generally ad-hoc, often in response to `betterproto` updates or targeted performance improvements.","status":"active","version":"0.1.1","language":"en","source_language":"en","source_url":"https://github.com/dancmc/betterproto-rust-codec","tags":["protobuf","serialization","performance","rust","betterproto","codec"],"install":[{"cmd":"pip install betterproto-rust-codec","lang":"bash","label":"Install betterproto-rust-codec"}],"dependencies":[{"reason":"Required for defining Protobuf messages and base functionality. This codec specifically operates on betterproto message types.","package":"betterproto","optional":false},{"reason":"Direct dependency, required for underlying Protobuf type handling.","package":"protobuf","optional":false}],"imports":[{"symbol":"ProtobufCodec","correct":"from betterproto_rust_codec import ProtobufCodec"}],"quickstart":{"code":"import betterproto\nfrom betterproto.lib.google.protobuf import Timestamp\nfrom betterproto_rust_codec import ProtobufCodec\nimport datetime\n\n@betterproto.dataclass\nclass MyMessage(betterproto.Message):\n    name: str = betterproto.string_field(1)\n    value: int = betterproto.int32_field(2)\n    timestamp: Timestamp = betterproto.message_field(3, wraps=True)\n\n# Create an instance of the codec\ncodec = ProtobufCodec()\n\n# Create a betterproto message\nnow_timestamp = Timestamp().from_datetime(datetime.datetime.now(datetime.timezone.utc))\noriginal_message = MyMessage(\n    name=\"Test Name\",\n    value=123,\n    timestamp=now_timestamp\n)\n\nprint(f\"Original message: {original_message}\")\n\n# Encode the message\nencoded_bytes = codec.encode(original_message)\nprint(f\"Encoded bytes length: {len(encoded_bytes)}\")\n\n# Decode the message\ndecoded_message = codec.decode(MyMessage, encoded_bytes)\n\n# Verify\nprint(f\"Decoded message: {decoded_message}\")\nassert original_message.name == decoded_message.name\nassert original_message.value == decoded_message.value\nassert original_message.timestamp.seconds == decoded_message.timestamp.seconds\nprint(\"\\nEncode/Decode successful!\")","lang":"python","description":"This quickstart demonstrates how to define a `betterproto` message, instantiate the `ProtobufCodec`, and then use it to efficiently encode and decode the message to and from Protobuf wire format bytes."},"warnings":[{"fix":"Upgrade `betterproto` to the required version: `pip install betterproto>=\"2.0.0b6\"` or check the `pyproject.toml` of `betterproto-rust-codec` for the exact requirement.","message":"This library has strict dependency requirements for `betterproto`. Ensure your `betterproto` version meets the minimum requirement specified by `betterproto-rust-codec` (currently `>=2.0.0b6`). Using incompatible versions can lead to runtime errors or incorrect serialization.","severity":"breaking","affected_versions":"<0.1.1 (and potentially future versions)"},{"fix":"Upgrade `protobuf` to the required version: `pip install protobuf>=\"3.20.0\"` or check the `pyproject.toml` of `betterproto-rust-codec` for the exact requirement.","message":"Similarly, `betterproto-rust-codec` also has a minimum requirement for the `protobuf` library itself (currently `>=3.20.0`). Incompatible `protobuf` versions might cause issues with underlying type conversions or wire format interpretation.","severity":"breaking","affected_versions":"<0.1.1 (and potentially future versions)"},{"fix":"Ensure you are using `betterproto.Message` subclasses. If working with non-betterproto messages, use the standard `protobuf` library's encoding/decoding or convert messages to `betterproto` format first.","message":"This codec is designed exclusively for `betterproto` messages. It will not work directly with messages generated by `protoc` or other standard `protobuf` Python libraries without first converting them to `betterproto` types.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Evaluate your performance needs. If speed is paramount for Protobuf serialization/deserialization, this library is highly recommended. Otherwise, Python-native `betterproto` encoding/decoding might suffice.","message":"While providing significant performance benefits, `betterproto-rust-codec` introduces a Rust dependency (compiled into a wheel). For extremely simple use cases or environments where Rust compilation might be an issue (e.g., highly restricted embedded systems, though pre-built wheels usually mitigate this), the overhead might not be justified if performance isn't a critical concern.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}