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-apiVerified import paths — ran on the pinned version, not inferred.
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.
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.
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`.
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.
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.
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.
Install the package using pip: `pip install tensorflow-serving-api`.
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.
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).
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.