How ev-horrosh/graphet works
Purpose and Entry Points
graphet is a Python 3.13+ workflow library layered over pydantic-graph. It supports explicit, typed async workflows defined either in Python or YAML, with local auditability and recovery. Primary entry points are the Python flow API in src/graphet/flow.py, YAML compilation in src/graphet/compiler/compiler.py, CLI commands in src/graphet/cli.py, and the optional HTTP service in src/graphet/serve.py.
Flow Definition Layer
Python-defined workflows use LLMFlow, node decorators, and explicit graph edges. Typed state and execution context are modeled separately, allowing step callables to operate on workflow state without owning orchestration concerns. Core definitions live in src/graphet/flow.py, src/graphet/node.py, and src/graphet/context.py.
YAML Specification and Compilation
YAML workflows are represented by Pydantic specification models, loaded from files, then compiled into the same executable graph model as Python flows. The compiler resolves named functions and state types through registries and validates references before graph construction. This boundary is implemented by src/graphet/specs/workflow.py, src/graphet/specs/loader.py, src/graphet/compiler/compiler.py, src/graphet/compiler/validators.py, src/graphet/registry/functions.py, and src/graphet/registry/states.py.
Graph Construction and Runtime
Graph construction ultimately delegates to pydantic-graph’s GraphBuilder; graphet supplies higher-level node semantics and drives execution through its engine. WorkflowEngine coordinates graph runs, dispatches runtime node behavior, emits events, and supports checkpoint-based resume. Key runtime paths are src/graphet/runtime/engine.py, src/graphet/runtime/events.py, and src/graphet/runtime/helpers.py.
Control-Flow Primitives
Beyond ordinary steps and explicit edges, the runtime implements routes, predefined repair/revision loops, human approval gates, concurrent spread/join, subflows, and orchestration-oriented behavior. Human gates pause execution for external edits and resume against persisted state; spreads fan out branches and join under the currently supported all_required policy. Implementations are in src/graphet/runtime/loops.py, src/graphet/runtime/human_gates.py, src/graphet/runtime/spreads.py, src/graphet/runtime/subflows.py, and src/graphet/runtime/orchestrator.py.
Persistence and Recovery Boundary
Persistence is local filesystem-backed rather than distributed or database-backed. A run directory records workflow/input/state/output documents, JSONL event and state-snapshot trails, checkpoints, branch artifacts, and human-gate artifacts. The engine uses checkpoints for crash recovery via resume. The abstraction and active implementation are src/graphet/persistence/base.py, src/graphet/persistence/file.py, and src/graphet/persistence/jsonl.py.
Operational Interfaces and Dependencies
The core runtime depends on Pydantic, PyYAML, and pydantic-graph; workflow steps are arbitrary async Python and may call LLM services but no LLM provider is required by the architecture. Optional interfaces add Click-based CLI support and FastAPI/Uvicorn serving. Test-oriented dry-run and snapshot execution is exposed through src/graphet/testing.py; package exports and public API assembly begin at src/graphet/__init__.py.
Main entry points: compiler.py Workflow Compiler · engine.py Workflow Engine · flow.py Python Flow API · __init__.py Package API
Components 19
Flow Definition
- Python Flow API
src/graphet/flow.py - Node Semantics
src/graphet/node.py - Execution Context · Separates orchestration context from workflow state.
src/graphet/context.py
YAML Compilation
- Workflow Spec
src/graphet/specs/workflow.py - YAML Loader
src/graphet/specs/loader.py - Workflow Compiler · Builds the executable graph shared with Python flows.
src/graphet/compiler/compiler.py - Spec Validators
src/graphet/compiler/validators.py - Function Registry
src/graphet/registry/functions.py - State Registry
src/graphet/registry/states.py
Runtime Control
- pydantic-graph Builder
- Workflow Engine · Coordinates runs, node dispatch, events, and resume.
src/graphet/runtime/engine.py - Runtime Events
src/graphet/runtime/events.py - Control Primitives · Implements repair loops, approval pauses, fan-out/join, and nested flows.
src/graphet/runtime/orchestrator.py
Persistence & Recovery
- Filesystem Persistence · Stores documents, JSONL trails, checkpoints, branches, and gate artifacts locally.
src/graphet/persistence/file.py - Persistence Contract
src/graphet/persistence/base.py
Public Interfaces
- Package API
src/graphet/__init__.py - CLI
src/graphet/cli.py - HTTP Service
src/graphet/serve.py - Testing API
src/graphet/testing.py
Connections 20
- Package API → Python Flow API: exports
- Package API → Workflow Compiler: exports
- Python Flow API → Node Semantics: defines steps
- Python Flow API → Execution Context: uses
- Python Flow API → pydantic-graph Builder: constructs
- YAML Loader → Workflow Spec: parses into
- Workflow Spec → Workflow Compiler: compiles
- Workflow Compiler → Spec Validators: validates
- Workflow Compiler → Function Registry: resolves functions
- Workflow Compiler → State Registry: resolves state types
- Workflow Compiler → pydantic-graph Builder: builds
- pydantic-graph Builder → Workflow Engine: executable graph
- Workflow Engine → Runtime Events: emits
- Workflow Engine → Control Primitives: dispatches
- Workflow Engine → Persistence Contract: persists and resumes
- Persistence Contract → Filesystem Persistence: implemented by
- CLI → Workflow Compiler: loads YAML flows
- CLI → Workflow Engine: runs
- HTTP Service → Workflow Engine: serves runs
- Testing API → Workflow Engine: dry-runs