Registry / ai-ml / tensorflow-serving-api

tensorflow-serving-api

JSON →
library2.20.0pypypi✓ verified 26d ago

The `tensorflow-serving-api` library provides the Python client API for interacting with TensorFlow Serving, a flexible, high-performance serving system for machine learning models. Designed for production environments, TensorFlow Serving facilitates model deployment, versioning, and management, exposing both gRPC and HTTP/REST inference endpoints. The Python API primarily focuses on client-side gRPC communication. The current version is 2.19.1, and its releases typically align with the main TensorFlow project's release cadence.

pip install tensorflow-serving-api
INSTALL
IMPORT
SIG · TENSORFLOW-SERVING
T
tensorflow-serving-api
ai-mlpythonv2.20.0
Install
33.3s avg
Import
294ms
Disk
2150MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.20.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
build_error
glibc
py 3.103.95 runs
installs and imports cleanly · install 33.3s · import 0.294s · 2150.4MB
2150MB installed
● package 2150MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

grpc
import grpc
prediction_service_pb2_grpc
from tensorflow_serving.apis import prediction_service_pb2_grpc
predict_pb2
from tensorflow_serving.apis import predict_pb2
get_model_status_pb2
from tensorflow_serving.apis import get_model_status_pb2
model_service_pb2_grpc
from tensorflow_serving.apis import model_service_pb2_grpc
TensorProto
from tensorflow.core.framework import tensor_pb2
from tensorflow_serving.apis.tensor_pb2 import TensorProto
TensorProto is part of the core TensorFlow framework, not directly within tensorflow_serving.apis.

This quickstart demonstrates how to construct and send a gRPC prediction request to a running TensorFlow Serving instance using the `tensorflow-serving-api` library. It covers setting up the gRPC channel and stub, creating a `PredictRequest`, populating it with input data converted to `TensorProto` format, and handling the response. Ensure that a TensorFlow Serving server is running and accessible at the specified address and port, with your model loaded, before attempting to run this client code. Replace `your_model`, `input_tensor_name`, and `output_tensor_name` with your actual model's details.

