How to deploy and serve prediction using TensorFlow from API?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Serving TensorFlow predictions through an API requires more than loading a model and exposing one route. Reliable deployment also needs stable preprocessing, versioned artifacts, health checks, and predictable response formats. This guide shows a practical path using SavedModel plus a FastAPI service.
Export a Stable TensorFlow Model
Train your model and export it in SavedModel format so serving code can load it consistently.
Version directories like model/1, model/2, and so on make rollbacks and staged rollout easier.
Build a Prediction API with FastAPI
Load the model once at startup, validate input schema, and return structured outputs.
Run locally:
Containerize for Repeatable Deployment
A container image ensures your model server runs with consistent dependencies.
Example requirements.txt:
Build and run:
Operational Hardening for Production
Production serving needs guardrails beyond correctness.
- enforce strict request schemas and size limits
- add request logging with latency and model version
- export metrics for success rate and p95 latency
- include readiness checks that verify model is loaded
- keep model preprocessing identical to training
Consider loading model metadata from a config file so deployments can switch versions without code edits.
Return MODEL_VERSION in prediction responses for traceability.
Alternative: TensorFlow Serving Endpoint
If you need high throughput and model lifecycle tooling, TensorFlow Serving is a strong option. Your API gateway can call its REST endpoint and keep business logic in a separate service layer.
Then call:
Common Pitfalls
A common mistake is running training preprocessing and serving preprocessing differently. Even small mismatch can degrade prediction quality dramatically.
Another issue is reloading model for each request. Always load once on startup to avoid high latency and memory churn.
A third issue is no timeout and retry policy in upstream clients. Model APIs can spike during deployments and need client side resilience.
Finally, avoid unversioned model artifacts in production. Versioning is essential for rollback, auditability, and incident response.
Automate deployment checks so schema, model path, and health probes stay synchronized.
Summary
- Export TensorFlow models as versioned
SavedModelartifacts - Serve predictions through validated API contracts with clear response schemas
- Containerize for reproducible deployment across environments
- Add health checks, metrics, and model version traceability
- Keep preprocessing consistent and versioned to maintain prediction quality
Related reading
- How To Determine the 'filter' Parameter in the Keras Conv2D Function
- How to disable dropout while prediction in keras?
- How to disable GPU in keras with tensorflow?
- How to disable keras warnings?
- How to deploy machine learning algorithm in production environment?
- How to determine an object's class?
- How to deploy changes to a Cassandra CQL schema
- How to deploy in kubernetes without any changes, just to get pods to cycle

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.