Skip to main content

DefSpec

Python Check PyPI

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

Why not ...

[!NOTE] There are also lots of other projects can generate the OpenAPI document or even validate the data. This project is not intended to replace them.

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)

Metadata

Release files for defspec 0.5.1

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.5.1
File Size Uploaded
defspec-0.5.1.tar.gz 12.9 kB Details

Built distribution (wheel)

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

Total release size: 24.9 kB

Release files / defspec-0.5.1.tar.gz

Download URL defspec-0.5.1.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
474f76ac93241ad536347fc240c477032e90f8c5baa072e93cbce45858f43a63
BLAKE2b-256 checksum
How to use checksums
fe9e35ca61132aa70641fc9d125268d8ece93c09bb78d7e5cce13120d321b5f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"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.5.1-py3-none-any.whl

Download URL defspec-0.5.1-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6812ed6cb5b9f66c3fef222ebd5c22e1a5cfebb74f32057e80b8db7766e16913
BLAKE2b-256 checksum
How to use checksums
2aea853257f4feb91ad44c7b991a8fc31a6c53c4fa23efc039aa8de16685ddfc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"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

This release

0.5.1 This release

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