Skip to main content
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 minimal Truss that accepts and returns binary data. The Truss performs no operations and is purely illustrative.
To create and deploy the example Truss:
  1. Create a Truss:
    Terminal
    This creates a Truss in a new directory binary_test. By default, newly created Trusses implement an identity function that returns the exact input they are given.
  2. Optionally, modify binary_test/model/model.py to log that the data received is of type bytes:
    binary_test/model/model.py
  3. Deploy the Truss to Baseten:
    Terminal

Send raw bytes as model input

To send binary data as model input:
  1. Set the content-type HTTP header to application/octet-stream.
  2. Use msgpack to encode the data or file.
  3. 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.
call_model.py
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:
call_model.py

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.
model/model.py
Then, in your client, use the streaming output directly without decoding:
stream_model.py

Next steps

File I/O

Pass files and URLs as model input and save output to disk

Streaming

Stream text responses token by token