pydantic-graph


Namepydantic-graph JSON
Version 0.4.7 PyPI version JSON
download
home_pageNone
SummaryGraph and state machine library
upload_time2025-07-24 21:47:55
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pydantic Graph

[![CI](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml/badge.svg?event=push)](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml?query=branch%3Amain)
[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/pydantic/pydantic-ai.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/pydantic/pydantic-ai)
[![PyPI](https://img.shields.io/pypi/v/pydantic-graph.svg)](https://pypi.python.org/pypi/pydantic-graph)
[![python versions](https://img.shields.io/pypi/pyversions/pydantic-graph.svg)](https://github.com/pydantic/pydantic-ai)
[![license](https://img.shields.io/github/license/pydantic/pydantic-ai.svg)](https://github.com/pydantic/pydantic-ai/blob/main/LICENSE)

Graph and finite state machine library.

This library is developed as part of [Pydantic AI](https://ai.pydantic.dev), 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](https://ai.pydantic.dev/graph).

Here's a basic example:

```python {noqa="I001" py="3.10"}
from __future__ import annotations

from dataclasses import dataclass

from pydantic_graph import BaseNode, End, Graph, GraphRunContext


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


fives_graph = Graph(nodes=[DivisibleBy5, Increment])
result = fives_graph.run_sync(DivisibleBy5(4))
print(result.output)
#> 5
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pydantic-graph",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Samuel Colvin <samuel@pydantic.dev>, Marcelo Trylesinski <marcelotryle@gmail.com>, David Montague <david@pydantic.dev>, Alex Hall <alex@pydantic.dev>, Douwe Maan <douwe@pydantic.dev>",
    "download_url": "https://files.pythonhosted.org/packages/6b/be/bd4324083e3b5dbd4b46a45f003e04c38eca421a0c1bbfbba43e1e56837f/pydantic_graph-0.4.7.tar.gz",
    "platform": null,
    "description": "# Pydantic Graph\n\n[![CI](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml/badge.svg?event=push)](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml?query=branch%3Amain)\n[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/pydantic/pydantic-ai.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/pydantic/pydantic-ai)\n[![PyPI](https://img.shields.io/pypi/v/pydantic-graph.svg)](https://pypi.python.org/pypi/pydantic-graph)\n[![python versions](https://img.shields.io/pypi/pyversions/pydantic-graph.svg)](https://github.com/pydantic/pydantic-ai)\n[![license](https://img.shields.io/github/license/pydantic/pydantic-ai.svg)](https://github.com/pydantic/pydantic-ai/blob/main/LICENSE)\n\nGraph and finite state machine library.\n\nThis library is developed as part of [Pydantic AI](https://ai.pydantic.dev), however it has no dependency\non `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.\n\nAs with Pydantic AI, this library prioritizes type safety and use of common Python syntax over esoteric, domain-specific use of Python syntax.\n\n`pydantic-graph` allows you to define graphs using standard Python syntax. In particular, edges are defined using the return type hint of nodes.\n\nFull documentation is available at [ai.pydantic.dev/graph](https://ai.pydantic.dev/graph).\n\nHere's a basic example:\n\n```python {noqa=\"I001\" py=\"3.10\"}\nfrom __future__ import annotations\n\nfrom dataclasses import dataclass\n\nfrom pydantic_graph import BaseNode, End, Graph, GraphRunContext\n\n\n@dataclass\nclass DivisibleBy5(BaseNode[None, None, int]):\n    foo: int\n\n    async def run(\n        self,\n        ctx: GraphRunContext,\n    ) -> Increment | End[int]:\n        if self.foo % 5 == 0:\n            return End(self.foo)\n        else:\n            return Increment(self.foo)\n\n\n@dataclass\nclass Increment(BaseNode):\n    foo: int\n\n    async def run(self, ctx: GraphRunContext) -> DivisibleBy5:\n        return DivisibleBy5(self.foo + 1)\n\n\nfives_graph = Graph(nodes=[DivisibleBy5, Increment])\nresult = fives_graph.run_sync(DivisibleBy5(4))\nprint(result.output)\n#> 5\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Graph and state machine library",
    "version": "0.4.7",
    "project_urls": {
        "Changelog": "https://github.com/pydantic/pydantic-ai/releases",
        "Documentation": "https://ai.pydantic.dev/graph",
        "Homepage": "https://ai.pydantic.dev/graph/tree/main/pydantic_graph",
        "Source": "https://github.com/pydantic/pydantic-ai"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "36a3f900c4b7a60d78354bcf948a7941df27f086f661e6ffaa77c80908d50452",
                "md5": "ddec2ee011dda15e838e7954041249fc",
                "sha256": "eb677d92e68222babb98fa99a08ead2611f3a2ec2f75262b403eb45617106dcb"
            },
            "downloads": -1,
            "filename": "pydantic_graph-0.4.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ddec2ee011dda15e838e7954041249fc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 27565,
            "upload_time": "2025-07-24T21:47:46",
            "upload_time_iso_8601": "2025-07-24T21:47:46.569451Z",
            "url": "https://files.pythonhosted.org/packages/36/a3/f900c4b7a60d78354bcf948a7941df27f086f661e6ffaa77c80908d50452/pydantic_graph-0.4.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "6bbebd4324083e3b5dbd4b46a45f003e04c38eca421a0c1bbfbba43e1e56837f",
                "md5": "f51dcf06d50dbb6601b68a5bde0418a4",
                "sha256": "5c4125efa68e922946581b050d00defcf2ffd4a5ff61a03a3aa298dfd49d1503"
            },
            "downloads": -1,
            "filename": "pydantic_graph-0.4.7.tar.gz",
            "has_sig": false,
            "md5_digest": "f51dcf06d50dbb6601b68a5bde0418a4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 21980,
            "upload_time": "2025-07-24T21:47:55",
            "upload_time_iso_8601": "2025-07-24T21:47:55.388680Z",
            "url": "https://files.pythonhosted.org/packages/6b/be/bd4324083e3b5dbd4b46a45f003e04c38eca421a0c1bbfbba43e1e56837f/pydantic_graph-0.4.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-24 21:47:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pydantic",
    "github_project": "pydantic-ai",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pydantic-graph"
}
        
Elapsed time: 0.81958s