Advanced·22 min·0/52 done

Serving models with FastAPI

Turn a model or RAG pipeline into a validated HTTP service with health checks.

Why it matters: A notebook model has no product value until it is an API. FastAPI is the usual Python choice for inference endpoints.

Basics

Why FastAPI

You describe the request with types. FastAPI validates and returns 422 on bad input. It also builds docs at /docs.

Load the model once at startup, not per request. Loading a large model inside the handler wastes seconds and memory every call.

The browser lab simulates each FastAPI step, so you can learn the full request flow on this website.

Tiny example

Validate a request dict like an API model would.

def validate(body):
    if not body.get("question"):
        raise ValueError("question required")
    return {"ok": True, "q": body["question"]}
print(validate({"question": "What is RAG?"}))

Basics

Health and predict shapes

Useful endpoints:

  • GET /health - process is up
  • GET /ready - model is loaded
  • POST /predict or /chat - real work

Return real status codes: 400 bad input, 422 validation, 503 not ready. Do not hide errors inside a 200 body.

Tiny example

Sketch health and predict responses.

health = {"status": "ok"}
predict = {"label": "pass", "score": 0.91}
print(health)
print(predict)

Basics

Deploy checklist

Run with uvicorn. For GPU inference, usually one worker per GPU because each worker loads its own model copy.

Add timeouts, body size limits, and rate limits. Log request id, model version, latency, and token counts.

Tiny example

Print a short run checklist.

checks = ["health", "ready", "timeouts", "auth"]
for c in checks:
    print("[ ]", c)

Then AI / ML

AI / ML examples

Same idea, used in real AI work. Press Try in lab to run it.

AI / ML example

AI / ML: request validation for predict

Same rules pydantic enforces, written by hand.

In AI / ML: Inference APIs reject empty text and bad thresholds early.

example.py

Python

def validate_request(payload):
    errors = []
    text = payload.get("text")
    if not isinstance(text, str) or not text.strip():
        errors.append("text required")
    elif len(text) > 5000:
        errors.append("text too long")
    thr = payload.get("threshold", 0.5)
    try:
        thr = float(thr)
        if not 0.0 <= thr <= 1.0:
            errors.append("threshold out of range")
    except (TypeError, ValueError):
        errors.append("threshold must be a number")
    if errors:
        return {"status": 422, "errors": errors}
    return {"status": 200, "text": text.strip(), "threshold": thr}

for p in [{"text": "good"}, {"text": "  "}, {"text": "ok", "threshold": 5}]:
    print(p, "->", validate_request(p))

AI / ML example 2

AI / ML: FastAPI request flow

Learn the endpoint flow without installing anything.

In AI / ML: Production inference services follow this health + predict pattern.

example.py

Python

flow = [
    "load model once",
    "check health",
    "validate request",
    "predict",
    "log latency",
]
for step in flow:
    print(step)

Your turn · answer checked automatically

Exercise

Finish handle(payload) so it returns exactly these dicts: {"status": 422, "detail": "text required"} when text is missing or blank, {"status": 422, "detail": "too long"} when text is over 200 characters, and otherwise {"status": 200, "label": ...} where the label is positive if the text contains good or great (any case), else neutral.

Edit the code, then press Run & check. Passing it marks this lesson done.

python console

Exercise

~/lesson.py

Ctrl+Enter

console output

ready

$ waiting for Run...

On your laptop

Run the real thing on your computer

The lesson above simulates a model service in plain Python. Here is the real FastAPI version. FastAPI checks every request for you (the 422 errors come for free) and builds interactive docs.

  1. 1

    Make a folder called sentiment-api, save app.py into it, and open a terminal there.

  2. 2

    Create a virtual environment, so this project's packages stay inside this folder. On Mac and Linux, type python3 instead of python everywhere.

    python -m venv .venv
  3. 3

    Turn it on. Windows: .venv\Scripts\activate. Mac or Linux: source .venv/bin/activate. Your terminal line now starts with (.venv).

  4. 4

    Install FastAPI and the Uvicorn server:

    pip install fastapi uvicorn

app.py

"""A tiny model service: a health check and a /predict endpoint."""
from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI(title="Sentiment API")


class PredictRequest(BaseModel):
    text: str = Field(min_length=1, max_length=200)


class PredictResponse(BaseModel):
    label: str


def predict_label(text: str) -> str:
    # Swap this rule for a real model later, for example the spam classifier project.
    lowered = text.lower()
    return "positive" if ("good" in lowered or "great" in lowered) else "neutral"


@app.get("/health")
def health():
    return {"status": "ok"}


@app.post("/predict", response_model=PredictResponse)
def predict(request: PredictRequest):
    return PredictResponse(label=predict_label(request.text))

Run it from the same folder:

uvicorn app:app --reload

You should see something like:

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Open http://127.0.0.1:8000/docs in your browser, click POST /predict,
then "Try it out". Send {"text": "great course"} and you get {"label": "positive"}.
Send {"text": ""} and FastAPI answers 422 on its own.

Takeaways

  • 1Load the model once at startup, never per request.
  • 2Ship /health and /ready separately.
  • 3Validate input and return real HTTP status codes.