KServe Tf2OpenAPI: Generate OpenAPI Specs from TensorFlow Models

Jul 10, 2025

Introduction

Deploying TensorFlow models into production often creates a gap between the model’s internal tensor requirements and the external API requirements of a client application. Developers frequently struggle to manually define the HTTP request and response schemas for their models, leading to integration errors and poor documentation. KServe’s Tf2OpenAPI tool solves this by automatically converting TensorFlow SavedModel files into standardized OpenAPI 3.0 specifications, ensuring that the interface between the model and the client is formally defined and validated.

What Is KServe Tf2OpenAPI?

KServe Tf2OpenAPI is a specialized utility within the KServe ecosystem designed to extract the input and output signatures of a TensorFlow SavedModel and translate them into an OpenAPI 3.0 specification. It is written in Go and serves as a bridge between the rigid protobuf-based binary format of TensorFlow and the human-readable, industry-standard REST API documentation format.

The tool is part of the broader KServe project, a CNCF incubating project that provides a standardized distributed inference platform for scalable, multi-framework deployment on Kubernetes. Tf2OpenAPI specifically targets the “predictive AI” side of the platform, helping developers move from a raw model file to a fully documented HTTP endpoint.

Why KServe Tf2OpenAPI Matters

In a typical ML pipeline, the model is the “black box.” When a model is exported as a SavedModel, the information about what tensors are required for a prediction is buried in the protobuf binary. For a frontend developer or an API consumer, knowing exactly which keys and data types are required in a JSON payload is a guessing game unless the data scientist provides a manual, often outdated, document.

Tf2OpenAPI eliminates this manual overhead. By generating a formal OpenAPI spec, it allows teams to use standard API tools for input validation, client generation, and automated testing. This reduces the “time-to-integration” for new models and ensures that the API contract is always in sync with the actual model version being served.

As KServe continues to be adopted by industry leaders like IBM, NVIDIA, and Red Hat, tools like Tf2OpenAPI ensure that the platform remains framework-agnostic and accessible, allowing TensorFlow models to be served with the same professional API standards as any other microservice.

Key Features

  • Automatic Signature Extraction: The tool analyzes the TensorFlow SavedModel’s protobuf binary to identify all available signatures and their corresponding input/output tensor shapes and types.
  • OpenAPI 3.0 Compliance: It generates specifications that are fully compliant with the OpenAPI 3.0 standard, making them compatible with Swagger UI and other industry-standard documentation tools.
  • Flexible Payload Formats: It supports both “Row” and “Column” formats for request payloads. Row format is generally more intuitive for batchable inputs, while Column format is used for specific non-batchable configurations.
  • Input Validation: By providing a formal spec, it enables the KServe inference gateway to validate incoming HTTP requests against the model’s actual requirements before the request even reaches the model server.
  • Benchmarking Support: The generated specifications can be used to automatically generate sample payloads for performance benchmarking and load testing.
  • Integration with KServe Ecosystem: It is designed to work seamlessly with the KServe InferenceService, allowing the generated spec to be used for routing and validation at the edge.

How KServe Tf2OpenAPI Compares

When compared to other methods of documenting TensorFlow models, Tf2OpenAPI provides a unique balance of automation and standardization. Most developers either manually write a Swagger file or rely on the internal logs of TensorFlow Serving (TFS) to understand the model’s API.

Feature KServe Tf2OpenAPI Manual Swagger/OpenAPI TFS Internal Logs
Automation Level High (Automatic) None (Manual) Low (Observational)
Standardization OpenAPI 3.0 OpenAPI 3.0 Proprietary/Internal
Accuracy High (Direct from Model) Medium (Prone to Human Error) High (Actual Runtime)
Client Generation Supported Supported Not Supported

The primary differentiator for Tf2OpenAPI is its ability to treat the model as the “source of truth.” Instead of writing documentation that might drift from the actual model implementation, Tf2OpenAPI extracts the requirements directly from the SavedModel binary. This is critical for teams managing dozens of model versions across different environments.

While manual OpenAPI files are a great way to define a custom wrapper around a model, Tf2OpenAPI is the correct choice for those who want to expose the raw model’s capabilities without the overhead of writing a custom API layer.

Getting Started: Installation

Tf2OpenAPI is distributed as part of the KServe repository. To use it, you must build it from the source code using the Go toolchain.

Prerequisites

Ensure you have the following installed on your system:

  • Go (version 1.25 or later, as per recent updates)
  • Protobuf compiler (protoc)
  • Git

Build from Source

Follow these steps to compile the tool:

git clone https://github.com/kserve/kserve.git
cd kserve/tools/tf2openapi
go build -o tf2openapi main.go

After building, you will have a binary named tf2openapi that you can move to your system path for global access.

How to Use KServe Tf2OpenAPI

Using the tool is straightforward: you provide the path to your TensorFlow SavedModel and specify where you want the resulting YAML file to be saved. The tool will then parse the model’s signatures and generate the specification.

The basic workflow involves running the command in your terminal. If your model is stored locally, ensure the path is absolute. If you are using KServe, you can integrate this step into your CI/CD pipeline to automatically generate the spec for every new model version.

tf2openapi -m /absolute/path/to/saved_model -o /absolute/path/to/openapi_spec.yaml

