# ev-horrosh/graphet architecture

> Architecture diagram of the GitHub repository ev-horrosh/graphet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ev-horrosh/graphet
- Repository: https://github.com/ev-horrosh/graphet

## Overview

#### 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: [`src/graphet/compiler/compiler.py`](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/compiler/compiler.py) (Workflow Compiler), [`src/graphet/runtime/engine.py`](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/runtime/engine.py) (Workflow Engine), [`src/graphet/flow.py`](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/flow.py) (Python Flow API), [`src/graphet/__init__.py`](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/__init__.py) (Package API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_definition["Flow Definition"]
  node_python_flow["Python Flow API<br/>flow definitions<br/>[flow.py]"]
  node_node_semantics["Node Semantics<br/>decorators and nodes<br/>[node.py]"]
  node_execution_context["Execution Context<br/>typed context<br/>[context.py]"]
end

subgraph group_yaml["YAML Compilation"]
  node_workflow_spec["Workflow Spec<br/>Pydantic models<br/>[workflow.py]"]
  node_yaml_loader["YAML Loader<br/>spec loading<br/>[loader.py]"]
  node_compiler["Workflow Compiler<br/>graph compiler<br/>[compiler.py]"]
  node_validators["Spec Validators<br/>reference validation<br/>[validators.py]"]
  node_function_registry["Function Registry<br/>named function lookup<br/>[functions.py]"]
  node_state_registry["State Registry<br/>state type lookup<br/>[states.py]"]
end

subgraph group_runtime["Runtime Control"]
  node_graph_builder{{"pydantic-graph Builder<br/>external graph builder"}}
  node_workflow_engine["Workflow Engine<br/>async execution engine<br/>[engine.py]"]
  node_runtime_events["Runtime Events<br/>event emission<br/>[events.py]"]
  node_control_primitives["Control Primitives<br/>loops, gates, spreads, subflows<br/>[orchestrator.py]"]
end

subgraph group_persistence["Persistence &amp; Recovery"]
  node_file_persistence[("Filesystem Persistence<br/>run artifact store<br/>[file.py]")]
  node_persistence_contract["Persistence Contract<br/>persistence abstraction<br/>[base.py]"]
end

subgraph group_interfaces["Public Interfaces"]
  node_public_api["Package API<br/>Python package<br/>[__init__.py]"]
  node_cli["CLI<br/>[cli.py]"]
  node_http_service["HTTP Service<br/>FastAPI service<br/>[serve.py]"]
  node_testing["Testing API<br/>dry-run and snapshots<br/>[testing.py]"]
end

node_public_api -->|"exports"| node_python_flow
node_public_api -->|"exports"| node_compiler
node_python_flow -->|"defines steps"| node_node_semantics
node_python_flow -->|"uses"| node_execution_context
node_python_flow -->|"constructs"| node_graph_builder
node_yaml_loader -->|"parses into"| node_workflow_spec
node_workflow_spec -->|"compiles"| node_compiler
node_compiler -->|"validates"| node_validators
node_compiler -->|"resolves functions"| node_function_registry
node_compiler -->|"resolves state types"| node_state_registry
node_compiler -->|"builds"| node_graph_builder
node_graph_builder -->|"executable graph"| node_workflow_engine
node_workflow_engine -->|"emits"| node_runtime_events
node_workflow_engine -->|"dispatches"| node_control_primitives
node_workflow_engine -->|"persists and resumes"| node_persistence_contract
node_persistence_contract -->|"implemented by"| node_file_persistence
node_cli -->|"loads YAML flows"| node_compiler
node_cli -->|"runs"| node_workflow_engine
node_http_service -->|"serves runs"| node_workflow_engine
node_testing -->|"dry-runs"| node_workflow_engine

click node_public_api "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/__init__.py"
click node_python_flow "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/flow.py"
click node_node_semantics "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/node.py"
click node_execution_context "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/context.py"
click node_workflow_spec "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/specs/workflow.py"
click node_yaml_loader "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/specs/loader.py"
click node_compiler "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/compiler/compiler.py"
click node_validators "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/compiler/validators.py"
click node_function_registry "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/registry/functions.py"
click node_state_registry "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/registry/states.py"
click node_workflow_engine "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/runtime/engine.py"
click node_runtime_events "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/runtime/events.py"
click node_control_primitives "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/runtime/orchestrator.py"
click node_file_persistence "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/persistence/file.py"
click node_persistence_contract "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/persistence/base.py"
click node_cli "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/cli.py"
click node_http_service "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/serve.py"
click node_testing "https://github.com/ev-horrosh/graphet/blob/main/src/graphet/testing.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_python_flow,node_node_semantics,node_execution_context toneBlue
class node_workflow_spec,node_yaml_loader,node_compiler,node_validators,node_function_registry,node_state_registry toneAmber
class node_graph_builder,node_workflow_engine,node_runtime_events,node_control_primitives toneMint
class node_file_persistence,node_persistence_contract toneRose
class node_public_api,node_cli,node_http_service,node_testing toneIndigo
```

## Components

### Flow Definition

- [Python Flow API](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/flow.py): flow definitions · `src/graphet/flow.py`
- [Node Semantics](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/node.py): decorators and nodes · `src/graphet/node.py`
- [Execution Context](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/context.py): typed context · Separates orchestration context from workflow state. · `src/graphet/context.py`

### YAML Compilation

- [Workflow Spec](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/specs/workflow.py): Pydantic models · `src/graphet/specs/workflow.py`
- [YAML Loader](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/specs/loader.py): spec loading · `src/graphet/specs/loader.py`
- [Workflow Compiler](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/compiler/compiler.py): graph compiler · Builds the executable graph shared with Python flows. · `src/graphet/compiler/compiler.py`
- [Spec Validators](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/compiler/validators.py): reference validation · `src/graphet/compiler/validators.py`
- [Function Registry](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/registry/functions.py): named function lookup · `src/graphet/registry/functions.py`
- [State Registry](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/registry/states.py): state type lookup · `src/graphet/registry/states.py`

### Runtime Control

- **pydantic-graph Builder**: external graph builder
- [Workflow Engine](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/runtime/engine.py): async execution engine · Coordinates runs, node dispatch, events, and resume. · `src/graphet/runtime/engine.py`
- [Runtime Events](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/runtime/events.py): event emission · `src/graphet/runtime/events.py`
- [Control Primitives](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/runtime/orchestrator.py): loops, gates, spreads, subflows · Implements repair loops, approval pauses, fan-out/join, and nested flows. · `src/graphet/runtime/orchestrator.py`

### Persistence & Recovery

- [Filesystem Persistence](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/persistence/file.py): run artifact store · Stores documents, JSONL trails, checkpoints, branches, and gate artifacts locally. · `src/graphet/persistence/file.py`
- [Persistence Contract](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/persistence/base.py): persistence abstraction · `src/graphet/persistence/base.py`

### Public Interfaces

- [Package API](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/__init__.py): Python package · `src/graphet/__init__.py`
- [CLI](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/cli.py): Click commands · `src/graphet/cli.py`
- [HTTP Service](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/serve.py): FastAPI service · `src/graphet/serve.py`
- [Testing API](https://github.com/ev-horrosh/graphet/blob/main/src/graphet/testing.py): dry-run and snapshots · `src/graphet/testing.py`

## Connections

- 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
