﻿# Encode variable-length BYTES elements for Triton

Encode BYTES elements with their individual byte lengths, including empty elements, and validate the tensor shape separately.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/guide-triton-bytes-tensor/
Language: en

A Triton client sends a batch of strings as one concatenated byte array. The receiver cannot reconstruct element boundaries, especially when values have different lengths or include zero bytes. A BYTES tensor is an array of length-delimited elements under the binary extension, not an ordinary null-terminated string buffer.

## Keep tensor shape separate from byte length

The tensor shape determines how many elements are present. Each element can contain a different number of bytes. A character count is not the same as an encoded byte count for text outside ASCII. Determine the application encoding first, obtain the actual bytes for each element and preserve the tensor's declared shape independently. A total payload size cannot tell the server how many strings the model expects without that shape contract.

## Encode every element boundary

Triton's binary representation places a 4-byte unsigned length before each BYTES element, followed by that element's bytes, using the documented byte order. The tensor block size includes those length prefixes as well as content. Do not add an implicit terminating character unless it belongs to the actual value. A zero-length element still occupies its length prefix and is different from a missing tensor element.

## Avoid accidental string truncation

Some array or language string types have fixed-width or null-related behavior that can change values before serialization. Use the supported client utility or a deliberately tested encoder that preserves the intended bytes. A round trip through a display string is not a reliable binary conversion. For arbitrary binary content, compare byte sequences rather than decoding as text. Only decode elements whose application schema actually defines a character encoding.

## Build a boundary-focused fixture

Test differently sized text values, an empty element and a value containing a zero byte where the model contract permits arbitrary bytes. Include text with a multibyte encoding. Verify each received element and the total number of elements, not just the concatenated payload. Add a truncated length prefix and an element length extending beyond the block in an isolated decoder test. Rejection should identify malformed input without attempting to read outside the supplied data.

## Reconstruct outputs under the same rules

When the server returns binary BYTES output, walk each length-delimited element within the declared tensor block and verify that parsing consumes the expected data. Do not split on delimiters appearing in model text. Acceptance means exact element preservation, correct shape and bounded handling of malformed sizes. It does not establish that the content is valid for the model's application task: a correctly serialized string can still violate a tokenizer, image decoder or backend-specific input requirement. Keep those validations separate and report the failing layer precisely.

## Primary references

[Triton Binary Tensor Data Extension](https://docs.nvidia.com/deeplearning/triton-inference-server/user-guide/docs/protocol/extension_binary_data.html). Specifies BYTES length prefixes, byte order and binary tensor layout.

[KServe Inference Protocol required APIs](https://github.com/kserve/kserve/blob/master/docs/predict-api/v2/required_api.md). Defines tensor element shape and supported data types.

[Triton gRPC service definition](https://github.com/triton-inference-server/common/blob/main/protobuf/grpc_service.proto). Defines raw and typed tensor-content fields used by the gRPC client representation.

Related reading: [Define variable-length token input contracts for Triton ragged batching](/journal/triton-ragged-token-input-contract/); [Validate tokenizer, inference and postprocessing contracts in Triton ensembles](/journal/triton-tokenizer-ensemble-contract/).
