Skip to the page

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.

The 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

  1. What is FastAPI built on? What do Starlette, Pydantic and Uvicorn each do?
  2. What does ASGI mean?
  3. How do you check that end_date is after start_date?
  4. How do you reject unknown fields?
  5. How do you update only the fields the client sent in a PATCH?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview