Versioned, extensible wire serialization protocol for Sinapsis data containers.
pip install sinapsis_protocol Copy PIP instructions
sinapsis-protocol implements Universal Data Transport (UDT): a Python protocol for serializing, transporting, and reconstructing Sinapsis DataContainer objects and their nested packets.
The protocol standardizes the container and wire representation while allowing individual payloads to use the format best suited to their data. MessagePack is the common wire format; NumPy, Torch, Apache Arrow, Protobuf, or an application-defined representation can be used within a payload, without replacing the DataContainer boundary.
Core principle: Standardize the container. Optimize the payload.
DataContainer and packet types belong to sinapsis-core and are exposed through the stable sinapsis.data_transport interface. sinapsis-protocol is responsible for their transport representation. It is not a general-purpose Python object serializer and does not implicitly serialize arbitrary Python classes.
Sinapsis data may cross Agent, process, worker, or service boundaries. Its transport contract needs to be:
AI and ML workloads frequently exchange arrays, tensors, images, audio, and tabular data, rather than just small text objects. The cost of converting those payloads into a generic interchange format can become significant when Templates exchange data repeatedly or stream results at high frequency.
Consider a NumPy float32 array with shape (1024, 1024). Its numerical content occupies 4 MiB as raw values. Turning it into a JSON array would require expanding a million values into decimal text, allocating a much larger intermediate representation in many cases, parsing those strings on receipt, and reconstructing the original dtype and shape. JSON is useful for human-readable configuration and API metadata, but it is not an efficient default for large numerical payloads.
sinapsis-protocol instead separates structure from content:
The receiver uses the descriptor to choose the correct decoder and reconstruct the supported Python value. The outer DataContainer remains the same regardless of the payload codec.
JSON-oriented numerical transport Sinapsis UDT ------------------------------------ --------------------------------- array → Python numbers → JSON text array → binary bytes + dtype/shape → parse numbers → rebuild array → MessagePack metadata → rebuild typed array Why it helps: less numeric-to-text conversion, fewer large temporary objects, and less time spent encoding and parsing payloads. That matters for real-time video, high-throughput inference, time-series streams, and distributed model training. Payload size, CPU cost, copying, and network latency all affect the result; this is a design choice, not a promise of zero-copy operation or a fixed speedup. For example, adapters may make an array contiguous or move a tensor to CPU memory before encoding.
A Torch tensor does not need to become a list simply because a downstream Template uses NumPy, and a DataFrame does not need to be converted to a list of dictionaries for transport. Each codec preserves the useful native binary representation and type metadata as far as its implementation allows. Compatibility is enforced at the container and codec boundaries, rather than by forcing every library into the least expressive format.
flowchart TD A[Sinapsis DataContainer] --> B[Recursive dataclass walker] B --> C[Nested DataPackets / dataclasses] C --> D{Payload} D -->|NumPy| E[NumPy payload codec] D -->|Torch| F[Torch payload codec] D -->|Pandas / Polars| G[Arrow payload codec] D -->|bytes / string / path| H[Core payload representation] D -->|Protobuf / custom type| I[Registered payload codec] E --> J[Encoded payload] F --> J G --> J H --> J I --> J J --> K[DataContainer wire representation] K --> L{Payload size} L -->|small| M[Inline payload] L -->|large| N[ArtifactRef] M --> O[Versioned MessagePack wire format] N --> O O --> P[Wire representation] P --> Q[Transport] Q --> R[Deserializer] R --> S[Trusted dataclass / payload codec registry] S --> T[Reconstructed DataContainer] Protobuf and other specialised formats are payload codecs. They encode individual payloads inside packets; they do not bypass the generic UDT container or its MessagePack envelope.
DataContainer ↓ recursive dataclass walker ↓ nested packet dataclasses ↓ leaf-value dispatch ↓ typed payload representation / inline bytes / ArtifactRef ↓ versioned MessagePack envelope ↓ transport ↓ registered decoding and reconstruction A specialised application may deliberately choose a separate, fixed contract:
Application-specific contract ↓ Protobuf schema ↓ Protobuf bytes That is appropriate when both endpoints explicitly agree on a fixed schema and do not require the general DataContainer protocol. It is a different choice from embedding a Protobuf payload inside UDT.
Requires Python 3.13+. Install directly from the standard Python package index:
uv pip install sinapsis-protocol # or pip install sinapsis-protocol Optional payload adapters:
uv pip install 'sinapsis-protocol[numpy]' uv pip install 'sinapsis-protocol[torch]' uv pip install 'sinapsis-protocol[pandas]' uv pip install 'sinapsis-protocol[polars]' uv pip install 'sinapsis-protocol[cv2]' # All bundled optional adapters uv pip install 'sinapsis-protocol[all]' msgpack is a required dependency. Optional libraries are not required when transferring ordinary container metadata, text, or bytes.
The main entry points serialize and deserialize a Sinapsis container:
from sinapsis.data_transport import DataContainer, TextPacket from sinapsis_protocol import deserialize_data_container, serialize_data_container container = DataContainer(texts=[TextPacket(content="Hello from Sinapsis")]) packet: bytes = serialize_data_container(container) restored = deserialize_data_container(packet) assert isinstance(restored, DataContainer) assert restored.texts[0].content == "Hello from Sinapsis" serialize and deserialize are equivalent general entry points for supported values. The *_data_container names make the intended UDT boundary explicit.
import numpy as np from sinapsis.data_transport import DataContainer, ImagePacket, TextPacket from sinapsis_protocol import deserialize_data_container, serialize_data_container container = DataContainer( images=[ ImagePacket(content=np.arange(300, dtype=np.float32).reshape(3, 10, 10)) ], texts=[TextPacket(content="example")], ) packet = serialize_data_container(container) restored = deserialize_data_container(packet) assert isinstance(restored.images[0].content, np.ndarray) assert restored.images[0].content.shape == (3, 10, 10) The serializer traverses the logical tree recursively:
DataContainer ├── images │ └── ImagePacket │ ├── content ── NumPy adapter → binary payload │ ├── id │ ├── source │ └── other packet fields └── texts └── TextPacket └── content ── string The registered adapter applies to the leaf value. All other dataclass fields are encoded as part of the same recursive structure, including packet identifiers, metadata, and provenance fields when present.
An empty DataContainer is an intentional fast path:
A normal encoded container is a versioned MessagePack envelope. Its conceptual structure is:
{ "protocol": "sinapsis-data", "major": 1, "minor": 0, "kind": "container", "payload": ..., } The fields separate protocol identity, compatibility version, message kind, and the encoded data tree. The top-level structure is consistent even when its leaf values use different codecs.
A registered dataclass is represented conceptually as:
{ "__type__": "dataclass", "version": 1, "name": "myapp:my-custom-packet", "fields": { "field_a": ..., "field_b": ..., }, } The name and version select a type from the receiver's explicit dataclass registry. The Python module path need not be part of the wire contract.
Internally, the protocol uses typed dataclasses to describe payloads and their representations. A simplified view of the actual model structure is:
TransportValue ├── descriptor: PayloadDescriptor │ ├── kind │ ├── version │ ├── library │ └── codec ├── metadata: Metadata │ └── type-specific fields (shape, dtype, device, ...) └── payload: Payload ├── kind: INLINE | ARTIFACT ├── inline: InlinePayload | None ├── artifact: ArtifactPayload | None └── extras ArrayMetadata, TorchMetadata, ImageMetadata, DataFrameMetadata, and other specialised metadata classes capture representation-specific details. The typed models are converted to ordinary MessagePack-compatible mappings at the final wire boundary.
An encoded NumPy value looks conceptually like:
{ "__type__": "array", "version": 1, "library": "numpy", "codec": "raw", "metadata": { "shape": [3, 10, 10], "dtype": "<f4", "order": "C", }, "payload": { "kind": "inline", "inline": {"data": b"...", "sha256": "..."}, }, } Payload metadata is necessary for decoding: an array's byte sequence is insufficient without shape and dtype information. Protocol models also carry format and version metadata to distinguish specialised representations.
Not every exchange carries application data. A runtime may need to signal no content, a heartbeat, or the end of a stream many times per second. Wrapping each of those events in a full DataContainer and a normal wire envelope would waste bytes, allocations, and encoding/decoding work.
For that reason, the protocol reserves MessagePack extension code 42 for compact control values. Each sentinel is encoded as a three-byte fixext 1 value:
0xD4 0x2A 0x01 ... 0x05 fixext1 code 42 sentinel value The decoder can distinguish the control message from a normal container without walking a dataclass graph, decoding payload metadata, or reconstructing packets. This is especially valuable for frequent empty results, stream heartbeats, and completion signalling, where control-message overhead can otherwise dominate useful work.
The non-empty sentinel values are available to protocol consumers; their use depends on the surrounding transport and application. The protocol does not imply that every stream automatically emits heartbeats or an end marker.
from sinapsis_protocol import ( ProtocolSentinel, decode_sentinel, encode_sentinel, is_sentinel_packet, ) heartbeat: bytes = encode_sentinel(ProtocolSentinel.HEARTBEAT) assert len(heartbeat) == 3 assert is_sentinel_packet(heartbeat) assert ProtocolSentinel(decode_sentinel(heartbeat)) is ProtocolSentinel.HEARTBEAT is_sentinel_packet is a cheap check for the sentinel wire shape; use the decoder to validate the extension payload and its known value. A normal container still uses the versioned MessagePack envelope and the usual registered decoding path.
Control plane versus data plane: sentinels communicate protocol state; DataContainer and packet values communicate application data. Treating them separately avoids paying the full data-serialization cost for messages that have no payload.
The wire format describes how data is encoded. A wire envelope describes the context of the exchange carrying that data. The two concerns are distinct.
The package provides SSEEnvelope for API/SSE-style exchanges:
SSEEnvelope ├── agent_id ├── agent_status ├── event_type ├── request_type ├── job_id ├── errors └── data → DataContainer | None data is optional. When present, it represents content available in that event, not necessarily the complete result. For example, a streaming audio workflow may emit an envelope containing just the current audio chunk.
For WebSocket transports that prefix an opaque encoded envelope with a MessagePack metadata map, split_websocket_frame separates the metadata and returns a view of the remaining envelope bytes. It does not itself decode the complete SSEEnvelope.
Wire envelope = exchange context. Wire format = encoded data representation.
Large binary payloads need not be embedded in a single MessagePack allocation. The protocol separates the message representation from payload storage.
The encoder decides between an inline representation and an external artifact reference according to the payload and the configured thresholds:
Payload bytes ├── within inline limit → InlinePayload └── above inline limit → ArtifactStore → ArtifactRef The reference identifies the offloaded content; the receiving side must use a store capable of resolving it.
A pathlib.Path is treated as a file payload. Plain strings are not automatically interpreted as paths unless the corresponding SerializationConfig path options are enabled.
Optional adapters are loaded lazily. A caller can register an explicit override when another representation is more appropriate for a recognised type.
Use dataclass registration when an object should be recursively traversed, including its nested fields:
from sinapsis_protocol import deserialize_data_container restored = deserialize_data_container(packet, registry=registry) Stable names matter Prefer a stable wire identity such as myapp:my-custom-packet over a Python import path such as my_package.models.MyCustomPacket. Python modules can be reorganised without changing the transport identity.
When an incompatible schema change is needed, give the new structure a new version:
registry.register_dataclass( MyCustomPacketV2, type_name="myapp:my-custom-packet", version=2, ) Versions identify distinct registered schemas. Compatibility migrations between versions must be implemented intentionally; they are not inferred by the decoder.
Use a leaf adapter when the object should be handled as a single specialised payload rather than recursively walking its Python fields. The adapter decides how to encode and reconstruct the value.
The following example assumes an application-defined MyEmbedding type with to_bytes() and from_bytes() methods:
from sinapsis_protocol import default_registry from sinapsis_protocol.codecs.base import make_payload, read_payload from sinapsis_protocol.types import DecodeContext, EncodeContext from sinapsis_protocol.wire.constants import PayloadCodec from sinapsis_protocol.wire.models import ( Metadata, PayloadDescriptor, TransportValue, ) class MyEmbeddingAdapter: type_name = "embedding" library = "myapp" version = 1 @classmethod def can_encode(cls, value: object) -> bool: return isinstance(value, MyEmbedding) @classmethod def encode(cls, value: object, context: EncodeContext) -> TransportValue: return TransportValue( descriptor=PayloadDescriptor( kind=cls.type_name, version=cls.version, library=cls.library, codec=PayloadCodec.RAW, ), metadata=Metadata(), payload=make_payload( value.to_bytes(), context, filename="embedding.bin", mime_type="application/octet-stream", ), ) @classmethod def decode(cls, value: TransportValue, context: DecodeContext) -> object: return MyEmbedding.from_bytes(read_payload(value.payload, context)) registry = default_registry() registry.register(MyEmbeddingAdapter) Pass this registry to serialize_data_container and deserialize_data_container. An explicit adapter can override the corresponding built-in lazy adapter when its (type_name, library, version) matches.
Protobuf is useful for a compact, language-neutral representation of a particular payload. Within UDT it belongs inside a packet, for example through a registered adapter:
DataContainer └── MyProtoPacket └── protobuf message ↓ protobuf serialization ↓ payload bytes ↓ MessagePack DataContainer wire representation Likewise, NumPy may use raw binary, DataFrames use Arrow IPC, and other objects use their declared payload formats. None of these require replacing the universal container boundary.
Specialize the payload, not the universal container boundary.
AdapterRegistry ├── DataclassRegistry │ └── explicitly registered structured packet types └── Leaf adapters ├── NumPy ├── Torch ├── Pandas ├── Polars ├── OpenCV └── application overrides / custom codecs The registry resolves known dataclasses by wire name and version and payload adapters by kind, library, and version. The default registry registers Sinapsis's public data-transport dataclasses and lazy built-in adapters. Applications can extend it explicitly.
If a value is not a supported primitive, registered dataclass, or recognised adapter type, encoding raises UnsupportedTypeError rather than silently pickling the object.
SerializationConfig provides inline-handling controls and a SerializationLimits object. Supported limits include:
from sinapsis_protocol import SerializationConfig from sinapsis_protocol.types import SerializationLimits config = SerializationConfig( inline_binary_limit=1024 * 1024, limits=SerializationLimits(max_nesting_depth=32), ) For supported binary arrays, decoding validates expected byte length from shape and dtype before constructing the array. Inline payloads and local artifacts can carry integrity checksums.
The format has no implicit pickle/cloudpickle fallback and does not dynamically import arbitrary Python classes based on packet contents. Nonetheless, applications should register only trusted types/adapters, select limits appropriate to their workloads, and restrict artifact-store access as appropriate. Registry allow-listing and resource limits are complementary safeguards, not a substitute for application-level authorization.
The wire protocol version is separate from the Python distribution version.
Included: recursive container/dataclass representation, MessagePack framing, typed payload metadata, NumPy/Torch/Pandas/Polars/OpenCV adapters, inline and artifact-backed payloads, explicit custom types, configurable resource limits, streaming envelopes, and protocol sentinels.
Not a goal: arbitrary object graphs, live processes, automatic transport of torch.nn.Module objects, implicit pickle execution, or a separate top-level protocol for every data type. An application-specific Protobuf contract remains possible, but it is not the generic UDT contract.
sinapsis-serializer-core and sinapsis-deserialization use an older, separate representation for existing Cloud API integrations; protocol bytes should be decoded using the matching sinapsis-protocol decoder.
src/sinapsis_protocol/ ├── container/ # DataContainer integration and serialization config ├── wire/ # MessagePack, typed wire models, envelopes, sentinels, frames ├── dataclass/ # Recursive dataclass encoding and registration ├── codecs/ # Built-in optional adapters and codec registry ├── artifacts/ # ArtifactStore, ArtifactRef, local staging ├── types.py # Limits and adapter/context contracts └── errors.py # Protocol-specific errors Development pip install -e '.[dev]' pytest The test suite covers wire encoding, dataclass registries, supported payload adapters, artifacts, sentinel handling, and SSE envelopes. Integration tests using the supported Sinapsis data-transport models are particularly important for compatibility.
Sinapsis open-source components are available under your choice of the MIT License or the Apache License 2.0, unless otherwise noted.
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
None of the files in this release were uploaded with provenance metadata.
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.




