Q&A-1A teammate says 'FastAPI is a Python web framework that validates requests with Pydantic.' Which parts of a FastAPI service come from FastAPI itself, which from Starlette and which from Pydantic, and why does the split matter when you debug a 422 response?
Answer
The 60-second answer. FastAPI is a layer on top of two libraries. Starlette is the ASGI toolkit underneath: it owns the application object, routing, middleware, requests and responses, and the ASGI server interface that Uvicorn or Hypercorn drives. Pydantic owns data modelling and validation: it turns a JSON body or a typed value into a model instance, or raises a structured error. FastAPI itself adds what sits between them: it reads your function signature and type hints at startup, decides which parameter comes from the path, query, header, cookie or body, builds the Pydantic models for them, calls your endpoint with the validated values, serialises the return value through a response model, and generates the OpenAPI schema from all of that. So when a candidate says FastAPI is a framework, the precise version is: a signature-driven composition layer over Starlette routing and Pydantic validation.
The split matters because a 422 Unprocessable Entity is produced by FastAPI, from a Pydantic ValidationError, for a request that never reached your function body.
# FastAPI 0.115 on Pydantic 2.x
from fastapi import FastAPI
from pydantic import BaseModel, Field
app = FastAPI()
class Order(BaseModel):
sku: str
qty: int = Field(gt=0)
@app.post("/orders")
async def create(order: Order, limit: int = 10) -> Order:
return order
Posting {"sku": "A1", "qty": 0} gives a 422 whose body lists loc: ["body", "qty"], type: "greater_than". The body prefix comes from FastAPI, which knows where the value was read from. The constraint text comes from Pydantic. Your handler never runs. Posting ?limit=abc gives loc: ["query", "limit"], and that is FastAPI's parameter analysis, not Pydantic seeing a body.
Where the layers fail, and how you tell.
- Response shape drifts from the declared model. If the handler returns an ORM object without a response model, FastAPI serialises whatever attributes the encoder can reach. Detect: the OpenAPI schema and the actual JSON disagree, and a
response_modeldeclared on the decorator is what makes the output filtered and validated. - Blocking code inside
async def. This is a Starlette and asyncio problem, not a FastAPI one. The event loop stalls for every request on the worker. Detect: p99 rises across unrelated routes at the same moment, and a plaindefendpoint on the same app does not show the stall, since FastAPI runs those in a threadpool. - Middleware ordering. Middleware is Starlette's; FastAPI exposes it unchanged. A 500 that skips your logging middleware was raised by a layer outside it.
When not to lean on the framework. For a service whose inputs are untyped or arrive as a stream, the signature-driven binding gives you less than hand-written parsing with explicit limits. Say so when it matters.
Curated: · Written: · Reviewed: