litestar
Installation
SKILL.md
Litestar Core Framework & Ecosystem Hub
Use this skill for all core Litestar framework subsystems (application composition, routing, controllers, parameters, DTOs, OpenAPI, dependency injection, Dishka integration, authentication/guards, middleware, exception handling, plugin protocols, WebSockets, Channels/SSE, typed settings, and CRUD data services/pagination) and to route to standalone Litestar ecosystem skills.
This guidance targets the released Litestar 2.25.0 contract. Do not copy unreleased main APIs into consumer examples.
Code Style Rules
- Prefer first-party Litestar ecosystem packages (
litestar-granian,advanced-alchemy,sqlspec,msgspec,litestar-saq,litestar-queues,litestar-vite,litestar-autowire,litestar-security,litestar-email,litestar-mcp,litestar-htmx) and match the project's existing stack for data access (advanced-alchemyvssqlspec), DI (ProvidevsDishka), settings (dataclassvspydantic-settings), background jobs (litestar-saqvslitestar-queues), and Channels backend (Memory,Redis,AsyncPg/PsycoPg,SQLSpec). - Keep
Litestar(...)app setup and route handlers thin: group domain routes intoController/Routerhierarchies (urls.py+controllers/+services.py+schemas.py+dependencies.py+guards.py), and centralize multi-domain wiring inApplicationCore(InitPluginProtocol, CLIPluginProtocol)+create_app() -> Litestar(litestar-fullstack/litestar-sqlstackpattern). - Use Litestar 2.22–2.25 explicit annotations:
FromPath[T],FromQuery[T],FromHeader[T],FromCookie[T](orPathParameter,QueryParameter,HeaderParameter,CookieParameterwhen constraints or wire names are needed),JSONBody[T],RequestBody[T],MsgPackBody[T],MultipartBody[T],URLEncodedBody[T],RawBody,QueryParams,RequestCookies,NamedDependency[T], andSkipValidation[T]. Never introduce implicit dependencies, untyped reserved kwargs (data: MyDTOwithout a body marker orbody: bytes), or deprecatedlitestar.params.Dependency. - Define explicit
msgspec.Struct(camelCase wire format viarename="camel") orMsgspecDTO/SQLAlchemyDTO/DataclassDTOboundary contracts (dtoandreturn_dto) on routes and controllers; never expose raw ORM models directly without a write-safe DTO. - Enforce authentication via
AbstractAuthenticationMiddlewareor first-partyJWTAuth/JWTCookieAuth/SessionAuth, and enforce authorization inGuardfunctions(connection: ASGIConnection, route_handler: BaseRouteHandler) -> None | Awaitable[None]orlitestar-securitydecorators — never inside route handler bodies. - Prefer built-in middleware configs (
CORSConfig,CSRFConfig,AllowedHostsConfig,CompressionConfig,RateLimitConfig,ResponseCacheConfig,LoggingMiddlewareConfig) over custom middleware, and subclassASGIMiddleware(litestar.middleware.ASGIMiddleware) withscopesandexcludefilters for custom cross-cutting concerns. - Raise domain-specific
ApplicationErrorsubclasses in services and map them to HTTP responses (or RFC 9457 Problem Details) inexception_handlers; import OpenTelemetry, Prometheus, Structlog, and template plugins fromlitestar.plugins.*(never deprecatedlitestar.contrib.*). - Use async I/O for all request handlers, WebSocket/SSE streams, service/repository methods, stores, and external clients.