msgspec-fastapi
An experimental FastAPI router that uses msgspec for JSON request bodies and responses.
This is a prototype. Its API may change, and it relies on private FastAPI internals, so it is pinned to FastAPI 0.142.2 to 0.143.x. It is a personal project, not part of msgspec; feedback is welcome in the issues.
Installation
pip install msgspec-fastapi
Usage
import msgspec
from fastapi import FastAPI
from msgspec_fastapi import MsgspecRouter, install_openapi
class City(msgspec.Struct):
name: str
population: int
router = MsgspecRouter()
@router.post('/cities')
async def create_city(city: City) -> City:
return city
app = FastAPI()
app.include_router(router)
install_openapi(app, router)
What it does
- A msgspec body parameter (a Struct or another type msgspec can decode) is decoded from
the raw request bytes by a
msgspec.json.Decoderbuilt once at route registration. A non-JSON content type returns 415, malformed JSON returns 400, and a missing or invalid body returns 422. Bodies are decoded in msgspec strict mode: for example, the JSON string"1"is not accepted for anintfield, unlike FastAPI with Pydantic. - A msgspec response type, from the return annotation or
response_model, is encoded with msgspec. An instance of exactly the declared Struct class is encoded without revalidation, as FastAPI does for a Pydanticresponse_model; other values are validated against the declared type. Nested values and type parameters of a trusted instance are not checked. - Responses of routes without a return annotation or
response_modelare also encoded with msgspec, so the output can differ from FastAPI'sjsonable_encoder(for example,bytesare base64-encoded andDecimalis encoded as a string). - Path, query, header and cookie parameters keep FastAPI extraction, aliases and
defaults, but are converted with
msgspec.convert(strict=False)instead of Pydantic, so coercion differs for some inputs (for example," 1.5"is rejected forfloatand"nan"is accepted forDecimal). Parameter types must be supported by msgspec; Pydantic-specific types such asAnyUrlraiseTypeErrorat route registration. install_openapiadds the msgspec JSON schemas to the OpenAPI document. It is required for a valid document: call it once after including the routers, passing everyMsgspecRouterthat is not included directly in anotherMsgspecRouter.- A Pydantic body or response model is left to FastAPI; the route parameters are still validated with msgspec.
Limitations
- Only one msgspec body parameter per route, and it cannot be combined with other
FastAPI body fields. Only JSON bodies are supported:
application/json,application/*+json, or a request without a content type. - Structs, alone or inside containers, are not supported as path, query, header or cookie parameters.
response_model_include,response_model_exclude,response_model_exclude_*andresponse_model_by_alias=Falseare rejected for typed msgspec responses.
Development
uv run --group test pytest
Metadata
Release files for msgspec-fastapi 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| msgspec_fastapi-0.0.1.tar.gz | 14.3 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| msgspec_fastapi-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.1 kB
Release files / msgspec_fastapi-0.0.1.tar.gz
| Download URL | msgspec_fastapi-0.0.1.tar.gz |
|---|---|
| Size | 14.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8996ec431a66ab07ff6605cc0ef4f6d31e94805bb0b45cb5dc0b9767c59b7505
|
|
BLAKE2b-256 checksum How to use checksums |
dd06ae3aa6379fec14b2c95e7773db40040ece07febe3ce89245453be872f67e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / msgspec_fastapi-0.0.1-py3-none-any.whl
| Download URL | msgspec_fastapi-0.0.1-py3-none-any.whl |
|---|---|
| Size | 12.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5b5d62475a5bcf83f5a0823f2b9b112e762085419774aa6de4667ea4dbdc5d12
|
|
BLAKE2b-256 checksum How to use checksums |
46d87e799ecd1b08dfbdbe37fd3a79f1359939f08540ad58596f893d6ea8f3fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logSecurity for msgspec-fastapi 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for msgspec_fastapi-0.0.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.