Principle:Tensorflow Serving JSON Response Serialization
| Knowledge Sources | |
|---|---|
| Domains | Data_Serialization, API_Design |
| Last Updated | 2026-02-13 17:00 GMT |
Overview
A data conversion process that transforms TensorFlow output tensors into JSON format for REST API responses, preserving type fidelity and matching the request format.
Description
After TensorFlow session execution, output tensors are in TensorProto format (binary protobuf). JSON response serialization converts these back to human-readable JSON. The serialization format mirrors the request format:
- Row format request →
{"predictions": [...]}response - Columnar format request →
{"outputs": {...}}response
The serializer handles all TensorFlow data types with special cases:
- Floats/Doubles: Serialized as JSON numbers with sufficient precision
- Integers: Serialized as JSON numbers
- Strings: Serialized as JSON strings; binary strings use
{"b64": "..."}encoding - Booleans: Serialized as JSON true/false
Error responses follow the format {"error": "<message>"}.
Usage
This serialization is automatically applied to all REST inference responses. The format is determined by the corresponding request format and cannot be overridden independently.
Theoretical Basis
# Abstract response serialization (NOT real implementation)
def serialize_response(tensor_map, request_format):
if request_format == ROW:
# {"predictions": [[0.1, 0.9], [0.8, 0.2]]}
return {"predictions": interleave_tensors(tensor_map)}
elif request_format == COLUMNAR:
# {"outputs": {"scores": [[0.1, 0.9], [0.8, 0.2]]}}
return {"outputs": {name: tensor_to_json(t) for name, t in tensor_map}}