API Engineering with FastAPI, Pydantic & Async Workers
CSC3119 · Lab

FastAPI Model Serving, Pydantic Schema Validation & Latency Simulator

Send good and bad patient payloads to /predict and see Pydantic reject them at the door. Then load the service and watch latency explode as workers saturate — queueing theory, not magic.

  1. 1 · Predict0/1
  2. 2 · Experiment0/3
  3. 3 · Explain0/1
  4. 4 · Code it0/2
  5. 5 · Challenge
  6. 0/5 tasks done

Predict first

Commit to a guess before you touch anything. Being wrong here is how it sticks.

An endpoint handles 120 req/s with 25 ms average latency. How many requests are in flight on average?

Experiment

Move the controls and watch the outputs. Goals tick off automatically when you reach them.

Pydantic at the boundary

Response
200 OK
validated → pipeline.predict_proba → JSON
{"stroke_probability": 0.384, "risk_tier": "HIGH", "action": "Refer to chronic care clinic"}

Throughput, latency & Little's Law

Capacity
200 req/s
Utilisation ρ
30%
Mean latency
28.6 ms
Requests in flight (L = λW)
1.71

Latency ≈ service time / (1 − ρ): it stays flat until ρ ≈ 70%, then shoots up.

Explain it

Put what you saw into words. This is what the exam will ask you to do.

How does Pydantic protect the model code, and how would you scale CPU-bound inference in FastAPI?

Code it

Write real code against real data. Python and SQL run right here in your browser and hidden tests check your work; the AI reviews it like an examiner.

Pythoncore

The Patient request schema

Define Pydantic models for the stroke API:

  • Patient: age float 0–120; avg_glucose_level float > 0; bmi optional float between 10 and 80 (default None); hypertension and heart_disease bools; smoking_status one of "never", "formerly", "smokes", "unknown".
  • Prediction: stroke_probability float 0–1, risk_tier one of "LOW", "HIGH", model_version str.
Ctrl/⌘ + Enter to run

Challenge

Fixed variations, a new problem every day, and unlimited AI challenges built from your current settings.

Little's Law API Concurrency Capacity

A stroke screening endpoint handles an arrival rate λ = 250 requests/sec with average model inference latency W = 40 ms (0.040s). According to Little's Law (L = λ × W), how many concurrent requests are in flight on the server?