REST API Servers
A model file becomes a request/response service
The REST API server turns a model file into a request/response service: load the artifact, expose predict endpoints, batch requests, and return the contract alongside the prediction. Small and standard, it's how most models first meet their users.
What you will learn
- Serve a model behind a REST predict endpoint
- Define a response contract with metadata
- Enable request batching and error semantics
What is this?
A REST API server loads a serialized model into memory and exposes HTTP endpoints — typically /predict — that parse requests, run inference, batch when able, and return the prediction with metadata. It's the default on-ramp for 'make the model callable'.
Why do you need it?
- REST is universal: any client library, language or service can call it.
- A wall between model and consumer makes validation, versioning and A/B possible.
- Batching at the server turns many small requests into fewer model calls.
- Server-side load means the model isn't re-spun per request from scratch.
Mental model
A concierge at a hotel desk
REST prediction server
Guests (clients) don't talk to housekeeping directly; the concierge (API) takes requests, checks understanding (validation), groups same-floor errands (batching), and returns confirmations (metadata). One desk, many guests.
How it works
- The server loads the artifact at boot and keeps it resident (one model load for all requests).
- Requests hit /predict; the server validates input shape and types.
- A worker pool runs inference; concurrent requests can be batched.
- The response carries the prediction, model version and timing metadata.
- Health endpoints (/ready, /live) keep orchestration honest.
A REST serving round trip.
Examples
A minimal FastAPI predict endpoint
from fastapi import FastAPIfrom pydantic import BaseModelimport joblib app = FastAPI()model = joblib.load("model.joblib") class Payload(BaseModel): features: list[float] @app.post("/predict")def predict(p: Payload): prob = model.predict_proba([p.features])[0][1] return {"prob": prob, "model_version": model.meta["version"]}The model loads once into global state; each request is validated by Pydantic, scored, and answered with the prediction plus the served version.
In production
- A scoring service answers ~50ms predictions behind a load balancer, one model load across thousands of calls.
- A batch-aware server groups bursts of fraud checks into single model calls.
- Versioned servers let a canary route to /predict-v2 during rollout.
Best practices
- Return the model version and input checksum in every response.
- Validate input server-side; don't trust clients or internal callers.
- Expose /live and /ready for orchestrators to health-check.
- Cap request sizes and timeouts; a prediction endpoint is a service, not a notebook cell.
Common mistakes
- Reloading the model on every request in the handler.
- Returning bare floats with no version or metadata — undebuggable in production.
- No timeouts, so a slow batch-of-requests occupies workers forever.
- Trusting raw JSON and crashing on unexpected shapes instead of validating.
Debugging tips
- 'Works in notebook, 500 in API' — the request shape differs; log validation failures verbatim.
- Latency climbs with concurrency — check worker pool size and whether requests serialize on a lock.
- Model version confusion — read the response's version field, not the manifest on disk.
Interview checkpoint
Hands-on lab
- Load a model once and serve /predict
- Return versioned responses with timing
- Exercise readiness and failure paths
Serve a Model Behind REST
35 min · intermediate
Quick quiz
What should every predict response include beyond the prediction?
A prediction server should validate request payloads server-side.