Implementation:Tensorflow Serving Build Signature Def
| Knowledge Sources | |
|---|---|
| Domains | Model_Serialization, API_Design |
| Last Updated | 2026-02-13 17:00 GMT |
Overview
Wrapper for TensorFlow's build_tensor_info() and build_signature_def() utilities to construct SignatureDef protobufs for SavedModel export.
Description
These TensorFlow utility functions construct the protobuf messages that define a model's serving interface. build_tensor_info() converts a TensorFlow tensor into a TensorInfo proto (capturing name, dtype, and shape), while build_signature_def() assembles multiple TensorInfos into a complete SignatureDef with named inputs, outputs, and a method name.
In the TensorFlow Serving MNIST example, two signatures are constructed:
- A classification signature (default) using serialized TF examples as input
- A prediction signature (named "predict_images") using raw image tensors
Usage
Use these functions after training a model and before calling SavedModelBuilder. They bridge the gap between TensorFlow tensors in a session and the serialized serving interface.
Code Reference
Source Location
- Repository: tensorflow/serving
- File: tensorflow_serving/example/mnist_saved_model.py
- Lines: L107-139
Signature
# build_tensor_info: Convert tensor to TensorInfo proto
tf.compat.v1.saved_model.utils.build_tensor_info(
tensor # tf.Tensor - the tensor to describe
) -> TensorInfo
# build_signature_def: Assemble inputs/outputs into SignatureDef
tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
inputs: dict, # map of alias names to TensorInfo
outputs: dict, # map of alias names to TensorInfo
method_name: str # CLASSIFY_METHOD_NAME, PREDICT_METHOD_NAME, or REGRESS_METHOD_NAME
) -> SignatureDef
Import
import tensorflow as tf
# Access via:
# tf.compat.v1.saved_model.utils.build_tensor_info
# tf.compat.v1.saved_model.signature_def_utils.build_signature_def
# tf.compat.v1.saved_model.signature_constants.CLASSIFY_METHOD_NAME
# tf.compat.v1.saved_model.signature_constants.PREDICT_METHOD_NAME
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| tensor | tf.Tensor | Yes | For build_tensor_info: the tensor to describe |
| inputs | dict[str, TensorInfo] | Yes | For build_signature_def: named input tensor info map |
| outputs | dict[str, TensorInfo] | Yes | For build_signature_def: named output tensor info map |
| method_name | str | Yes | For build_signature_def: inference method constant |
Outputs
| Name | Type | Description |
|---|---|---|
| TensorInfo | protobuf | Proto describing tensor name, dtype, and shape |
| SignatureDef | protobuf | Complete serving signature with inputs, outputs, method |
Usage Examples
Building Classification and Prediction Signatures
import tensorflow as tf
# Assume trained tensors exist: serialized_tf_example, prediction_classes, values, x, y
# 1. Build TensorInfo for each tensor
classification_inputs = tf.compat.v1.saved_model.utils.build_tensor_info(serialized_tf_example)
classification_outputs_classes = tf.compat.v1.saved_model.utils.build_tensor_info(prediction_classes)
classification_outputs_scores = tf.compat.v1.saved_model.utils.build_tensor_info(values)
# 2. Build classification signature (default serving signature)
classification_signature = tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
inputs={
tf.compat.v1.saved_model.signature_constants.CLASSIFY_INPUTS: classification_inputs
},
outputs={
tf.compat.v1.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES: classification_outputs_classes,
tf.compat.v1.saved_model.signature_constants.CLASSIFY_OUTPUT_SCORES: classification_outputs_scores
},
method_name=tf.compat.v1.saved_model.signature_constants.CLASSIFY_METHOD_NAME
)
# 3. Build prediction signature
tensor_info_x = tf.compat.v1.saved_model.utils.build_tensor_info(x)
tensor_info_y = tf.compat.v1.saved_model.utils.build_tensor_info(y)
prediction_signature = tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
inputs={'images': tensor_info_x},
outputs={'scores': tensor_info_y},
method_name=tf.compat.v1.saved_model.signature_constants.PREDICT_METHOD_NAME
)