{"id":4085,"library":"litestar","title":"Litestar","description":"Litestar is a production-ready, highly performant, and extensible ASGI API framework, currently at version 2.21.1. It emphasizes type safety, developer experience, and modularity, supporting features like dependency injection, OpenAPI schema generation, and ORM integration. The project maintains a frequent release cadence, often issuing minor versions monthly or bi-weekly.","status":"active","version":"2.21.1","language":"en","source_language":"en","source_url":"https://github.com/litestar-org/litestar","tags":["web framework","asgi","api","http","async","validation","openapi","dependency injection","typescript"],"install":[{"cmd":"pip install 'litestar[standard]'","lang":"bash","label":"Recommended installation including Uvicorn"}],"dependencies":[{"reason":"Recommended ASGI web server, included with 'litestar[standard]' extra.","package":"uvicorn","optional":true},{"reason":"Optional data validation and settings management. While Litestar's core uses msgspec, Pydantic is fully supported via a plugin for model definition and validation.","package":"pydantic","optional":true},{"reason":"For database integration, especially when using the SQLAlchemyPlugin (requires SQLAlchemy 2.x).","package":"sqlalchemy","optional":true},{"reason":"Asynchronous driver for SQLite, often used with SQLAlchemy for async database operations.","package":"aiosqlite","optional":true},{"reason":"Required for SQLAlchemy's async support with some database drivers.","package":"greenlet","optional":true}],"imports":[{"symbol":"Litestar","correct":"from litestar import Litestar"},{"symbol":"get","correct":"from litestar import get"},{"symbol":"post","correct":"from litestar import post"},{"symbol":"Controller","correct":"from litestar import Controller"},{"note":"The `AbstractMiddleware` class was deprecated in Litestar v2.15, favoring `ASGIMiddleware` for creating custom middleware.","wrong":"from litestar.middleware.base import AbstractMiddleware","symbol":"ASGIMiddleware","correct":"from litestar.middleware import ASGIMiddleware"},{"note":"`TestClient` can cause event loop issues with async fixtures; `AsyncTestClient` is recommended for async testing.","wrong":"from litestar.testing import TestClient","symbol":"AsyncTestClient","correct":"from litestar.testing import AsyncTestClient"},{"note":"Following the rebranding from Starlite to Litestar in v2.0, all top-level imports and sub-package paths changed.","wrong":"from starlite.ASGIConnection","symbol":"ASGIConnection","correct":"from litestar.connection import ASGIConnection"}],"quickstart":{"code":"from litestar import Litestar, get\nimport os\n\n@get(\"/\")\nasync def hello_world() -> str:\n    return f\"Hello, {os.environ.get('NAME', 'World')}!\"\n\napp = Litestar(route_handlers=[hello_world])\n\n# To run this application:\n# 1. Save it as app.py\n# 2. Run in terminal: litestar run --reload\n# 3. Access at http://127.0.0.1:8000/","lang":"python","description":"This minimal example demonstrates how to create a Litestar application with a single GET endpoint. It uses an environment variable for a customizable greeting. The 'litestar run' command requires 'uvicorn', which is included with the 'standard' extra."},"warnings":[{"fix":"Update all `starlite` imports to `litestar`. Review the official v2 migration guide for a complete list of changes.","message":"Project Rebranding (Starlite to Litestar): With the release of v2.0, the framework was renamed from 'Starlite' to 'Litestar'. All import paths must be updated accordingly (e.g., `from starlite import ...` becomes `from litestar import ...`).","severity":"breaking","affected_versions":"2.0.0 and later"},{"fix":"Explicitly install `pydantic` if you need to use Pydantic models. For non-Pydantic types (dataclasses, attrs, msgspec structs), ensure they are correctly typed; Litestar will handle them directly, potentially improving performance.","message":"Pydantic Integration Changes: Pydantic was removed from Litestar's core in v2.0, making it an optional dependency. While Pydantic models are still fully supported via a plugin, the framework's internal data handling switched to `msgspec` for performance. If you were implicitly relying on Pydantic for non-Pydantic types, behavior might differ.","severity":"breaking","affected_versions":"2.0.0 and later"},{"fix":"Migrate custom middleware implementations to extend `litestar.middleware.ASGIMiddleware` instead of `AbstractMiddleware`.","message":"Middleware API Evolution: The `MiddlewareProtocol` and `AbstractMiddleware` classes were deprecated around v2.15 in favor of the `ASGIMiddleware` abstract base class. This change simplifies middleware configuration and dispatching.","severity":"breaking","affected_versions":"2.15.0 and later"},{"fix":"Always use precise type hints for function arguments and return values in route handlers, dependencies, and DTOs. Ensure Pydantic models, dataclasses, or other data structures are correctly defined.","message":"Strict Type Hinting Enforcement: Litestar rigorously uses and enforces Python type hints for data validation, parsing, serialization, and OpenAPI schema generation. Failing to provide accurate type hints (e.g., return types for route handlers) can lead to runtime exceptions.","severity":"gotcha","affected_versions":"All v2.x versions"},{"fix":"Prefer `litestar.testing.AsyncTestClient` for asynchronous tests, as it's designed to manage event loops correctly within the test context. Ensure your test setup respects the asynchronous nature of the client and application.","message":"Event Loop Management in Async Tests: When using `httpx.AsyncClient` or the synchronous `TestClient` with async testing frameworks like `pytest-asyncio`, running the Litestar application in a different event loop than the test/fixture can lead to `RuntimeError: Event loop is closed`. This is a common pitfall in async Python testing.","severity":"gotcha","affected_versions":"All v2.x versions"},{"fix":"If you need to customize responses for 404 or 405 errors, ensure their exception handlers are passed directly to the `Litestar` constructor when initializing your application.","message":"Exception Handler Scope for 404/405 Errors: `NotFoundException` (404) and `MethodNotAllowedException` (405) are raised by Litestar's ASGI Router before the middleware stack is fully invoked. Consequently, these exceptions can only be handled by exception handlers registered directly on the `Litestar` application instance, not by handlers defined on specific controllers or route handlers.","severity":"gotcha","affected_versions":"All v2.x versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}