5 ms·
Litestar is worth a look
- hnuser123456 1y agoIt's a python web framework, for those curious to know more before clicking through.
- LigmaBaulls 1y agothanks, saved me some time.
- monadoid 1y agoThis is well written, thanks!
- cr125rider 1y agoLitestar is awesome. It’s great it’s got more than a single maintainer too.
- ddejohn 1y agoExcellent post that actually gets into important details for real-world applications. I'm a huge fan of the design of Litestar. > I also still think there are a lot of bad use cases for repositories and service layers that people should avoid, but that’s a digression which should probably become its own post As a huge proponent of the repository pattern, I'll be looking forward to this post.
- punnerud 1y agoGood to see it using port 8000 as default, and not Flasks 5000 (does not work on Mac anymore)
- femtozer 1y agoI wasted a couple of hours recently before realizing that AirPlay was using port 5000...
- NeutralForest 1y agoPretty cool post! I'm not sure how I feel about SQLAlchemy (not the star of the post but mentioned quite a bit); it's such a big ball of state that has so many surprises, I wonder if some people build entirely without it.
- devjab 1y agoThere is a rather big difference between traditional SQLAlchemy and Advanced Alchemy which is also made by Litestar. We've build with pure SQL and with SQLAlchemy in the past, but since we transitioned from django ninja to Litestar, we've not used SQLAlchemy and are slowly moving away from it. Well I guess Advanced Alchemy is still SQLAlchemy under the hood.
- VerminOctopus1 1y agoMind elaborating a bit on why you migrated away from django ninja? Just curious, I’ve been using it for some small side projects and have enjoyed it.
- devjab 1y agoDjangi-ninja is excellent, but it's still Django and when you aren't using a lot of the "batteries included" then you're not really using Django. I mentioned SQLAlchemy which is already "fighting Django" compared to using Django Orm as an example. We picked Litestar because it's natively async, makes it easy to use dataclasses rather than Pydantic, has really fast cold start and it has great interoperability with Advanced Alchemy. I think Django is great, and by extension that Django-Ninja is too. Considering it runs Instagram it certainly scales as well, but unlike Instagram we aren't enough engineers to be able of ripping out more and more batteries while replacing them with our own specialized batteries.
- sambaumann 1y agoI recently built a personal project using peewee and it doesn't have a ton of bells and whistles but it works well for what it does do.
- hariwb 1y agoThanks for writing this. I have similar gripes about FastAPI having developed an application over the past few years; I'm also continually surprised at how prevalent the attitude is that FastAPI has excellent docs, given how divorced the tutorial / toy examples in the docs are from real-world development and measurement of an API.
- rtpg 1y agoI am really disappointed at the new generation of Python frameworks' documentation, which seem to have the same "docs are tutorials + chatty blog posts which imprecisely describe the APIs" attitude of Javascript libs. Two words: API Reference. Have the clinical explanation of methods exposed, with actual breakdowns of what method parameters do. List them all, don't surround it by prose. List out the options! Don't make me dive into the source to find out what I can or can't pass into a parameter! Having to play this game of "alright I want to know how to use this API, so first I need to figure out what tutorial page _might_ use this" to find the tiny examples that should just be next to the methods I care about in the reference is really frustrating.
- rmonvfer 1y agoThis, I’ve already said it in another reply but FastAPI is 10x harder to use because of this. I’ve had to read FastAPI code many times to literally reverse engineer features because they are not documented in any way. Documentation is for reference, tutorials are for learning, I just don’t even understand how maintainers don’t go crazy with the absolute lack of references… And SQL Model is even worse in that regard.
- rtpg 1y agomaintainers just having to assume every behavior is needed for backwards compatibility... and you still have the absolute mess which was pydantic 1 -> 2 (or django-ninja 0.x -> 1.0) Everyone talks about moving fast and being dynamic but everyone I know deep in this has lost like actual years to churning from this kind of behavior.
- deleted 1y ago
- cbzbc 1y agoHow do people deploy this framework typically - speaking for myself, I found NGINX Unit somewhat fiddly.
- devjab 1y agoWith uvicorn.
- intalentive 1y agouvicorn systemd service behind nginx reverse proxy
- indigodaddy 1y agoOr just docker/uvicorn
- brokegrammer 1y agoI tend to deploy all Python based apps using gunicorn behind a caddy proxy. Take a look at the compose.yml https://github.com/confuzeus/simple-django/blob/master/ansible/roles/django/files/compose.yml https://github.com/confuzeus/simple-django/blob/master/ansib... Deployment is as simple as `docker compose pull && docker compose up -d`.
- intalentive 1y agoI've been using Litestar for over a year now, serving both JSON and templated HTML. Great all-around Python async framework that manages to be fast (faster than FastAPI), lightweight, and still has enough batteries included to host a website with auth, sessions, etc. I'm a fan of first-class msgspec support and the Controller class for nested routing. Highly recommend.
- thewisenerd 1y agolove litestar.. working on migrating a couple of internal consoles to it from fastAPI. the docs could use some love though. i feel most of it is references [1], the "how to"s could be better. inb4, "where pull request", i don't grok asgi or the framework nuances to be able to say how to improve on it. [1] https://diataxis.fr/ https://diataxis.fr/
- andrewstuart 1y agoI love Starlette but not a fan of FastAPI and do not use it. I read this article but didn’t really get the sense there was anything sufficiently compelling to switch from Starlette.
- hangonhn 1y agoHuge fan of Starlette too. I use it for all my projects at work. That said, for some of my coworkers, I’ve pointed them to Litestar because it’s very similar to Starlette (I think it was originally built on it) but it has dependencies injection. It’s an useful feature for some projects.
- rmonvfer 1y agoThank you for writing this, I've been building a large backend with FastAPI for the last year or so and I've gone through all the levels of the purgatory. I began using the standard "tutorial" style and started cringing when I saw the official template [1] place all CRUD operations in a single file (I've been doing Rails and Spring for a while before) and the way dependencies where managed... let's just say I wasn't feeling very comfortable. Then came the SQLModel problems. The author pushes it very hard in the FastAPI docs (which imho are terrible because when I'm looking for docs I want that, documentation, not a fancy tutorial) but as an ORM (yes I know its a layer on top of SQLAlchemy) it doesn't even support polymorphic models and the community even has contributed PRs that have gone months without any review (is it even maintained anymore? I honestly can't tell). I guess I'm the only one to blame for choosing FastAPI to build a big project but after having used it quite a lot (and also read its code because again, docs are extremely poor) I wouldn't recommend it for anything serious. Sure, if you want to build a quick CRUD then go ahead and use SQLModel and FastAPI, but keep in mind that its not built for complex applications (at least not without requiring you to write a framework on top, like I've unfortunately done). So yeah, a big thank you to the author of this post because I will migrate to Litestar as soon as I wake up tomorrow.
- rmonvfer 1y agoedit: reading the litestar docs, it even has a built-in event system! I spent a couple weeks building something I could use with FastAPI...
- sureglymop 1y agoLooking at the docs and trying to figure out what this is for. Is it essentially when you want to break out of the "request lifecycle" and queue something to run after your response has already been returned? It strikes me that I haven't used web frameworks a lot and never even questioned how that may not be an easy thing to do!
- dontlaugh 1y agoThey seem to fill the same purpose as django's signals.
- zokier 1y agoI know fastapi gets the hype, but I have found plain starlette quite usable by itself. Sure, it doesn't have the whole kitchen sink, but if you just need something small and simple then it fits the bill. In comparison Litestar seems closer to fastapi/django in scope
- holler 1y agoSame, I've built all my recent api's in Starlette alone and I find it excellent. It's clean, concise, well documented, and you can extend it as needed, supporting small -> very large projects.
- rick1290 1y agoHow does this compare to Django? I see you have quite a bit of Django content. How would you decide to use Litestar vs Django on a new greenfield project?
- brokegrammer 1y agoLitestar looks promising because it has most of the features a Django developer would but in a more modern package. However, since it has so many features, I hope that the project doesn't get abandoned like so many frameworks these days because unlike tiny frameworks, it would be harder to migrate off of.
- davepeck 1y agoI think Litestar is superb for building API backends. Love it; use it; only good things to say. Their Advanced Alchemy is coming along nicely, too. Litestar of course supports old-school server-template-rendered sites, too; it even has a plugin for HTMX requests and responses. In practice, I find that the patterns that serve API endpoints so well sometimes get in the way of old-school "validate form and redirect, or re-render with errors" endpoints. In particular, Litestar has no "true" form support of its own; validation is really intended to flag inbound schema mismatch on API calls, not flag multiple individual error fields. Using `@post("/route", exception_handlers={...})` is pretty awkward for these endpoints. I'd be excited to see some better tools/DX in-the-box here.
- murkt 1y agoI haven’t ever used Litestar, but it seems like it would be possible to write your own decorator `@postform` that handles all of form-related stuff.
- androiddrew 1y agoWhen I look at a litestar all it feels a lot more planned out and patterned. I wish I was better at async. I’ve recently converted to Golang, but I’d love to come back and do a litestar app in the future.
- jaza 1y agoNice write-up! I've heard about Litestar now and then, haven't tried it, maybe I should give it a go. I've been using FastAPI quite heavily for the past several years. I think OP's arguments about FastAPI being hard to work with in a bigger codebase are exaggerated. Splitting up the routes into multiple files, each with its own route object, and then importing and building up a big hierarchy of route objects, isn't that hard, it does the job for me. Agreed that it's probably not well documented enough, how to structure a larger FastAPI codebase - but follow a mix of best practices and your personal tastes, break it up into modules, split it into specific files for constants / errors / routes / schemas / crud / etc, and you can scale up sanely. I haven't used SQLAlchemy with FastAPI - for my day job I mainly connect to data stores for which it doesn't make sense - so maybe I'm biased, because I've avoided that pain.
- baggiponte 1y agoLitestar is really underrated but deserves much more usage! I’ve been meaning to try it for at least a year now, but always felt a bit scary to tell the team “hey let’s deviate from our stack”
- dtkav 1y agoConnexion is also worth a look IMO. It uses spec-first development (a major benefit in larger orgs and for public APIs), and can plug into different server frameworks. (I used to be a maintainer, but it has been years since I worked on it).
- 8organicbits 1y ago> So if you’re going to be writing a database-backed web application in Python, and you’re not doing Django, you are almost certainly going to be using SQLAlchemy. I've preferred the Django ORM over SQLAlchemy, but I'm curious what others feel. I've gone so far as to use Django ORM for non-web projects as well. It takes a bit of work to extract though. If Django ORM had a better stand-alone story, I think more people would use it.
- globular-toast 1y agoSQLAlchemy is just a totally different beast to Django, it has a much higher learning curve but gives you so much more power and flexibility. It's a true data mapper ORM rather than the sad Active Record pattern which starts off well and quickly gets annoying.
- adrianh 1y agoI've been using the Django ORM for 20 years, and it has yet to get annoying. What's your definition of "quickly" — perhaps 25 years?
- globular-toast 1y agoObviously it will depend on what you're doing and one's tolerance for things many deem to be annoying. I've encountered people with incredibly high tolerance for slow and awkward workflows but, alas, I am not one of them. If I had to call out one thing it would be that you can't do tests without having a database there. This results in incredibly slow tests for even the simplest things. I don't need to test database persistence every time I'm testing some domain logic. So maybe then don't do fat models and "map" the data from Django models to a domain layer? Well, congrats, you've just manually implemented a data mapper ORM, which is what SQLAlchemy is. It works well for simple CRUD stuff, which is really useful. But it very quickly becomes a mess and a big ball of mud when you start to do more complicated things. IMO a db access layer or web framework should be completely independent of domain logic, but Django makes that really difficult.
- thelastbender12 1y agoThank you for writing this - there is a very clear split you feel when using fastapi for single script web servers, vs trying to organize it. And I probably share all the mentioned annoyances around writing bigger projects with fastapi.
- Copenjin 1y agoWhen will people understand that those very opinionated frameworks with enticing tutorials just ruin your life in the long term? I see someone citing Spring(yikes) elsewhere, that falls in the same category as FastAPI. You don't need Spring most of the times, a simple dependency injection library and small frameworks to handle the web routing or specific features you need are often enough (recent contenders to the throne of default app framework have the same issues).
- globular-toast 1y agoAm I the only one who prefers to just have separate models for API and database right from the start? I know it looks not DRY, but it is. Your API and your database schema are not the same thing. It's never that long before you need them to be separate so why not do it right from the start? I feel like this might actually be a win for LLMs because you won't feel the pain of adding a new field to both the db model and API model in trivial cases. Litestar does look great and a true web framework like Flask and Starlette. Stuff like FastAPI and SQLModel is a joke imo. Developers should be able to compose these things themselves if they want to.
- twothreeone 1y agoAgreed 100% FastAPI works but building complex applications in it is just not great. Taking a step back (and this will date me but..) I'm still astonished how the "Python microframework world" is slowly rediscovering everything JavaEE had 15 years ago. Anyhow, this looks nice. Now tell me how to handle error cases during streaming.. >.<
- whinvik 1y agoI agree that FastAPI is not the best but I would not jump to another framework just because of an article. For FastAPI I know that I can go to [1] and see what a good FastAPI code base looks like. These days even Airflow is on FastAPI but haven't looked at the codebase. For me to jump on Litestar, I would like to see a reference codebase to learn best practices. Otherwise its one more framework whose quirks I have to get comfortable with. 1 - https://github.com/polarsource/polar https://github.com/polarsource/polar
- adr1an 1y agoReady, set... jump! There's a reference application [0] [0] https://github.com/litestar-org/litestar-fullstack https://github.com/litestar-org/litestar-fullstack
- mleonhard 1y agoDoes it have tooling to help keep Python [0] and TypeScript [1] DTOs in sync? [0] https://github.com/litestar-org/litestar-fullstack/blob/0996536189cf48c0b7ce8c2c2600cb439968a377/src/app/domain/accounts/schemas.py https://github.com/litestar-org/litestar-fullstack/blob/0996... [1] https://github.com/litestar-org/litestar-fullstack/blob/0996536189cf48c0b7ce8c2c2600cb439968a377/resources/types/api.ts https://github.com/litestar-org/litestar-fullstack/blob/0996...
- talos_ 1y agoJust want to comment that Litestar is awesome. Docs are great and the built-in caching feature is very convenient!
- scriptr 1y agohttps://github.com/litestar-org/litestar-fullstack https://github.com/litestar-org/litestar-fullstack is also nice on figuring out how to scale Litestar to a "common" app pattern that is medium+ size