Skip to main content

Pydantic Graph

CI Coverage PyPI python versions license

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 pydantic.dev/docs/ai/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

Metadata

Release files for mpfaffenberger-pydantic-graph 2.43.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mpfaffenberger-pydantic-graph 2.43.0
File Size Uploaded
mpfaffenberger_pydantic_graph-2.43.0.tar.gz 45.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mpfaffenberger-pydantic-graph 2.43.0
File Interpreter ABI Platform
mpfaffenberger_pydantic_graph-2.43.0-py3-none-any.whl Python 3 none any Details

Total release size: 98.1 kB

Release files / mpfaffenberger_pydantic_graph-2.43.0.tar.gz

Download URL mpfaffenberger_pydantic_graph-2.43.0.tar.gz
Size 45.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8ed94662a864b0b68aedc7008ca3655bd06c3641f28872040d228e966d39218d
BLAKE2b-256 checksum
How to use checksums
a7d7b72a30872cbde41fd76fd9dd4b27732cb4d3da0ff6e55d7a6b2c1a2bc476
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / mpfaffenberger_pydantic_graph-2.43.0-py3-none-any.whl

Download URL mpfaffenberger_pydantic_graph-2.43.0-py3-none-any.whl
Size 52.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
96c517e435109963b583f44b1815dd1a1e123acb08192ed049ed04b7f3c1c19a
BLAKE2b-256 checksum
How to use checksums
b6e615d7401414240ab420406751c206f41f2d34347cb9424868febda8b5e438
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

2.43.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page