Generate msgspec Struct types from Pydantic v2 models (camelCase JSON, optional tag_field unions) and vice-versa.
Project description
pydantic-msgspec
Generate msgspec Struct types from Pydantic v2 BaseModel classes. Structs use rename="camel" for JSON keys. Models with a single-string Literal role or type field map to msgspec tag_field / tag for tagged unions.
Install
pip install pydantic-msgspec
# or
uv add pydantic-msgspec
Usage
from pydantic import BaseModel
import msgspec
from pydantic_msgspec import msgspec_from_pydantic, pydantic_from_msgspec
class Item(BaseModel):
name: str
count: int = 1
MSItem = msgspec_from_pydantic(Item)
raw = msgspec.json.encode(MSItem(name="x", count=2))
model = pydantic_from_msgspec(Item, MSItem(name="x", count=2))
assert model == Item(name="x", count=2)
Full API reference: Read the Docs.
Development
uv sync --all-extras # dev + docs
uv run pytest --cov=pydantic_msgspec
uv run sphinx-build -W -b html docs docs/_build
For maintainers
PyPI (trusted publishing with GitLab)
- On PyPI (or TestPyPI), add a pending publisher or open your project → Manage → Publishing → Add a new pending publisher / trusted publisher for GitLab.
- Set GitLab namespace and project to your path (for example
group/pydantic-msgspec), workflow to GitLab CI/CD, and the correct GitLab hostname if notgitlab.com. - Tag a release (
v0.1.0) so the deploy job in.gitlab-ci.ymlruns; the pipeline uses OIDC (id_tokens) and twine without long-lived API tokens.
Adjust Repository / Documentation URLs in pyproject.toml to match your real GitLab and Read the Docs project slug.
Read the Docs
- Import the GitLab repository at readthedocs.org.
- Use the included
.readthedocs.yaml; set the default branch and Python version as needed. - After the first successful build, update PyPI project URLs if the docs URL differs from the placeholder in
pyproject.toml.
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 pydantic_msgspec-0.1.1.tar.gz.
File metadata
- Download URL: pydantic_msgspec-0.1.1.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc792c9de59216c61e128d0371610ea2aa7c68b84eb88ebe9fd0cc80c406e8d3
|
|
| MD5 |
1f9d17dbaeef60ac7dcbe016724d2241
|
|
| BLAKE2b-256 |
ccf0dcdb43db52b24f569dd2490030da126b45554da4fe2b8af0b3880a6cf784
|
File details
Details for the file pydantic_msgspec-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pydantic_msgspec-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b46863495add2d56f2e1f7b4f6f769e6a3bc3aae4eeb2d7191d1f5acd76be1d4
|
|
| MD5 |
b9690fdbe3802b1d2bf975e86bb32c3f
|
|
| BLAKE2b-256 |
9a47af80ef12a78044e5d422c6b1f2b365230f9907786a7341bfaf0c00566f25
|