Registry / serialization / betterproto-rust-codec

betterproto-rust-codec

JSON →
library0.1.1pypypi✓ verified 24d ago

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.

pip install betterproto-rust-codec
INSTALL
IMPORT
SIG · BETTERPROTO-RUST-C
B
betterproto-rust-codec
serializationpythonv0.1.1
Install
1.6s avg
Import
Disk
20MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
build_error
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 22MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

betterproto_rust_codec
import betterproto_rust_codec
from betterproto_rust_codec import ProtobufCodec
deserialize
from betterproto_rust_codec import deserialize
serialize
from betterproto_rust_codec import serialize

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.

import betterproto from betterproto.lib.google.protobuf import Timestamp from betterproto_rust_codec import ProtobufCodec import datetime @betterproto.dataclass class MyMessage(betterproto.Message): name: str = betterproto.string_field(1) value: int = betterproto.int32_field(2) timestamp: Timestamp = betterproto.message_field(3, wraps=True) # Create an instance of the codec codec = ProtobufCodec() # Create a betterproto message now_timestamp = Timestamp().from_datetime(datetime.datetime.now(datetime.timezone.utc)) original_message = MyMessage( name="Test Name", value=123, timestamp=now_timestamp ) print(f"Original message: {original_message}") # Encode the message encoded_bytes = codec.encode(original_message) print(f"Encoded bytes length: {len(encoded_bytes)}") # Decode the message decoded_message = codec.decode(MyMessage, encoded_bytes) # Verify print(f"Decoded message: {decoded_message}") assert original_message.name == decoded_message.name assert original_message.value == decoded_message.value assert original_message.timestamp.seconds == decoded_message.timestamp.seconds print("\nEncode/Decode successful!")
Debug
Known issues
breakingThis 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.
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.
affects: <0.1.1 (and potentially future versions)
breakingSimilarly, `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.
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.
affects: <0.1.1 (and potentially future versions)
gotchaThis 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.
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.
affects: All versions
gotchaWhile 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.
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.
affects: All versions
Upgrade
Version history
0.1.1latest on PyPI · released Apr 13, 2024
Audit
Dependencies
betterprotorequiredRequired for defining Protobuf messages and base functionality. This codec specifically operates on betterproto message types.
protobufrequiredDirect dependency, required for underlying Protobuf type handling.
Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources