FastAPI plugin for automatic OpenAPI schema generation from msgspec structs
Project description
fastapi-msgspec-openapi
FastAPI plugin for automatic OpenAPI schema generation from msgspec structs. Enables Swagger UI documentation and TypeScript type generation.
Features
- ✨ Automatic OpenAPI schema generation from msgspec structs
- 📝 Swagger UI integration - See your msgspec types in
/docs - 🔷 TypeScript type generation support via
openapi-typescript - 🚀 Zero runtime overhead - Schema generation happens once at startup
- 🎯 Type-safe - Full type hints and mypy compatibility
- 🔧 Easy integration - Single line of code to enable
Installation
pip install fastapi-msgspec-openapi
Quick Start
from typing import Any
from fastapi import FastAPI
import msgspec
from fastapi_msgspec_openapi import MsgSpecPlugin
# Define your msgspec structs
class User(msgspec.Struct):
id: int
name: str
email: str
app = FastAPI()
# Inject the plugin
MsgSpecPlugin.inject(app)
@app.get("/user", response_model=Any)
async def get_user() -> User:
return msgspec.to_builtins(User(id=1, name="Alice", email="alice@example.com"))
Now visit /docs - your msgspec structs will appear in the Swagger UI! 🎉
Why Use This?
msgspec is one of the fastest Python serialization libraries, but FastAPI doesn't natively generate OpenAPI schemas for msgspec structs. This plugin bridges that gap.
Perfect Combo with fastapi-msgspec
This plugin works great alongside fastapi-msgspec for complete msgspec integration:
from fastapi import FastAPI
from fastapi_msgspec.responses import MsgSpecJSONResponse # Fast serialization
from fastapi_msgspec_openapi import MsgSpecPlugin # OpenAPI docs
app = FastAPI(default_response_class=MsgSpecJSONResponse)
MsgSpecPlugin.inject(app)
# Now you have both:
# ✅ Fast msgspec serialization (runtime performance)
# ✅ Full OpenAPI documentation (developer experience)
TypeScript Type Generation
Generate TypeScript types from your OpenAPI schema:
# Generate types
npx openapi-typescript http://localhost:8000/openapi.json -o api-types.ts
// Use in your frontend
import type { components } from './api-types';
type User = components['schemas']['User'];
const user: User = {
id: 1,
name: "Alice",
email: "alice@example.com"
};
Advanced Usage
Nested Structs
class Address(msgspec.Struct):
street: str
city: str
class User(msgspec.Struct):
id: int
name: str
address: Address # Nested struct
@app.get("/user", response_model=Any)
async def get_user() -> User:
return msgspec.to_builtins(
User(
id=1,
name="Alice",
address=Address(street="123 Main St", city="NYC")
)
)
Both User and Address schemas will be generated automatically!
Optional and Generic Types
from typing import Optional
class Response(msgspec.Struct):
user: Optional[User] = None
users: list[User] = []
@app.get("/response", response_model=Any)
async def get_response() -> Response:
return msgspec.to_builtins(Response(users=[...]))
The plugin handles Optional, list, and other generic types automatically.
How It Works
- Route scanning - Detects msgspec structs in route type hints
- Schema generation - Uses
msgspec.json.schema_components()for native OpenAPI schemas - Response updates - Patches FastAPI's OpenAPI schema to reference your structs
- Caching - Schema generation happens once, then cached
Requirements
- Python 3.10+
- FastAPI 0.100.0+
- msgspec 0.18.0+
Important Notes
- ⚠️ Struct types must be defined at module level (not inside functions) for proper type hint resolution
- ⚠️ Always use
response_model=Anyin route decorators when returning msgspec structs - ⚠️ Use
msgspec.to_builtins()to serialize structs before returning
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE file for details
Credits
Created by RamsesDev
Inspired by:
- msgspec by Jim Crist-Harif
- FastAPI by Sebastián Ramírez
- fastapi-msgspec by Iurii Skorniakov
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_msgspec_openapi-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_msgspec_openapi-0.1.0.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5c36b49bc9493323c4cf48734304fe9d0db735fc32cde4b1c9bcd885a7d4d06
|
|
| MD5 |
42523ecb9fd1db2db48657186e0e4f3b
|
|
| BLAKE2b-256 |
b887033bca9b88296e3453004f5ad93180224008458fc3943fc0bdf68a7953d7
|
Provenance
The following attestation bundles were made for fastapi_msgspec_openapi-0.1.0.tar.gz:
Publisher:
publish.yml on S3wnkin/fastapi-msgspec-openapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_msgspec_openapi-0.1.0.tar.gz -
Subject digest:
d5c36b49bc9493323c4cf48734304fe9d0db735fc32cde4b1c9bcd885a7d4d06 - Sigstore transparency entry: 657962528
- Sigstore integration time:
-
Permalink:
S3wnkin/fastapi-msgspec-openapi@a82180afef4505d48179946780bb1055fc7c755f -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/S3wnkin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a82180afef4505d48179946780bb1055fc7c755f -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastapi_msgspec_openapi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_msgspec_openapi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5299b2a0080ab81be47a30e6ce604fbc82eb35e6cc5855b45a30f951134e4aa
|
|
| MD5 |
690da7de21aaf760e99e869e37c4c7a0
|
|
| BLAKE2b-256 |
8eb6f9aa836731e32b6c687d4783c0ddc4c5654a3627a7db6d23a3d31a6b657d
|
Provenance
The following attestation bundles were made for fastapi_msgspec_openapi-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on S3wnkin/fastapi-msgspec-openapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_msgspec_openapi-0.1.0-py3-none-any.whl -
Subject digest:
f5299b2a0080ab81be47a30e6ce604fbc82eb35e6cc5855b45a30f951134e4aa - Sigstore transparency entry: 657962542
- Sigstore integration time:
-
Permalink:
S3wnkin/fastapi-msgspec-openapi@a82180afef4505d48179946780bb1055fc7c755f -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/S3wnkin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a82180afef4505d48179946780bb1055fc7c755f -
Trigger Event:
release
-
Statement type: