TensorFlow
REST API
Machine Learning Deployment
Frontend Development
AI Integration

TensorFlow REST Frontend but not TensorFlow Serving

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

You do not need TensorFlow Serving to expose a TensorFlow model behind a REST API. A normal Python web framework can load the model and provide a /predict endpoint directly. This is often the simplest path for prototypes, lightweight internal services, or deployments that need custom request validation and business logic around inference. The tradeoff is that you now own the serving process rather than delegating it to a specialized model server.

A Simple FastAPI Example

Load the model once at startup and reuse it for requests:

python
1from fastapi import FastAPI, HTTPException
2from pydantic import BaseModel
3import numpy as np
4import tensorflow as tf
5
6app = FastAPI()
7model = tf.keras.models.load_model("model.keras")
8
9
10class PredictRequest(BaseModel):
11    features: list[float]
12
13
14@app.post("/predict")
15def predict(request: PredictRequest):
16    try:
17        x = np.array([request.features], dtype="float32")
18        prediction = model.predict(x, verbose=0)
19        return {"prediction": prediction.tolist()}
20    except Exception as exc:
21        raise HTTPException(status_code=400, detail=str(exc))

This is a real REST frontend around a TensorFlow model, with no TensorFlow Serving involved.

Why This Works

At a high level, the service is doing three things:

  1. validating incoming JSON
  2. converting it to the tensor shape the model expects
  3. returning the prediction as JSON

TensorFlow Serving automates a lot of this in a production-oriented way, but it is not the only possible deployment model.

If you only need one model and straightforward request handling, a normal web service can be perfectly reasonable.

Make Input Shape Explicit

The most common bug in custom model APIs is not the REST layer. It is input shape mismatch.

For example, if the model expects shape (batch, 4):

python
x = np.array([request.features], dtype="float32")

wraps one feature vector into a batch of size 1.

Without that outer list, the model may receive the wrong rank and fail.

That is why the API contract should describe:

  • number of features
  • ordering of features
  • dtype expectations
  • batch versus single-example behavior

REST deployment only feels simple when the data contract is explicit.

Add Business Logic Around the Model

One big reason to choose a custom REST frontend is that it can do more than raw inference:

  • authentication and authorization
  • feature validation
  • request enrichment
  • post-processing of predictions
  • logging and audit hooks

For example, you might normalize features, enforce a tenant check, or combine the model output with rule-based thresholds before returning the final result.

That kind of application logic is often easier to express in a normal web service than in a specialized model-serving product.

Concurrency and Process Model Matter

When you own the REST frontend, you also own concurrency decisions.

A few practical rules help:

  • load the model once per worker process
  • avoid reloading the model on every request
  • benchmark your actual worker count
  • consider batching if request volume is high

A naive implementation that calls load_model() inside the endpoint will look correct in tests and perform badly in production.

That is a classic custom-serving mistake.

When TensorFlow Serving Is Still Better

A custom REST frontend is convenient, but TensorFlow Serving still has advantages when you need:

  • high-throughput model serving
  • standardized model versioning
  • optimized batching
  • a dedicated serving surface separate from app logic

So the real decision is not "is TensorFlow Serving required." It is "which tradeoff fits this deployment."

For internal tools or simple APIs, a custom REST wrapper may be the fastest path.

For heavy production inference infrastructure, a specialized serving layer may still be the better choice.

A Useful Middle Ground

Some teams keep a lightweight REST app in front of a model runtime. Even without TensorFlow Serving, you can still structure the code cleanly:

  • one module for loading the model
  • one module for request validation
  • one module for prediction logic
  • one module for HTTP routing

That makes later migration easier if you eventually move to a dedicated serving system.

Common Pitfalls

The biggest mistake is loading the model inside the request handler. That turns every request into model startup overhead.

Another issue is skipping input validation and assuming clients always send the correct feature vector length and type.

Developers also often forget that a custom REST app now owns operational concerns such as concurrency, health checks, metrics, and deployment behavior.

Finally, do not reject TensorFlow Serving automatically or embrace it automatically. The right choice depends on whether you need a general web service with model logic or a specialized high-throughput serving layer.

Summary

  • A normal REST API can serve TensorFlow predictions without TensorFlow Serving.
  • Load the model once at startup and reuse it across requests.
  • Make the request schema and input tensor shape explicit.
  • A custom REST frontend is often best when you need validation, auth, or business logic around inference.
  • TensorFlow Serving is still valuable when standardized, high-throughput model serving is the primary goal.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.