Implementation:Spcl Graph of thoughts GraphOfOperations
| Knowledge Sources | |
|---|---|
| Domains | Graph_Reasoning, DAG_Construction |
| Last Updated | 2026-02-14 12:00 GMT |
Overview
Concrete tool for building directed acyclic graphs of thought operations provided by the graph-of-thoughts framework.
Description
The GraphOfOperations class is the data structure that represents the execution plan for a Graph of Thoughts reasoning pipeline. It maintains three lists:
operations-- All operations in the graph, in insertion order.roots-- Operations with no predecessors (entry points for execution).leaves-- Operations with no successors (exit points whose thoughts are the final results).
The class provides two methods for adding operations, each maintaining the roots and leaves lists automatically:
append_operation(operation)-- Connects the new operation as a successor of all current leaf operations. After appending, the new operation becomes the sole leaf (unless it was added to multiple predecessors, in which case those predecessors are no longer leaves). If the graph is empty, the operation becomes both a root and a leaf. This method is ideal for building linear chains and simple fan-out topologies.add_operation(operation)-- Adds the operation to the graph while respecting any explicit predecessor and successor links already configured on the operation object. It updates the roots and leaves lists based on the operation's actual connections. This method is required for constructing arbitrary DAG topologies, including fan-in patterns where multiple branches converge at an aggregation node.
Usage
Import GraphOfOperations when you need to define the reasoning plan for a GoT pipeline. Use append_operation for straightforward sequential or branching graphs. Use add_operation when you need fine-grained control over the DAG structure, particularly for aggregation and merging patterns.
Code Reference
| Source | graph_of_thoughts/operations/graph_of_operations.py
|
|---|---|
| Repository | https://github.com/spcl/graph-of-thoughts |
| Lines | L15-69 |
Signature
class GraphOfOperations:
def __init__(self) -> None:
self.operations: List[Operation] = []
self.roots: List[Operation] = []
self.leaves: List[Operation] = []
def append_operation(self, operation: Operation) -> None: ...
def add_operation(self, operation: Operation) -> None: ...
Import
from graph_of_thoughts.operations import GraphOfOperations
I/O Contract
Inputs
| Parameter | Type | Required | Description |
|---|---|---|---|
| operation | Operation | Yes | The operation to add to the graph (for both methods) |
Outputs
| Method | Return Type | Description |
|---|---|---|
| append_operation(operation) | None | Modifies the graph in-place: adds operation to operations list, updates roots and leaves |
| add_operation(operation) | None | Modifies the graph in-place: adds operation respecting explicit predecessor/successor links |
Usage Examples
Example 1: Linear Chain (IO/CoT)
Build a simple chain using append_operation. Each call extends from the current leaves.
from graph_of_thoughts.operations import GraphOfOperations, Generate
goo = GraphOfOperations()
goo.append_operation(Generate(1, 1)) # Root and leaf
goo.append_operation(Generate(1, 1)) # Successor of first Generate; new leaf
goo.append_operation(Generate(1, 1)) # Successor of second Generate; new leaf
# Result: Generate -> Generate -> Generate (linear chain)
# roots = [first Generate], leaves = [last Generate]
Example 2: Branching with Selection (ToT)
Generate multiple candidates, score them, and keep the best.
from graph_of_thoughts.operations import GraphOfOperations, Generate, Score, KeepBestN
goo = GraphOfOperations()
goo.append_operation(Generate(1, 5)) # Generate 5 candidates from 1 input
goo.append_operation(Score(my_scorer)) # Score all candidates
goo.append_operation(KeepBestN(1)) # Keep the single best
# Result: Generate(1->5) -> Score -> KeepBestN
Example 3: Arbitrary DAG with Aggregation (GoT)
Use add_operation with explicit predecessor links for fan-in topologies.
from graph_of_thoughts.operations import GraphOfOperations, Generate, Aggregate
goo = GraphOfOperations()
# Create independent branches
gen_branch1 = Generate(1, 1)
gen_branch2 = Generate(1, 1)
# Create aggregation node with explicit predecessors
agg = Aggregate(2)
agg.add_predecessor(gen_branch1)
agg.add_predecessor(gen_branch2)
gen_branch1.add_successor(agg)
gen_branch2.add_successor(agg)
# Add all operations using add_operation
goo.add_operation(gen_branch1)
goo.add_operation(gen_branch2)
goo.add_operation(agg)
# Result:
# [Generate1] --\
# --> [Aggregate]
# [Generate2] --/
# roots = [gen_branch1, gen_branch2], leaves = [agg]