FastAPI Basics for ML Engineers
FastAPI app, GET/POST endpoints, path and query params, async def vs def for ML inference.
FastAPI Basics for ML Engineers is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why FastAPI for ML?
FastAPI is a modern Python web framework ideal for serving models: it is fast, auto-generates interactive docs, and validates requests with type hints. It has become the default way to wrap a model behind an HTTP API.
Creating the App
Instantiate a FastAPI application; this app object holds all your routes.
from fastapi import FastAPI
app = FastAPI(title="Model Serving API")A GET /health Endpoint
A health check lets load balancers and orchestrators confirm the service is alive. It returns a simple status with no heavy work.
@app.get("/health")
def health():
return {"status": "ok"}A POST /predict Endpoint
Predictions take input data, so they use POST with a request body. The function receives the parsed payload and returns the model output.
@app.post("/predict")
def predict(features: list[float]):
result = model.predict([features])
return {"prediction": result[0]}GET vs POST
- GET: retrieve data, no body, used for health checks and simple reads.
- POST: send data in the body, used for predictions where you submit features.
Running with uvicorn
uvicorn is the ASGI server that runs FastAPI. Start it from code or the command line.
import uvicorn
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)Command-Line Launch
More commonly you launch via the CLI, pointing uvicorn at the module and app object. --reload auto-restarts on code changes during development.
# terminal
uvicorn main:app --host 0.0.0.0 --port 8000 --reloadInteractive Docs for Free
FastAPI auto-generates Swagger UI at /docs and ReDoc at /redoc from your type hints, so you can test endpoints in the browser with zero extra code.
async def for IO-Bound Work
Use async def for endpoints that wait on IO (database, external APIs, file reads). Async lets the server handle other requests while waiting, improving throughput.
@app.get("/external")
async def fetch():
data = await call_remote_service()
return datasync def for CPU-Bound Inference
Model predict is usually CPU-bound. Define those endpoints with plain sync def; FastAPI runs them in a threadpool so they do not block the event loop. Using async for CPU-heavy work would actually freeze the server.
@app.post("/predict")
def predict(features: list[float]):
# CPU-bound inference -> sync def
return {"prediction": model.predict([features])[0]}Choosing async vs sync
- async def: IO-bound, awaits network/disk.
- sync def: CPU-bound model inference, runs in a threadpool.
Picking the right one keeps the server responsive under load.
Quick Check
Test your FastAPI basics.
Recap
You built a FastAPI service: created the app, added GET /health and POST /predict, ran it with uvicorn, and learned to use async def for IO-bound and sync def for CPU-bound inference. Next: Pydantic request/response schemas.
Frequently asked questions
Is the “FastAPI Basics for ML Engineers” lesson free?
Yes — the full text of “FastAPI Basics for ML Engineers” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “FastAPI Basics for ML Engineers”?
FastAPI app, GET/POST endpoints, path and query params, async def vs def for ML inference. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “FastAPI Basics for ML Engineers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- FastAPI Basics for ML Engineers
- Pydantic Schemas for Request and Response
- Loading and Serving ML Models
- Dockerizing the Model API