Sub topic 11
Adding Pre- and Post-Processing with a Transformer
This chapter assumes you understand Kubernetes pods, services, and namespaces, can apply YAML with kubectl, and know how a model endpoint accepts JSON. It also assumes you have a working KServe InferenceService.
A model server should usually receive data in the shape it expects. Client applications, however, often send business-friendly data. For example, an application might send:
{"age": 42, "income": 72000, "country": "US"}The model might require a numeric feature vector such as:
{"instances": [[42, 72000, 1]]}A KServe transformer handles this boundary. A transformer is a component that changes a request before it reaches the model and changes the model response before it returns to the client.
The request path
A request follows this path:
Preprocessing can perform tasks such as:
- Validating required fields.
- Converting strings to numbers.
- Applying the same normalization used during training.
- Encoding categories.
- Building the V1 or V2 inference payload.
Postprocessing can rename prediction fields, add class labels, attach confidence values, or convert model-specific output into a stable application response.
The transformer should repeat only transformations defined by the model's training contract. If training normalized a feature with a particular mean and standard deviation, production preprocessing must use those same values.
Connecting a transformer
A transformer is commonly packaged as a container and declared alongside the predictor:
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: income-model
spec:
transformer:
containers:
- name: transformer
image: registry.example.com/income-transformer:1.0
predictor:
model:
modelFormat:
name: sklearn
storageUri: gs://example-bucket/models/income/v1The image name is an example. Your cluster must be able to pull the image, and the image must run a server that implements the protocol expected by KServe. The transformer receives the external request, calls the predictor, and returns the transformed response.
Keep preprocessing and postprocessing versioned with the model contract. A new model may require different feature ordering or encoding. Updating the transformer without coordinating the model can produce valid-looking but incorrect predictions.
Choosing where logic belongs
Use a transformer when the logic is part of serving the model and should be shared by every client. Keep user-interface formatting, authentication, and business workflow logic in the calling application.
| Logic | Best location |
|---|---|
| Feature encoding required by the model | Transformer |
| Model output to application response | Transformer |
| User permissions and authentication | Gateway or application |
| Screen-specific formatting | Application |
A transformer adds a network hop and another container to monitor. It can also become a bottleneck. Keep it stateless where possible so KServe can scale it with the predictor. If preprocessing is expensive, measure its latency separately from model inference.
Testing the boundary
Test the transformer with representative valid requests, missing fields, invalid types, empty inputs, and unusually large batches. Check both the HTTP response and the model's input logs or metrics. A successful HTTP status does not prove that the feature values are correct.
Exercise
A client sends country names and raw income values, but the model requires encoded and normalized features. Where should this conversion happen, and why?
Show answer
It should happen in the transformer because the conversion is part of the model's serving contract and must be applied consistently for every client.