Skip to main content
Send bytes to a custom Truss model with MessagePack, or read raw bytes from a streaming response. The client and model must agree on the payload format. You need a Baseten API key in BASETEN_API_KEY and the Baseten CLI and Truss installed. Install the client dependencies with pip install requests msgpack.

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. Keep the watch process running, wait for the deployment to become active, and use another terminal for the client requests:
    Terminal

Send raw bytes as model input

To send bytes to your model: Configure the request as follows:
  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.
Set BASETEN_MODEL_ID to the identity Truss model ID. Run the following script to send a short byte string and check that the returned bytes match:
The development URL matches the --watch deployment above. For a published production deployment, use /environments/production/predict.
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 save the returned bytes: Continue the previous script and write the decoded byte field to a file:
MessagePack decoding returns the payload your model produced. Use the appropriate field and extension for your model’s output.

Streaming binary outputs

Streaming sends raw response chunks rather than one MessagePack object. Decode them according to the format your model yields. To stream bytes from a Truss:
  1. Replace the model’s predict method with a generator and let the watch process update the deployment:
    model/model.py
  2. Run the following client to save chunks directly and read the completed file:
For application-specific completion records and interrupted streams, see Streaming.

Next steps

File I/O

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

Streaming

Stream text responses token by token