docling-project/docling · error · RuntimeError

Invalid BYTES data: insufficient bytes for length prefix at

Error message

Invalid BYTES data: insufficient bytes for length prefix at offset {offset}

What it means

decode_bytes_tensor walks a length-prefixed BYTES payload (4-byte little-endian length then that many bytes, per element). It needs shape-product many elements, but fewer than 4 bytes remained where the next length prefix should be. Either the payload is truncated or the declared shape (element count) exceeds what the payload contains.

Source

Thrown at docling/models/inference_engines/common/kserve_v2_utils.py:36

    return str(value).encode("utf-8")


def encode_bytes_tensor(tensor: np.ndarray) -> bytes:
    """Encode a BYTES tensor as a length-prefixed byte stream."""
    chunks: list[bytes] = []
    for value in tensor.reshape(-1):
        encoded = encode_bytes_element(value)
        chunks.append(len(encoded).to_bytes(4, byteorder="little"))
        chunks.append(encoded)
    return b"".join(chunks)


def decode_bytes_tensor(raw_output: bytes, shape: tuple[int, ...]) -> np.ndarray:
    """Decode a length-prefixed BYTES payload to a numpy object array."""
    strings, offset = [], 0
    for _ in range(int(np.prod(shape))):
        if offset + 4 > len(raw_output):
            raise RuntimeError(
                f"Invalid BYTES data: insufficient bytes for length prefix at offset {offset}"
            )
        str_len = int.from_bytes(raw_output[offset : offset + 4], byteorder="little")
        offset += 4
        if offset + str_len > len(raw_output):
            raise RuntimeError(
                f"Invalid BYTES data: insufficient bytes for string of length {str_len} at offset {offset}"
            )
        strings.append(raw_output[offset : offset + str_len])
        offset += str_len
    return np.array(strings, dtype=object).reshape(shape)

View on GitHub (pinned to 61d76f1ff3)

Solutions

  1. Verify the server's BYTES encoding matches the Triton convention: 4-byte little-endian length + raw bytes per element
  2. Check the tensor's declared shape against the number of elements actually serialized (log shape vs payload size)
  3. If truncation is suspected, follow error 197 diagnosis (body sizes, proxy limits)
  4. For string-free models, avoid BYTES outputs entirely (emit INT64 class ids instead)
Defensive patterns

Strategy: try-catch

Validate before calling

import numpy as np

def bytes_payload_is_plausible(raw: bytes, shape: tuple[int, ...]) -> bool:
    """Cheap check: payload must have at least 4 bytes per declared element."""
    return len(raw) >= 4 * int(np.prod(shape))

Try / catch

from docling.models.inference_engines.common.kserve_v2_utils import decode_bytes_tensor

try:
    arr = decode_bytes_tensor(raw_output, shape)
except RuntimeError as e:
    if "Invalid BYTES data" in str(e):
        raise  # payload/shape mismatch from server; fix encoding or shape
    raise

Prevention

When it happens

Trigger: A BYTES output tensor whose declared shape implies more elements than were serialized; truncated binary body upstream (also triggers error 197); a server encoding BYTES with a different element format (no length prefix, big-endian, 8-byte prefixes); empty payload with non-empty shape.

Common situations: Custom predictors not implementing the Triton BYTES length-prefix layout; shape/batch-dimension mismatches after model changes; gateway truncation of large string outputs.

Related errors


AI-assisted analysis of docling-project/docling@61d76f1ff3 (2026-08-14). Data as JSON: /api/errors/f0ae8c2c401eeb75. Report an issue: GitHub.