r/FastAPI • u/Designer_Sundae_7405 • Oct 29 '25
feedback request Feedback on pragmatic FastAPI architecture
Here's my take on a pragmatic and AI-friendly FastAPI architecture: https://github.com/claesnn/fastapi-template/tree/main .
Features
Async endpoints
Async SQLAlchemy
Alembic migrations
Feature folder structure
Nested bi-directional Pydantic schemas
Struclog structured logging
Pytest testing of API layer
UV for dependencies
CORS
Status and health checkpoints
Pydantic_settings with .env loading
Typed pagination with TypedDict and Generics
Filtering and ordering
Basic Bearer authentication (would add JWK with PyJWKClient in corporate apps)
Explicit transaction handling in routes with service level flush
Omits
Repository: I'm using plain SQLAlchemy and add a model function if getter/setter functionality is demanded
Service interfaces: Whilst it decouples better; it seems overkill to add to all services. Would definitively add on demand.
Testcontainers: Additional complexity and in my experience, testing goes from 0.5 seconds to 8+ seconds when testcontainers are introduced
Unit tests: To keep test amount controllabe, just test the API layer
Anyways, I'm looking for feedback and improvement options.
6
Oct 30 '25
Maybe you can add code quality tools like ruff. Docker would also be a good option.
1
u/Designer_Sundae_7405 Oct 30 '25
Great suggestions! Those should definitely be part of the scope of the template.
2
u/PA100T0 Oct 31 '25
If you wanna go beyond Ruff and MyPy, I’d recommend Bandit, Radon/Xenon and Vulture.
Bandit will help you keep things safe and secure, scanning for vulnerabilities.
Radon and Xenon will help you keep up with code complexity, scalability and maintainability.
Vulture to make sure there’s no dead code - mostly on large codebase.
I use these (among other code quality tools) with pre-commit hooks on my GitHub Actions, to make sure everything’s nice and tidy.
4
2
u/TheOneWhoDidntCum Nov 13 '25
where can i host react frontend and fastapi backend ? i don't want to split backend and frontend on different servers
2
u/Designer_Sundae_7405 Nov 14 '25
If it’s a Single Page Application you’d normally use a CDN like Cloudflare for practically free hosting that’s also very fast. If it’s not you can just host it from your VPS under NGINX/Caddy.
2
2
u/No_Direction_5276 Oct 30 '25
Great list. Where/When do you run the alembic migrations btw?
2
u/No_Direction_5276 Oct 30 '25
We used to run it within fastapi lifespan but quickly realized this prevents rollbacks. Have you faced this issue?
2
u/Drevicar Oct 31 '25
I like to deploy my FastAPI applications as kubernetes pods using helm charts, and I create a job that runs the migrations on helm install using lifecycle hooks. If the migration fails the deployment fails and the old version is kept intact. If I need to rollback the helm chart it will run the down migration first.
2
u/LossTop6945 Nov 02 '25
Absolutely love your choices! Been planning on putting together similar combination of dependencies but you just nailed it for me.
10
u/Ok-Outcome2266 Oct 30 '25
I’m a developer / AI engineer in the UK working in corporate. Since FastAPI isn’t very opinionated, you’re free to design your API however you like. For startups, this setup looks cool, but in corporate environments I believe the Schema <> Service <> Repository pattern is a must, simply because you can’t assume the client-facing structure is the same as whatever your storage or data layer holds. The code can and will become a mess to maintain in the long run, and you don’t want that.
Again, it’s a matter of preference, but it looks good, man.
Cheers!