Skip to main content

DefSpec

Python Check PyPI

Create the OpenAPI spec and document from dataclass, attrs, msgspec, etc.

Why not ...

This project is a legacy of a private initiative. During the development, I discovered that using msgspec could elegantly define and generate the API schema. The OpenAPI component can be utilized to generate the API documentation for various projects. As a result, I made the decision to extract it and transform it into a public project.

You can use this project as a low-level component or a drop-in module when you don't want to introduce too many other dependencies.

Installation

pip install defspec
# to enable the offline feature
pip install defspec[offline]

Examples

You can run the above examples and open the OpenAPI document in your browser:

Usage

from dataclasses import dataclass
from typing import List

from defspec import OpenAPI, OpenAPIComponent, SecuritySchemeHTTP


@dataclass
class User:
    name: str
    age: int


openapi = OpenAPI(
    components=OpenAPIComponent(
        security_schemes={"token": SecuritySchemeHTTP(scheme="bearer")}
    ),
    security=[{"token": []}],
)
openapi.register_route("/", method="get", summary="Hello World")
openapi.register_route(
    "/users", method="post", summary="Get all the user info", response_type=List[User]
)

# get the OpenAPI spec
print(openapi.to_dict())
# get the OpenAPI spec bytes
with open("openapi.json", "wb") as f:
    f.write(openapi.to_json())

# serve as a HTTP server
openapi.serve_as_http_daemon(port=8000, run_in_background=True)

OpenAPI compatibility

DefSpec supports OpenAPI 3.1.x (3.1.0 by default).

  • Models use components.schemas and #/components/schemas/... references. When migrating, update saved $defs references. The defs= constructor argument is removed; openapi.defs remains an alias for openapi.components.schemas.
  • Query, header, cookie, and path models expand into one parameter per encoded field name; generated msgspec tags are omitted. Wrap unions such as int | str in a model. Named scalars, such as int or UUID, use the type name as the parameter name. Defaults make non-path fields optional unless the schema requires them. Path fields must match the URL placeholders and cannot have defaults or default factories. TypedDict path keys must be required. Each placeholder must have a unique name.
  • Field constraints and descriptions are preserved; parameter schemas are copied independently. Model titles, descriptions, and forbid_unknown_fields are ignored during expansion. Constraints that cannot be expanded raise ValueError, including additionalProperties: false annotations that forbid declared fields.
  • Header names Authorization, Accept, and Content-Type are rejected regardless of case. Use security schemes and request/response content types instead.
  • request_type=None omits the body; response_type=None omits response content. Use type(None) for JSON null. Routes document a 200 response by default.
  • Omit security to inherit global security; use security=[] for public routes. OAuth flows omit absent fields and keep the required scopes map.
  • Default operation IDs include a stable path hash: _users_<hash>_get for GET /users. Set operation_id to choose your own name.
  • Invalid routes, equivalent path templates, duplicate operation IDs, and empty or conflicting schema names raise ValueError without changing the document. HTTP methods are case-insensitive. Use distinct model names and operation_id values to resolve conflicts.

Custom schema hooks follow msgspec's rules: return a schema dictionary or True for an unconstrained schema. The tests validate generated documents against OpenAPI 3.1. Validate custom schemas and direct edits before use.

Metadata

Release files for defspec 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for defspec 0.6.0
File Size Uploaded
defspec-0.6.0.tar.gz 34.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for defspec 0.6.0
File Interpreter ABI Platform
defspec-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 53.3 kB

Release files / defspec-0.6.0.tar.gz

Download URL defspec-0.6.0.tar.gz
Size 34.0 kB
Tags Source
SHA-256 checksum
How to use checksums
99789c4766a2f8ca5da87ac198dfe4087409fb18ad2cea2699f4013ad2351e0a
BLAKE2b-256 checksum
How to use checksums
03fcc631a65a8f71bb4f394c3fa91833e5f5a3d19ad21f4ff01c93830864e5d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / defspec-0.6.0-py3-none-any.whl

Download URL defspec-0.6.0-py3-none-any.whl
Size 19.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35fe8e61ee1ce126206a0ee716d52020a216dc6ab0fac74538204c6080059754
BLAKE2b-256 checksum
How to use checksums
f0d623011c0ca9e99366d09d3ea4985a5018cece93a156213eb07f3112546cb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

0.7.1

2 release files

0.7.0

2 release files

This release

0.6.0 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page