BentoML icon indicating copy to clipboard operation
BentoML copied to clipboard

feature: serialization/deserialization with `ormsgpack`

Open judahrand opened this issue 2 years ago • 1 comments

Feature request

This would be much faster than JSON (even using Pydantic) and could be automatically detected/used based on the offical mime type application/vnd.mspack. ormsgpack also natively serializes Pydantic BaseModels so that should make life fairly easy.

Motivation

Faster serialization / deserialization.

Other

No response

judahrand avatar Jun 11 '24 14:06 judahrand

I'd like to work on this. Here is a concrete proposal so a maintainer can confirm the direction (it adds an optional dependency) before I open a PR:

  • New serde in src/_bentoml_impl/serde.py: MsgpackSerde(GenericSerde, Serde) with media_type = "application/vnd.msgpack" (the IANA-registered type). serialize_model uses ormsgpack.packb(model, option=ormsgpack.OPT_SERIALIZE_PYDANTIC) (excluding multipart_fields, as JSONSerde does), deserialize_model does cls.model_validate(ormsgpack.unpackb(body)), and serialize_value / deserialize_value wrap packb / unpackb. It encodes in "python" mode, so tensors and dataframes go through the same _encode / _decode path as today.
  • Registration: added to ALL_SERDE only when ormsgpack is importable. Without it, the server keeps its current behaviour (unknown media types fall back to JSON), so there is no new hard dependency. ormsgpack would go in a new optional extra, e.g. bentoml[msgpack].
  • Server: no change to api_endpoint; it already picks the serde from Content-Type and uses the same serde for the response. Unlike pickle, msgpack is safe to accept on the main server, so it would not be added to the pickle guard.
  • Client: SyncHTTPClient(..., media_type="application/vnd.msgpack") / AsyncHTTPClient would work through the existing media_type argument; I'd make sure the response decoding path uses the serde for msgpack responses too.
  • Tests: unit tests (a new tests/unit/_bentoml_impl/test_serde.py) for round-trips of a pydantic IO model, plain values, and the "ormsgpack not installed" fallback (patched import), all offline.

Does this fit how you'd want it done (optional extra vs. a hard dependency, and whether the client should expose it)? I'll wait for a go-ahead before sending the PR.

Rutvij97 avatar Nov 12 '26 16:11 Rutvij97