BentoML
BentoML copied to clipboard
feature: serialization/deserialization with `ormsgpack`
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
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)withmedia_type = "application/vnd.msgpack"(the IANA-registered type).serialize_modelusesormsgpack.packb(model, option=ormsgpack.OPT_SERIALIZE_PYDANTIC)(excludingmultipart_fields, asJSONSerdedoes),deserialize_modeldoescls.model_validate(ormsgpack.unpackb(body)), andserialize_value/deserialize_valuewrappackb/unpackb. It encodes in"python"mode, so tensors and dataframes go through the same_encode/_decodepath as today. - Registration: added to
ALL_SERDEonly whenormsgpackis importable. Without it, the server keeps its current behaviour (unknown media types fall back to JSON), so there is no new hard dependency.ormsgpackwould go in a new optional extra, e.g.bentoml[msgpack]. - Server: no change to
api_endpoint; it already picks the serde fromContent-Typeand 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")/AsyncHTTPClientwould work through the existingmedia_typeargument; 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.