import grpc import numpy as np from tensorflow_serving.apis import prediction_service_pb2_grpc, predict_pb2 from tensorflow.core.framework import tensor_pb2 from tensorflow.python.framework import dtypes # for tf.float32, etc. (usually for older TF, or explicit type) # NOTE: This quickstart assumes a TensorFlow Serving server is already running. # For example, via Docker: # docker run -p 8500:8500 --name tfserving_test \ # --mount type=bind,source=/path/to/your/model,target=/models/your_model \ # -e MODEL_NAME=your_model -t tensorflow/serving & # Configuration for your model server SERVER_ADDRESS = 'localhost:8500' MODEL_NAME = 'your_model' SIGNATURE_NAME = 'serving_default' def make_prediction(input_data: np.ndarray): """Sends a prediction request to TensorFlow Serving via gRPC.""" channel = grpc.insecure_channel(SERVER_ADDRESS) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) request = predict_pb2.PredictRequest() request.model_spec.name = MODEL_NAME request.model_spec.signature_name = SIGNATURE_NAME # Convert NumPy array to TensorProto tensor_proto = tensor_pb2.TensorProto() tensor_proto.CopyFrom(np.asarray(input_data).astype(np.float32)._to_proto()) # TensorFlow 2.x often requires explicit type, e.g., using a dtypes enum # tensor_proto.dtype = dtypes.float32.as_datatype_enum # Example for explicit type request.inputs['input_tensor_name'].CopyFrom(tensor_proto) # Replace 'input_tensor_name' with your model's input name try: response = stub.Predict(request, 10.0) # 10-second timeout print("Prediction successful:") # Process response.outputs to get results # Example: print(response.outputs['output_tensor_name']) for key, val in response.outputs.items(): print(f" Output '{key}': {np.array(val.float_val) if val.float_val else val}") except grpc.RpcError as e: print(f"Error making prediction: {e.code()} - {e.details()}") if __name__ == '__main__': # Example usage: a simple 1x5 float array as input sample_input = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]]) make_prediction(sample_input)
Debug
Known issues
breakingVersion compatibility between `tensorflow-serving-api` and the `tensorflow_model_server` binary is crucial. Mismatched versions can lead to protobuf deserialization errors or other unexpected client/server communication failures.
fix
Always align the `tensorflow-serving-api` Python package version with the version of the `tensorflow_model_server` (e.g., from Docker image `tensorflow/serving:2.19.1`). Consult the official TensorFlow Serving GitHub releases for version information.
affects: All versions
gotchaThis library (`tensorflow-serving-api`) provides only the *client* API. It does not include the `tensorflow_model_server` itself, which is the actual server component that loads and serves your models. The server must be installed and run separately (typically via Docker or `apt-get`).
fix
Ensure `tensorflow_model_server` is running and accessible before attempting to connect with this API. For example, using Docker: `docker run -p 8500:8500 --name tfserving_test -v "$(pwd)/my_model_dir:/models/my_model" -e MODEL_NAME=my_model -t tensorflow/serving`.
affects: All versions
gotchaModels must be exported in the TensorFlow `SavedModel` format and stored in a versioned directory structure (e.g., `model_name/1/`, `model_name/2/`) for the `tensorflow_model_server` to load them correctly. Incorrect directory structure is a common source of 'Model not found' or 'No versions of servable found' errors.
fix
When saving your model, place it in a subdirectory named with an integer version number (e.g., `tf.saved_model.save(model, '/path/to/models/my_model/1')`). The server automatically picks up the highest version.
affects: All versions
gotchaWhen sending gRPC requests, input data (e.g., NumPy arrays) must be correctly converted into Protobuf `TensorProto` format. Incorrect type mapping or shape can lead to prediction errors on the server side.
fix
Use `tensorflow.python.framework.tensor_util.make_tensor_proto` or `np.ndarray._to_proto()` with appropriate data types (e.g., `np.float32`) to ensure compatibility with your model's input signature.
affects: All versions
gotchaThe documentation specifically for the `tensorflow-serving-api` Python client can be sparse. Many guides focus on the server setup or REST API, requiring users of the gRPC Python client to infer usage from examples or C++ API definitions.
fix
Refer to official TensorFlow Serving examples on GitHub (e.g., `tensorflow/serving/tensorflow_serving/example`) and community blogs for comprehensive usage patterns, particularly for advanced scenarios or specific data types.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'tensorflow_serving'
The `tensorflow-serving-api` Python package, which provides the `tensorflow_serving` module, is either not installed or not accessible in the current Python environment.
fix
Install the package using pip: `pip install tensorflow-serving-api`.
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.UNAVAILABLE details = "failed to connect to all addresses"
The gRPC client failed to establish a connection with the TensorFlow Serving server, likely because the server is not running, is listening on a different host/port, or network connectivity is blocked.
fix
Ensure the TensorFlow Serving server is running and accessible on the specified host and port (e.g., `localhost:8500`). Check server logs, firewall rules, and host/port configuration.
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.INVALID_ARGUMENT details = "Expected argument[0] to be a float type, got string instead."
The input tensor(s) sent in the `PredictRequest` do not match the TensorFlow model's expected input signature regarding data type, shape, or name.
fix
Inspect the model's exact input signature using `saved_model_cli show --dir /path/to/model/version --tag_set serve --signature_def YOUR_SIGNATURE_NAME` and ensure your `PredictRequest` constructs `TensorProto` objects with matching `dtype`, `tensor_shape`, and `key` (input name).
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.NOT_FOUND details = "SignatureDef 'predict' not found: ..."
The `signature_name` specified in the `ModelSpec` of your `PredictRequest` does not correspond to an existing SignatureDef in the TensorFlow model being served.
fix
Verify the available signature definitions of your served model using `saved_model_cli show --dir /path/to/model/version --tag_set serve`. Use the correct `signature_name` (e.g., `serving_default` if no custom signature was explicitly defined) in your client code.
Upgrade
Version history
2.20.0latest on PyPI · released May 30, 2026
Audit
Dependencies
grpciorequiredRequired for gRPC communication with the TensorFlow Serving server.
protobufrequiredRequired for serializing and deserializing data for gRPC requests.
tensorflowoptionalNecessary for creating and exporting models in the SavedModel format, which TensorFlow Serving consumes. Not a direct dependency of `tensorflow-serving-api`, but essential for the ecosystem.
Agent activity
17 hits · last 30 days
node
12
OpenAI (training)
1
Resources
tensorflow-serving-api — pip install tensorflow-serving-api · libregistry