Once the YAML file is generated, you can import it into Swagger UI or use it within a KServe InferenceService manifest to enable request validation at the gateway.

Code Examples

The following examples demonstrate how to use the tool with different flags to customize the output.

Example 1: Basic Specification Generation

This is the most common use case. It takes a local SavedModel and generates a standard OpenAPI spec.

tf2openapi -m ./my_model/1/ -o ./model_api.yaml

Example 2: Customizing Model Name and Version

You can override the default model name and version in the generated specification to match your deployment environment.

tf2openapi -m ./my_model/1/ -o ./model_api.yaml -n "CustomerChurnModel" -v "v1.2.0"

Example 3: Specifying a Particular Signature

If your SavedModel contains multiple signatures (e.g., one for prediction and one for training), you can specify which one to use for the API definition.

tf2openapi -m ./my_model/1/ -o ./model_api.yaml -s "serving_default"

Real-World Use Cases

Tf2OpenAPI is particularly useful in enterprise environments where model governance and API stability are critical.

  • Automated API Documentation: A data science team can integrate Tf2OpenAPI into their model export pipeline. Every time a model is saved, the OpenAPI spec is automatically generated and uploaded to a company’s internal API portal, ensuring documentation is never out of date.
  • Strict Input Validation: In a high-traffic production environment, sending malformed JSON payloads to a TensorFlow Serving instance can cause unnecessary resource consumption or internal server errors. By using the generated spec with a KServe gateway, requests are rejected at the edge, protecting the model server from invalid inputs.
  • Client SDK Generation: Frontend developers can use the generated OpenAPI spec to automatically create typed client libraries in TypeScript or Python, ensuring that the data sent to the model is perfectly aligned with the tensor shapes required by the TensorFlow model.
  • Performance Benchmarking: QA engineers can use the spec to generate a suite of valid sample payloads, which are then used in load testing tools to simulate realistic traffic patterns without manually crafting JSON tensors.

Contributing to KServe Tf2OpenAPI

Tf2OpenAPI is part of the KServe project. Contributions are welcome and encouraged to improve the tool’s parsing logic or add support for more complex TensorFlow tensor types.

To contribute, start by reading the CONTRIBUTING.md file in the root of the KServe repository. The project follows the standard GitHub flow: fork the repository, create a feature branch, and submit a Pull Request. If you are new to the project, look for issues labeled “good first issue” to find an approachable entry point.

The project also adheres to a formal Code of Conduct to ensure a community-driven, inclusive environment for all contributors.

Community and Support

Since Tf2OpenAPI is a tool within the KServe ecosystem, support is provided through the same channels as the rest of the platform. You can find extensive documentation on the official KServe website.

  • GitHub Discussions: The primary place for architectural questions and community feedback.
  • KServe Slack: For real-time support and troubleshooting with other developers and MLOps engineers.
  • Official Documentation: The KServe website provides detailed guides on predictive AI model serving and the V2 inference protocol.

Conclusion

KServe Tf2OpenAPI is an essential tool for anyone moving TensorFlow models from a research environment to a production-grade API. By automating the extraction of model signatures and translating them into a standardized OpenAPI 3.0 specification, it removes the friction between data science and software engineering.

If you are currently serving TensorFlow models using KServe or TensorFlow Serving, Tf2OpenAPI is the right choice for providing professional, validated, and documented APIs. However, if you are building a highly customized API wrapper around your model, a manual OpenAPI spec may be more appropriate.

Star the repo, try the quickstart, and join the KServe community to help shape the future of standardized AI inference.

What is KServe Tf2OpenAPI and what problem does it solve?

KServe Tf2OpenAPI is a tool that converts TensorFlow SavedModel files into OpenAPI 3.0 specifications. It solves the problem of manually documenting the required input and output tensors for a model, ensuring that the API contract is automatically derived from the model itself.

How do I install KServe Tf2OpenAPI?

Tf2OpenAPI must be built from the KServe source code. Clone the repository, navigate to the tools/tf2openapi directory, and use the Go toolchain to run go build -o tf2openapi main.go.

Does Tf2OpenAPI support all TensorFlow versions?

The tool is designed to work with TensorFlow SavedModels, which is the standard export format for TensorFlow 2.x. It is primarily compatible with TensorFlow 2.x models exported as SavedModels.

How does Tf2OpenAPI compare to manual OpenAPI specs?

Unlike manual specs, Tf2OpenAPI generates the documentation directly from the model’s protobuf binary, meaning the documentation is always in sync with the model’s actual tensor requirements, eliminating human error.

Can I use Tf2OpenAPI for PyTorch models?

No, Tf2OpenAPI is specifically designed for TensorFlow SavedModels. For other frameworks, you would need to use the rest of the KServe platform’s standardized V2 inference protocol for API definition.

Can I use the generated spec for input validation at the gateway?

Yes, the generated OpenAPI spec can be used by the KServe inference gateway to validate that incoming HTTP requests are correctly formatted before they are passed to the model server.

What is the difference between Row and Column format in Tf2OpenAPI?

Row format organizes the data in a way that is more intuitive for individual predictions or batches of predictions, while Column format is used for specific non-batchable configurations or rigid tensor structures.