Skip to main content

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)

  1. On PyPI (or TestPyPI), add a pending publisher or open your project → ManagePublishingAdd a new pending publisher / trusted publisher for GitLab.
  2. Set GitLab namespace and project to your path (for example group/pydantic-msgspec), workflow to GitLab CI/CD, and the correct GitLab hostname if not gitlab.com.
  3. Tag a release (v0.1.0) so the deploy job in .gitlab-ci.yml runs; 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

  1. Import the GitLab repository at readthedocs.org.
  2. Use the included .readthedocs.yaml; set the default branch and Python version as needed.
  3. After the first successful build, update PyPI project URLs if the docs URL differs from the placeholder in pyproject.toml.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_msgspec-0.1.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_msgspec-0.1.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

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

Hashes for pydantic_msgspec-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dc792c9de59216c61e128d0371610ea2aa7c68b84eb88ebe9fd0cc80c406e8d3
MD5 1f9d17dbaeef60ac7dcbe016724d2241
BLAKE2b-256 ccf0dcdb43db52b24f569dd2490030da126b45554da4fe2b8af0b3880a6cf784

See more details on using hashes here.

File details

Details for the file pydantic_msgspec-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_msgspec-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b46863495add2d56f2e1f7b4f6f769e6a3bc3aae4eeb2d7191d1f5acd76be1d4
MD5 b9690fdbe3802b1d2bf975e86bb32c3f
BLAKE2b-256 9a47af80ef12a78044e5d422c6b1f2b365230f9907786a7341bfaf0c00566f25

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page