litestar-responses
Responses
Execution Workflow
- Start with the simplest return form that matches the contract: plain typed data,
Response[T], or a specialized response container. - Set status code, media type, headers, and cookies explicitly when defaults are not enough.
- Choose specialized response types for redirects, files, streams, SSE, templates, or third-party ASGI responses.
- Apply response-class layering only when serialization or envelope behavior should be shared across many handlers.
- Verify OpenAPI accuracy for typed responses, documentation-only headers/cookies, empty-body statuses, and specialized response containers.
Core Rules
- Prefer plain typed returns for standard JSON responses.
- Use
Response[T]when you need runtime control over headers, cookies, background tasks, or status code. - Keep generic arguments on response classes precise so schema generation stays accurate.
- Document runtime headers and cookies when clients depend on them, especially if values are set dynamically.
- Treat
204,304, and other no-body responses asNone-returning contracts. - Keep success envelopes consistent with exception-handling envelopes where the API uses a shared contract.
- Use specialized response containers instead of reimplementing file, redirect, stream, or SSE behavior manually.
- Keep layered
response_class,response_headers, andresponse_cookiesoverrides intentional and local.
Decision Guide
- Return plain data when Litestar defaults already match the contract.
- Return
Response[T]when the payload is normal but metadata is dynamic. - Use
response_class=when many endpoints need the same serialization strategy. - Return
ASGIAppwhen wrapping third-party ASGI response objects or custom low-level behavior. - Use
File,Stream,ServerSentEvent, or redirect responses when the transport semantics are special. - Use subprocess test clients for live-server scenarios the in-process client cannot emulate well, especially infinite SSE streams.
Reference Files
Read only the sections you need:
- For plain typed returns,
Response[T], status codes, layeredresponse_class, custom response classes, and background tasks, read references/response-basics.md. - For headers, cookies, redirects, files, streams, SSE, templates, and ASGI app returns, read references/response-containers.md.
Recommended Defaults
- Let Litestar serialize normal JSON returns unless you need transport-level control.
- Use status-code constants from
litestar.status_codesin both code and tests. - Document dynamic headers and cookies with
documentation_only=Truemetadata and set the runtime value in the response or hook. - Keep stream and SSE generators cancellation-safe and bounded by disconnect behavior.
- Keep file delivery and redirect behavior explicit so intermediaries and clients behave predictably.
Anti-Patterns
- Returning
Response[Any]everywhere and losing schema precision. - Using custom response classes when a normal
Response[T]or built-in container is enough. - Returning bodies for
204 No Contentendpoints. - Setting dynamic headers or cookies without documenting them for OpenAPI when clients depend on them.
- Testing infinite stream or SSE behavior only with the in-process client.
- Reimplementing file serving manually instead of using
File.
Validation Checklist
- Confirm return annotations match the actual response body and status code.
- Confirm empty-body responses use
Noneannotations and bodies. - Confirm layered
response_class,response_headers, andresponse_cookiesprecedence is intentional. - Confirm dynamic headers and cookies are documented if clients depend on them.
- Confirm stream, SSE, redirect, and file responses have the intended media type and transport behavior.
- Confirm background tasks run only after the response body has been sent.
- Confirm OpenAPI output remains accurate after custom response-class or container changes.
- Confirm response documentation stays aligned with
litestar-openapimetadata and examples.
Cross-Skill Handoffs
- Use
litestar-exception-handlingfor error-envelope and failure-response design. - Use
litestar-openapiwhen response changes materially affect schema output. - Use
litestar-templatingfor deeper template-engine concerns. - Use
litestar-testingfor assertions on status, headers, cookies, streams, redirects, and SSE behavior.
Litestar References
More from alti3/litestar-skills
litestar-logging
Configure Litestar logging with `LoggingConfig`, `queue_listener`, exception logging policy, selective stack-trace suppression, standard logging, picologging, Structlog, and custom logging config subclasses. Use when establishing or refactoring application logging behavior, request-level logs, or production-safe error logging in Litestar. Do not use for metrics/tracing instrumentation or exception-response contract design.
22litestar-authentication
Implement Litestar authentication with custom authentication middleware, built-in security backends, JWT and session flows, route inclusion and exclusion rules, and typed auth context on `Request` / `ASGIConnection`. Use when establishing identity, issuing or validating credentials, or attaching authenticated user context in Litestar. Do not use for generic request parsing, broad security audits, or unrelated transport concerns.
22litestar-middleware
Design and apply Litestar middleware for cross-cutting concerns such as CORS, CSRF, allowed-host checks, compression, rate limiting, logging, sessions, request enrichment, policy enforcement, and custom ASGI pipeline control. Use when behavior must wrap broad route sets consistently across the ASGI stack. Do not use for route-specific business rules, simple response mutation better handled by lifecycle hooks, or auth/guard policy work that belongs in security-focused skills.
20litestar-routing
Design and implement Litestar routing with app/router/controller composition, handler decorators, path and parameter modeling, route indexing/reverse lookups, ASGI mounting, and layered route metadata. Use when creating or refactoring endpoint topology and URL contracts. Do not use for purely internal service logic unrelated to HTTP route structure.
18litestar-dto
Configure Litestar DTO behavior for inbound parsing and outbound serialization, including layer-scoped `dto`/`return_dto`, `DTOConfig` policies, `DTOData` update workflows, and custom `AbstractDTO` implementations. Use when API payload contracts differ from internal model structures. Do not use when internal models can be exposed safely without transformation.
18litestar-openapi
Configure Litestar OpenAPI schema generation and documentation UX with `OpenAPIConfig`, route-level schema metadata, schema generation controls, operation customization, UI render plugins, and documentation endpoint strategy. Use when API contract and docs quality must be implemented or corrected, especially for request, auth, and metrics surfaces. Do not use for runtime business logic changes unrelated to schema or documentation.
17