litestar-dto-openapi

Configures Litestar DTOs and OpenAPI schemas with msgspec-first request and response typing.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires litestar, msgspec, and includes references (resource) components.

What problem does it solve? Shaping API request and response contracts in Litestar apps is error-prone when persistence models leak into wire payloads, field naming is inconsistent, or the generated OpenAPI schema drifts from the intended contract. This Skill guides DTO selection, field exclusion, renaming, and encoding so the /schema output matches the intended API contract. ## Core Features & Use Cases - DTO selection and configuration: Choose between MsgspecDTO, DataclassDTO, and SQLAlchemyDTO, then apply DTOConfig knobs like exclude, include, rename_fields, rename_strategy, partial, and max_nested_depth. - CamelCase wire naming: Use a CamelizedBaseStruct pattern so Python stays snake_case while the API ships camelCase field names. - OpenAPI contract validation: Verify request body schemas, response DTOs, RequestEncodingType, and /schema output against the intended contract. - Use Case: When building a user endpoint, exclude server-owned fields like id and created_at from the write DTO, rename fields to camelCase, and confirm the generated OpenAPI schema exposes only the intended fields. ## Quick Start Ask the assistant to create a Litestar write DTO for your model that excludes server-owned fields and uses camelCase wire names, then check the generated OpenAPI schema.

Frequently Asked Questions about litestar-dto-openapi

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

FAQPage Schema
How do I exclude fields from a Litestar DTO?

Use DTOConfig with the exclude parameter, for example DTOConfig(exclude={"id", "created_at"}), to drop server-owned or sensitive fields from the DTO. You can also use include for a whitelist approach, but the two options are mutually exclusive.

How to rename Litestar DTO fields to camelCase?

Set rename_strategy="camel" in DTOConfig for bulk renaming, or define a msgspec Struct with rename="camel" as a shared base class. For individual fields, use rename_fields={"name": "full_name"} or msgspec.Meta with an extra rename entry.

MsgspecDTO vs DataclassDTO in Litestar, which should I use?

Use MsgspecDTO with msgspec Structs for greenfield API DTOs and maximum performance, since msgspec is Rust-backed. Use DataclassDTO or SQLAlchemyDTO when wrapping existing dataclasses or ORM models that you want to shape with DTOConfig.

How do I make all DTO fields optional for PATCH endpoints in Litestar?

Set partial=True in DTOConfig to make every field optional, which is the standard pattern for PATCH endpoints. This lets clients send only the fields they want to update without failing validation on omitted fields.

Should I switch a Pydantic-based Litestar project to msgspec DTOs?

No, do not switch an existing Pydantic-heavy project to msgspec opportunistically. The guidance is to prefer msgspec DTOs in greenfield Litestar apps but match the existing stack when the project is already Pydantic-led.