Course Content
FastAPI Essentials
1 sections · 32 lessons
What is FastAPI?
What you need to know
An HTTP API is a program that other programs call over the web. A mobile app, a ticketing system or another service sends a request such as POST /sentiment with some JSON, and gets JSON back. A web framework is the library that turns those raw requests into calls to your Python functions.
FastAPI is that library, assembled from two well-known parts:
| Part | What it does for you |
|---|---|
| Starlette | The web layer: routing, requests and responses, middleware, WebSockets, streaming |
| Pydantic | The data layer: checks incoming data, converts types, turns objects into JSON |
| Your type hints | The glue: FastAPI reads them to know what each endpoint expects |
Here is a complete sentiment-model API:
1from fastapi import FastAPI2from pydantic import BaseModel, Field34app = FastAPI(title="Sentiment API")56class Review(BaseModel):7 text: str = Field(min_length=1, max_length=2000)89class Sentiment(BaseModel):10 label: str11 score: float1213def predict(text: str) -> Sentiment: # stand-in for a real model14 good = any(w in text.lower() for w in ("good", "great", "love"))15 return Sentiment(label="positive" if good else "negative", score=0.91)1617@app.post("/sentiment")18def sentiment(review: Review) -> Sentiment:19 return predict(review.text)Calling it with FastAPI's TestClient gives these real responses:
POST /sentiment {"text": "Delivery was great"}200 {'label': 'positive', 'score': 0.91}POST /sentiment {"text": ""}422 {'detail': [{'type': 'string_too_short', 'loc': ['body', 'text'], 'msg': 'String should have at least 1 character', ...}]}Notice what you did not write: no JSON parsing, no if not text checks, no error formatting, no documentation. The Review class did all of that. The empty string was rejected before predict ever ran, and /docs already shows a page where anyone can try the endpoint.
What FastAPI gives you, and what it does not
- Gives you: request validation, JSON conversion, OpenAPI docs,
asyncsupport, dependency injection, streaming responses and a test client. - Does not give you: a database layer, an admin panel, user accounts or a job queue. You pick those yourself (SQLAlchemy, a login provider, Celery and so on).
- Needs a server to run. FastAPI is the app, not the server. After
pip install "fastapi[standard]"you start it withfastapi dev main.pywhile developing, orfastapi run/uvicorn main:appin production.
A real-life example
An e-commerce support team has a sentiment model in a notebook. They want every new ticket (about 3,000 an hour) scored so angry customers jump the queue. The engineer wraps the model in the 20 lines above, adds the model file to a Docker image, and gives the ticketing team the /docs link.
In the first week, the ticketing system sends some tickets with an empty body because of a bug on their side. Instead of the model crashing or scoring an empty string as "negative", those calls get a clear 422 that names the field. The ticketing team fixes their bug from the error message alone. This is the everyday value of FastAPI: a model becomes a safe, documented service in an afternoon.
The same pattern runs at much larger scale. vLLM's OpenAI-compatible server, used to serve open-weight LLMs, is itself a FastAPI app.
Follow-up questions to expect
- "Is FastAPI a web server?" — No. It is a framework. An ASGI server such as Uvicorn listens on the port and passes each request to the FastAPI app.
- "Why is it called fast?" — Two reasons: it is fast to write (little boilerplate), and it performs well for a Python framework thanks to async and Pydantic's Rust core. Your model's inference time is still the real bottleneck.
- "What does the 422 status mean?" — "Unprocessable content": the JSON was readable but broke the declared rules. FastAPI sends it automatically when validation fails.