{"record":{"id":"f0ae8c2c401eeb75","repo":"docling-project/docling","slug":"invalid-bytes-data-insufficient-bytes-for-length","errorCode":null,"errorMessage":"Invalid BYTES data: insufficient bytes for length prefix at offset {offset}","messagePattern":"Invalid BYTES data: insufficient bytes for length prefix at offset (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"docling/models/inference_engines/common/kserve_v2_utils.py","lineNumber":36,"sourceCode":"    return str(value).encode(\"utf-8\")\n\n\ndef encode_bytes_tensor(tensor: np.ndarray) -> bytes:\n    \"\"\"Encode a BYTES tensor as a length-prefixed byte stream.\"\"\"\n    chunks: list[bytes] = []\n    for value in tensor.reshape(-1):\n        encoded = encode_bytes_element(value)\n        chunks.append(len(encoded).to_bytes(4, byteorder=\"little\"))\n        chunks.append(encoded)\n    return b\"\".join(chunks)\n\n\ndef decode_bytes_tensor(raw_output: bytes, shape: tuple[int, ...]) -> np.ndarray:\n    \"\"\"Decode a length-prefixed BYTES payload to a numpy object array.\"\"\"\n    strings, offset = [], 0\n    for _ in range(int(np.prod(shape))):\n        if offset + 4 > len(raw_output):\n            raise RuntimeError(\n                f\"Invalid BYTES data: insufficient bytes for length prefix at offset {offset}\"\n            )\n        str_len = int.from_bytes(raw_output[offset : offset + 4], byteorder=\"little\")\n        offset += 4\n        if offset + str_len > len(raw_output):\n            raise RuntimeError(\n                f\"Invalid BYTES data: insufficient bytes for string of length {str_len} at offset {offset}\"\n            )\n        strings.append(raw_output[offset : offset + str_len])\n        offset += str_len\n    return np.array(strings, dtype=object).reshape(shape)\n","sourceCodeStart":18,"sourceCodeEnd":48,"githubUrl":"https://github.com/docling-project/docling/blob/61d76f1ff3f8428065465889f7b4577da7df704c/docling/models/inference_engines/common/kserve_v2_utils.py#L18-L48","documentation":"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.","triggerScenarios":"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.","commonSituations":"Custom predictors not implementing the Triton BYTES length-prefix layout; shape/batch-dimension mismatches after model changes; gateway truncation of large string outputs.","solutions":["Verify the server's BYTES encoding matches the Triton convention: 4-byte little-endian length + raw bytes per element","Check the tensor's declared shape against the number of elements actually serialized (log shape vs payload size)","If truncation is suspected, follow error 197 diagnosis (body sizes, proxy limits)","For string-free models, avoid BYTES outputs entirely (emit INT64 class ids instead)"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"import numpy as np\n\ndef bytes_payload_is_plausible(raw: bytes, shape: tuple[int, ...]) -> bool:\n    \"\"\"Cheap check: payload must have at least 4 bytes per declared element.\"\"\"\n    return len(raw) >= 4 * int(np.prod(shape))","typeGuard":null,"tryCatchPattern":"from docling.models.inference_engines.common.kserve_v2_utils import decode_bytes_tensor\n\ntry:\n    arr = decode_bytes_tensor(raw_output, shape)\nexcept RuntimeError as e:\n    if \"Invalid BYTES data\" in str(e):\n        raise  # payload/shape mismatch from server; fix encoding or shape\n    raise","preventionTips":["Ensure servers use the Triton BYTES layout: 4-byte little-endian length + bytes per element","Keep declared shapes consistent with serialized element counts in predictor tests","Prefer numeric class-id outputs over BYTES strings when possible"],"tags":["kserve","bytes","binary-data","codec","server-bug"],"backgroundTag":null,"analyzedSha":"61d76f1ff3f8428065465889f7b4577da7df704c","analyzedAt":"2026-08-14T23:53:18.727Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}