litestar-settings

Configure typed Litestar application settings with env loading and cached factories.

Updated Aug 17, 2026
One-click install
npx skills add https://github.com/renjianguo666/litecms --skill litestar-settings-renjianguo666
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: litestar-settings
Source: https://github.com/renjianguo666/litecms/tree/main/.agents/skills/litestar-settings
Command: npx skills add https://github.com/renjianguo666/litecms --skill litestar-settings-renjianguo666

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Litestar applications need a consistent, typed way to load environment variables into configuration without scattering os.environ reads across handlers or re-parsing env vars on every request. This Skill provides the patterns for dataclass-based or pydantic-settings-based config with cached factories and app-state wiring. ## Core Features & Use Cases - Two settings patterns: Choose between zero-dependency @dataclass(frozen=True) + get_env() for fresh projects, or pydantic_settings.BaseSettings when Pydantic is already in the dependency graph. - Cached settings factories: Use @lru_cache(maxsize=1) on get_settings() so configuration is evaluated once per process and shared across all call sites. - Lazy materialization with PEP 562: Defer DB pools, Redis clients, and Channels backends until first attribute access so Docker builds, CLI commands, and test suites do not trigger import-time connections. - Use Case: You are bootstrapping a new Litestar API and need DATABASE_URL, REDIS_URL, and APP_SECRET_KEY loaded as typed, immutable settings with test-friendly overrides via get_settings.cache_clear(). ## Quick Start Ask the AI to create a typed Litestar settings module with a cached get_settings factory that loads DATABASE_URL and REDIS_URL from environment variables.

Frequently Asked Questions about litestar-settings

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I load environment variables into Litestar settings?

Define a frozen dataclass with field default_factory lambdas calling a get_env helper, then expose it through an lru_cache-decorated get_settings() factory. This evaluates env vars once per process and gives every call site the same immutable instance.

Should I use dataclass settings or pydantic-settings for Litestar config?

Use @dataclass(frozen=True) + get_env for fresh projects with no Pydantic dependency, matching canonical Litestar reference apps. Use pydantic_settings.BaseSettings when Pydantic is already in your stack, you need dotenv loading, or you want field-level validation on config values.

How do I override Litestar settings in tests?

Call get_settings.cache_clear() to reset the lru_cache, then set new env vars before the next get_settings() call. For lazy config modules, use a _reset() function that clears cached names and chains into sub-module resets.

Why does my Litestar Docker build fail with Redis connection errors?

Importing a config module that instantiates ChannelsPlugin or database pools at module scope triggers connections during the build, before services are running. Use PEP 562 module-level __getattr__ to defer materialization until first attribute access.

Can I use msgspec Structs for environment variable loading?

No, msgspec Structs lack first-class env-loading affordances and are optimized for request/response DTOs. Use dataclasses or pydantic_settings for config, and reserve msgspec for serialization boundaries.