How to do model I/O in binary
Decode and save binary model output
Baseten and Truss natively support model I/O in binary and use msgpack encoding for efficiency.
Deploy a basic Truss for binary I/O
If you need a deployed model to try the invocation examples below, follow these steps to create and deploy a super basic Truss that accepts and returns binary data. The Truss performs no operations and is purely illustrative.
Send raw bytes as model input
To send binary data as model input:
- Set the
content-type
HTTP header toapplication/octet-stream
- Use
msgpack
to encode the data or file - Make a POST request to the model
This code sample assumes you have a file Gettysburg.mp3
in the current working directory. You can download the 11-second file from our CDN or replace it with your own file.
To support certain types like numpy and datetime values, you may need to extend client-side msgpack
encoding with the same encoder and decoder used by Truss.
Parse raw bytes from model output
To use the output of a non-streaming model response, decode the response content.
Streaming binary outputs
You can also stream output as binary. This is useful for sending large files or reading binary output as it is generated.
In the model.py
, you must create a streaming output.
Then, in your client, you can use streaming output directly without decoding.