﻿# Calculate Triton binary tensor offsets from the JSON header

Compute Triton header offsets from the exact encoded JSON prefix and preserve one tensor order for metadata and payload.

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

A custom Triton HTTP client sends a JSON inference header followed by binary tensors. The server rejects apparently valid input or reads the wrong values after the JSON changes. The binary payload starts at a byte offset, so the size of the serialized header must be measured after encoding, not guessed from character count or an earlier object layout.

## Construct the final header first

Build the complete inference request object with tensor names, shapes, data types and binary-size parameters. Serialize it using the intended JSON encoder, then encode those exact bytes for transmission. Only now compute the value for Inference-Header-Content-Length. A later change in whitespace, escaping or a non-ASCII string can change byte length. Do not compute the offset from the in-memory document before its final serialization.

## Keep total and header lengths distinct

The special inference header length describes the JSON prefix. Ordinary HTTP Content-Length, when used, describes the complete body containing that prefix and the binary tail. They answer different questions. Let the HTTP library frame the assembled body consistently. An adapter that updates the JSON length but leaves a stale total length can still create a malformed message. Store the calculated lengths beside a safe fixture digest so the test can verify the exact transmitted body.

## Order the tensor payloads deliberately

The binary blocks follow the order of tensors described in the JSON, under Triton's extension rules. Determine each block's byte count from its actual encoded representation. A dictionary iteration order or a separate list sorted by tensor name can diverge from the serialized declaration. Keep one authoritative tensor sequence for both metadata and payload assembly. Shape and data-type validation remains necessary even when every offset falls within the HTTP body.

## Test a header whose byte length changes

Use small synthetic tensors and change only the serialized metadata, including a supported non-ASCII request identifier or harmless whitespace. Verify that the binary decoder still receives the same tensor values. Add a deliberately incorrect header length and confirm clear rejection in an isolated fixture. This distinguishes boundary calculation from a model numerical error without needing a large checkpoint or expensive generation workload.

## Validate before sending and after receiving

Assert that the assembled prefix length equals the advertised header length and that the sum of binary blocks matches the declared sizes. For a binary response, apply the received boundary rather than searching for the last JSON brace inside arbitrary bytes. Acceptance means stable round-trip tensor values under serialization changes and predictable rejection of malformed boundaries. It does not prove the tensor names or shapes match every model version; those belong to the effective model configuration and should be checked as a separate contract.

## Primary references

[Triton Binary Tensor Data Extension](https://docs.nvidia.com/deeplearning/triton-inference-server/user-guide/docs/protocol/extension_binary_data.html). Defines JSON-prefix length, binary block order and total body framing.

[KServe Inference Protocol required APIs](https://github.com/kserve/kserve/blob/master/docs/predict-api/v2/required_api.md). Defines request tensor names, shapes and data types underneath the binary extension.

[Triton Model Configuration](https://docs.nvidia.com/deeplearning/triton-inference-server/user-guide/docs/user_guide/model_configuration.html). Defines the model-side tensor contract that correct offsets do not replace.

Related reading: [Define variable-length token input contracts for Triton ragged batching](/journal/triton-ragged-token-input-contract/); [HTTP response integrity: Content-Length, chunked transfer and truncation](/journal/provider-http-message-framing-integrity/).
