FastAPI · core
What FastAPI is built on, and validating requests with Pydantic
FastAPI is a thin layer over Starlette (the ASGI toolkit) and Pydantic v2 (validation), run by a server such as Uvicorn. Type hints become validation, documentation and OpenAPI.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- ASGI is the async successor to WSGI: an app is an async callable taking (scope, receive, send). It supports HTTP, WebSockets and lifespan events.
- Starlette provides routing, requests and responses, middleware, WebSockets, background tasks and TestClient. Uvicorn (or Gunicorn with an ASGI worker, Hypercorn, Granian) is the server.
- Pydantic v2 (Rust core) validates and converts input, serialises output and makes JSON Schema. FastAPI dropped Pydantic v1 at the end of 2025.
- FastAPI adds parameters from type hints, dependency injection, automatic OpenAPI with Swagger UI at /docs and ReDoc at /redoc. A validation error returns 422 with the location, message and type of each error.
- Declare a BaseModel as the parameter. One-field rules: Field(gt=0, max_length=...) or @field_validator. Rules across fields: @model_validator(mode="after") that raises ValueError.
- Unknown fields are ignored by default. Use ConfigDict(extra="forbid") to reject them. Lax mode turns "25" into 25; strict=True rejects it.
- Use a separate response_model so internal fields never leave. model_dump(exclude_unset=True) for PATCH so only the fields sent are updated.
- v1 to v2 renames: .dict() to model_dump(), .parse_obj() to model_validate(), @validator to @field_validator, orm_mode to from_attributes=True, inner Config class to model_config. FastAPI is 0.x: pin the version and read release notes before upgrading. fastapi dev reloads for local work; fastapi run is for production.
The red pen
Common traps: mistake → fix
Mistake: FastAPI is a web server
Fix: it is a framework; Uvicorn is the server.
Mistake: Extra fields in the JSON are rejected by default
Fix: they are ignored unless extra="forbid".
Mistake: Return the ORM object directly
Fix: it can expose password_hash; use response_model.
Mistake: A validation error returns 400
Fix: FastAPI returns 422.
Easy to hard
Questions interviewers ask, easy to hard
- What is FastAPI built on? What do Starlette, Pydantic and Uvicorn each do?
- What does ASGI mean?
- How do you check that end_date is after start_date?
- How do you reject unknown fields?
- How do you update only the fields the client sent in a PATCH?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.