Pydantic Graph
Graph and finite state machine library.
This library is developed as part of Pydantic AI, however it has no dependency
on pydantic-ai or related packages and can be considered as a pure graph-based state machine library. You may find it useful whether or not you're using Pydantic AI or even building with GenAI.
As with Pydantic AI, this library prioritizes type safety and use of common Python syntax over esoteric, domain-specific use of Python syntax.
pydantic-graph allows you to define graphs using standard Python syntax. In particular, edges are defined using the return type hint of nodes.
Full documentation is available at ai.pydantic.dev/graph.
Here's a basic example:
from __future__ import annotations
from dataclasses import dataclass
from pydantic_graph import BaseNode, End, GraphBuilder, GraphRunContext, StepContext
@dataclass
class DivisibleBy5(BaseNode[None, None, int]):
foo: int
async def run(
self,
ctx: GraphRunContext,
) -> Increment | End[int]:
if self.foo % 5 == 0:
return End(self.foo)
else:
return Increment(self.foo)
@dataclass
class Increment(BaseNode):
foo: int
async def run(self, ctx: GraphRunContext) -> DivisibleBy5:
return DivisibleBy5(self.foo + 1)
g = GraphBuilder(input_type=int, output_type=int)
@g.step
async def start(ctx: StepContext[None, None, int]) -> DivisibleBy5:
return DivisibleBy5(ctx.inputs)
g.add(
g.node(DivisibleBy5),
g.node(Increment),
g.edge_from(g.start_node).to(start),
)
fives_graph = g.build()
async def main():
result = await fives_graph.run(inputs=4)
print(result)
#> 5
Release files for pydantic-graph 2.30.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 | |
|---|---|---|---|
| pydantic_graph-2.30.0.tar.gz | 45.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_graph-2.30.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 97.8 kB
Release files / pydantic_graph-2.30.0.tar.gz
| Download URL | pydantic_graph-2.30.0.tar.gz |
|---|---|
| Size | 45.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9bf43c1e2785598399b4679a2837173c0e3018c8167d86c2f99713685e4ba845
|
|
BLAKE2b-256 checksum How to use checksums |
c3b8557b040138ae5d5fdf8afec655e0e9cc0493ed674221dd301e0466245053
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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 14, 2026.
Transparency logRelease files / pydantic_graph-2.30.0-py3-none-any.whl
| Download URL | pydantic_graph-2.30.0-py3-none-any.whl |
|---|---|
| Size | 52.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8282c1091fc03f2d4f40e83ab7ca09e3c117df6b645bbd6cd433561a8064e361
|
|
BLAKE2b-256 checksum How to use checksums |
a2767afc039ff5e34a08c6acc862818e1a7ee389862c892fcfb49b4e4c0d1d50
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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 14, 2026.
Transparency log