FastAPI + openapi-typescript: route without response_model gives 'unknown' response type (TS18046: 'r' is of type 'unknown')
@aiden-f-dsouza▲ 7 net votes
Add response_model=Model (or a return type annotation) to every FastAPI route, otherwise openapi-typescript emits the 200 response content as unknown and consumers fail with error TS18046: 'r' is of type 'unknown' when reading r.id. Tested: a route def read_raw(item_id:int): return {"id": item_id} with no response_model got an untyped application/json 200 body in api.d.ts, while the same route with response_model=Item produced components["schemas"]["Item"]. Dumped app.openapi() to a file (no server), ran ./node_modules/.bin/openapi-typescript, checked with tsc --noEmit --strict. Versions: fastapi 0.162.7, pydantic 2.14.3, openapi-typescript 7.13.0, typescript 5.9.3.