Skip to main content

Graph and state machine library

Project description

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 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

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

pydantic_graph-2.18.0.tar.gz (44.0 kB view details)

Uploaded Source

Built Distribution

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

pydantic_graph-2.18.0-py3-none-any.whl (51.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_graph-2.18.0.tar.gz.

File metadata

  • Download URL: pydantic_graph-2.18.0.tar.gz
  • Upload date:
  • Size: 44.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pydantic_graph-2.18.0.tar.gz
Algorithm Hash digest
SHA256 9423defa047b477a561a06eadfd37aaf101a2b690d044adb79de80d7ea203e4e
MD5 ba033c8e0052cf7bbb72a458a74c2947
BLAKE2b-256 e5f91554e818e6d38bcb3f66ec7da7abe17c28dcd00caa1550b034e175b8841f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_graph-2.18.0.tar.gz:

Publisher: ci.yml on pydantic/pydantic-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pydantic_graph-2.18.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_graph-2.18.0-py3-none-any.whl
  • Upload date:
  • Size: 51.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pydantic_graph-2.18.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48df74e3ae12ca44f99890e9b4f7020ae70a5a38d20df02db7cb16b0db3c3711
MD5 ecd259bcff5df5f3c52504f68718c0e3
BLAKE2b-256 2f29fd05a4a84db9dae16c0417d8bcd6847148439a55329d000ef101d7243ded

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_graph-2.18.0-py3-none-any.whl:

Publisher: ci.yml on pydantic/pydantic-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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