﻿# BentoML Shape and DType: a valid tensor can still use the wrong feature order

Use BentoML tensor validators as structural checks while separately preserving feature ordering, normalization and axis meanings in the model API contract.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-bentoml-tensor-shape-dtype-semantic-feature-order/
Language: en

## The request passes validation but predicts incorrectly

A tabular model expects temperature, pressure and humidity in its trained coordinate order. A new caller sends the same numeric types and tensor dimensions with humidity first. The request can be structurally valid while the model evaluates different features than the caller intended.

This failure is especially difficult to spot when all columns have plausible numeric ranges. A serving schema should distinguish representation checks from the feature contract that explains what each coordinate means.

## Annotated validators constrain representation

The [IO guide](https://docs.bentoml.com/en/latest/build-with-bentoml/iotypes.html) documents tensor inputs with Shape and DType annotations. These annotations make structural expectations visible at the service boundary. They do not derive a feature vocabulary from an arbitrary array or reconstruct the training transformation.

Specify permitted dimensions and numeric representation deliberately. Treat conversion behavior as something to exercise with actual requests rather than assuming every malformed caller input is rejected in the same way by every serialization path.

## Store preprocessing with the released model

The [model-management guide](https://docs.bentoml.com/en/latest/build-with-bentoml/model-loading-and-management.html) explains declared model dependencies. Keep the associated feature definition and preprocessing revision identifiable alongside the model release, so a deployment can explain which coordinate interpretation it serves.

[Explicit table schemas](/journal/data-guide-frictionless-table-schema/) provide background for documenting column semantics. If an array endpoint is necessary, publish the ordered feature mapping; otherwise a named structured input can make accidental permutation easier to detect before tensor construction.

## The response also needs a coordinate meaning

The [SDK reference](https://docs.bentoml.com/en/latest/reference/bentoml/sdk.html) describes endpoint configuration and IO specifications. Use the output contract to distinguish class scores, embeddings and other numeric arrays instead of assuming their shapes explain their interpretation.

[Attention mask axes](/journal/broadcasting-attention-mask-axes/) illustrate a related issue: dimension compatibility alone does not establish semantic correctness. An output vector with the expected length may still have a different label ordering after a model update.

## Test a deliberately permuted request

Prepare a recognizable reference sample and a version with its feature positions swapped. Confirm that the documented input construction produces the intended prediction and that unsupported representations receive the intended handling.

Repeat this check when changing preprocessing or model revisions. A successful serving contract links structural validation to the actual feature interpretation, rather than treating an accepted tensor as proof that the application supplied the right information.
