Pydantic JSON Patch
Pydantic models for implementing JSON Patch.
Installation
Pydantic JSON Patch is published to PyPI, and can be installed with e.g.:
pip install pydantic-json-patch
Models
A model is provided for each of the six JSON Patch operations:
AddOpCopyOpMoveOpRemoveOpReplaceOpTestOp
As repeating the op is a bit awkward (CopyOp(op="copy", ...)), a create factory method is available:
>>> from pydantic_json_patch import AddOp
>>> op = AddOp.create(path="/foo/bar", value=123)
>>> op
AddOp(op='add', path='/foo/bar', value=123)
>>> op.model_dump_json()
'{"op":"add","path":"/foo/bar","value":123}'
The operations that take a value (AddOp, ReplaceOp, and TestOp) are generic, so you can parameterize them with a specific value type:
>>> from pydantic_json_patch import ReplaceOp
>>> op = ReplaceOp[str].create(path="/foo/bar", value="hello")
>>> op
ReplaceOp[str](op='replace', path='/foo/bar', value='hello')
Additionally, there are two compound types:
Operationis the union of all the operations; andJsonPatchis a PydanticRootModelrepresenting a sequence of operations.
JsonPatch can be used directly for validation:
>>> from pydantic_json_patch import JsonPatch
>>> patch = JsonPatch.model_validate_json('[{"op":"add","path":"/a/b/c","value":"foo"}]')
>>> patch[0]
AddOp(op='add', path='/a/b/c', value='foo')
Pointer tokens
The path property (and from property, where present) of an operation is a JSON Pointer.
This means that any ~ or / characters in property names need to be properly encoded.
To aid working with these, the models expose a read-only path_tokens property (and, where appropriate, from_tokens):
>>> from pydantic_json_patch import CopyOp
>>> op = CopyOp.model_validate_json('{"op":"copy","path":"/foo/bar~1new","from":"/foo/bar~0old"}')
>>> op
CopyOp(op='copy', path='/foo/bar~1new', from_='/foo/bar~0old')
>>> op.path_tokens
('foo', 'bar/new')
>>> op.from_tokens
('foo', 'bar~old')
Similarly, the create factory methods can accept sequences of tokens, and will encode them appropriately:
>>> from pydantic_json_patch import TestOp
>>> op = TestOp.create(path=("annotations", "scope/value"), value=None)
>>> op
TestOp(op='test', path='/annotations/scope~1value', value=None)
>>> op.model_dump_json()
'{"op":"test","path":"/annotations/scope~1value","value":null}'
FastAPI
You can use this package to validate a JSON Patch endpoint in a FastAPI application, for example:
import typing as tp
from uuid import UUID
from fastapi import Body, FastAPI
from pydantic_json_patch import JsonPatch
app = FastAPI()
@app.patch("/resource/{resource_id}")
def _(resource_id: UUID, operations: tp.Annotated[JsonPatch, Body()]) -> ...: ...
This will provide a sensible example of the request body:
and list the models along with the other schemas:
Value type validation
You can also use a more specific type to apply type validation to the value properties:
import typing as tp
from uuid import UUID
from fastapi import Body, FastAPI
from pydantic import Discriminator
from pydantic_json_patch import AddOp, TestOp
app = FastAPI()
@app.patch("/resource/{resource_id}")
def _(
resource_id: UUID,
operations: tp.Annotated[
list[tp.Annotated[AddOp[int] | TestOp[int], Discriminator("op")]], Body()
],
) -> ...: ...
Notes:
- Explicitly specifying the discriminator gives better results on failed validation for unions of operations; and
- Parameterised versions of the operations will also appear in the JSON Schema as e.g.
AddOp_int_(with the title "JsonPatchAddOperation[int]").
Development
This project uses uv for managing dependencies. Having installed uv, you can set the project up for local development with:
uv sync
uv run pre-commit install
The pre-commit hooks will ensure that the code style checks (using isort and ruff) are applied.
Testing
The test suite uses pytest and can be run with:
uv run pytest
Additionally, there is ty type-checking that can be run with:
uv run ty check
FastAPI
You can preview the FastAPI/Swagger documentation by running:
uv run fastapi dev tests/app.py
and visiting the Documentation link that's logged in the console. This will auto-restart as you make changes.
Metadata
Release files for pydantic-json-patch 2.2.0
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 | |
|---|---|---|---|---|
| pydantic_json_patch-2.2.0.tar.gz | 6.6 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_json_patch-2.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.1 kB
Release files / pydantic_json_patch-2.2.0.tar.gz
| Download URL | pydantic_json_patch-2.2.0.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f942f0fa4fdd6e3b25b9564b5ec236a93a6929ae5e7336c239828e5392344476
|
|
BLAKE2b-256 checksum How to use checksums |
14d95e22540fa33827ce72c4efc4505c0aff5dc9c17f4064a8521098bbeac7b7
|
| 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 Aug 31, 2026.
Transparency logRelease files / pydantic_json_patch-2.2.0-py3-none-any.whl
| Download URL | pydantic_json_patch-2.2.0-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d35b25ff5dbb3051bf6568c1bbed14cd56afffdd1f892a9f8e143e11a810ebce
|
|
BLAKE2b-256 checksum How to use checksums |
6ead60e01a700740e961f8124fe7686236632dbc7531ed9399eccbcc7c9d7ec2
|
| 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 Aug 31, 2026.
Transparency logSecurity for pydantic-json-patch 2.2.0
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 pydantic_json_patch-2.2.0.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 Aug 31, 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.