Skip to main content

A lightweight graph-based state machine.

Project description

justgraph

A lightweight graph-based state machine inspired by LangGraph.

from dataclasses import dataclass
from justgraph import State, FieldUpdate, Step, Graph
from justgraph.reducers import Extend, Increment


@dataclass
class ChatState(State):
    messages: list[str]
    counter: int


graph = Graph([ChatState])

@graph.node("greet")
def greet(state: ChatState) -> list[Step]:
    return [Step("log", [
        FieldUpdate(ChatState, "messages", Extend(["hello"])),
        FieldUpdate(ChatState, "counter", Increment(1)),
    ])]

@graph.node("log")
def log(state: ChatState) -> list[Step]:
    print(state.messages)
    return []

graph.set_entry_point("greet")
app = graph.compile()
app.invoke([ChatState(messages=[], counter=0)])
# ['hello']

Features

  • Nodes — functions that receive state and return list[Step]
  • Step(target, updates) — encapsulate routing and data mutations together
  • No edges — all routing is implicit in return values (no add_edge())
  • Parallel fan-out — return multiple Steps and branches run concurrently
  • N=1 optimization — single Step with no updates reuses state directly (no copy)
  • Depth limit — configurable max_depth prevents infinite loops in cyclic graphs
  • ReducersExtend, Increment, Assign, or custom
  • Multiple states — nodes can depend on different state types

Custom Reducers

Subclass Reducer[T] and implement apply(old: T) -> T:

from justgraph import Reducer, FieldUpdate

class Multiply(Reducer[int]):
    def __init__(self, factor: int):
        self._factor = factor
    def apply(self, old: int) -> int:
        return old * self._factor

# Use it like any built-in reducer
FieldUpdate(ChatState, "counter", Multiply(3))

Examples

uv run examples/chat.py
uv run examples/conditional.py

Tests

uv run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

justgraph-0.2.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

justgraph-0.2.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file justgraph-0.2.0.tar.gz.

File metadata

  • Download URL: justgraph-0.2.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for justgraph-0.2.0.tar.gz
Algorithm Hash digest
SHA256 51e70997ef432c3f05532323cd31f72c013319f4d88f7022ccbc4a0a84cb1830
MD5 2acec53667dddacbfa66fe7275955d6b
BLAKE2b-256 8a06523e38e7d3845cb3ee50ec3dc061588beb33fdabf06e0d5230908ac3f505

See more details on using hashes here.

File details

Details for the file justgraph-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: justgraph-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for justgraph-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 07c2a2010bb04e69b0db647bf339487a42ed12ab1a8564c12ab029a9b1e17993
MD5 8d7e48de43777bf60e9a3f6b3da4809e
BLAKE2b-256 590b5f2df711f68ca8923807a0bb2481d7c1009c088b35dcbb2977ccbeecb98f

See more details on using hashes here.

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