ML8: Serving models

Lesson 2 of 5 · reading 15–25 minutes; practice and project time is on the module page

An API with validation, using FastAPI

🎯You'll be able to

Build a prediction endpoint with FastAPI and pydantic that rejects missing, unknown and impossible inputs before the model sees them.

🏠The everyday picture

A bank counter checks the form before processing it: is every field filled, is the date real, is the amount positive? It sends back a form with a clear note rather than processing nonsense.

📘What it really is

📝A worked example

Input: the reference solution's Shipment model in dakdost/serve.py, and two requests.

uvicorn dakdost.serve:app --port 8000
curl -s localhost:8000/predict -H 'content-type: application/json' -d '{"booked_at": "2026-06-12T18:40+05:30", "origin_hub": "JAI",
  "dest_zone": "remote", "distance_km": 640, "weight_kg": 2.5, "service": "standard", "pickup_slot": "evening", "address_quality": 0.31,
  "hub_load": 52, "payment": "cod", "promised_days": 6}'

Expected output (2 November 2026): {"probability_late": 0.8533, "likely_late": true, "threshold": 0.3, "model_version": "2026.07.1", "request_id": "…"}. The same request without address_quality returns 422 with "type": "missing" and "loc": ["body", "address_quality"]. A weight_kg of −1 returns 422 with "Input should be greater than 0".

🌍Where it's used

📝Case scenario

A pricing model accepted a distance field in metres from one app and kilometres from another. Neither failed; prices were wildly wrong for one app's users for a week. Adding a schema with a named unit (distance_km) and a range check, plus a test with each app's real request, ended it.

🖱️Try it

Start the solution API (once Project 2 works) and send: a request with "dest_zone": "moon", one with "customer_phone" added, and one with "address_quality": 1.5. Success: all three get 422 with a reason you could show to the app developer.

⚠️Common mistakes

🛠️For your project

Project 2's tests 2, 3 and 4 check the response fields and that missing, unknown, impossible and personal inputs are rejected.

✅Quick check

What does FastAPI return when a request doesn't match the schema?

Show the answer

422, with the reasons for each field.

Why forbid extra fields?

Show the answer

So inputs the model doesn't expect, including personal data, are rejected instead of being passed on or logged.

← PreviousBatch or online predictions
ProLearners Assist