Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Spcl Graph of thoughts Thought

From Leeroopedia
Knowledge Sources
Domains Graph_Reasoning, State_Management
Last Updated 2026-02-14 12:00 GMT

Overview

Concrete tool for encapsulating LLM thought state with scoring, validation, and ground truth metadata provided by the graph-of-thoughts framework.

Description

The Thought class is the fundamental data unit in the Graph of Thoughts framework. Every piece of reasoning state -- from initial problem input to final scored output -- is encapsulated in a Thought instance.

Key characteristics:

  • Auto-incrementing ID: Each Thought receives a unique integer ID from a class-level counter (_ids = itertools.count(0)). This enables tracing thoughts through the execution graph.
  • State dictionary: The state attribute holds problem-specific data as a Python dict. The schema is defined by the use case's Prompter and Parser.
  • Metadata properties: The class provides property-based access to scoring, validation, and ground truth evaluation results. Each property has a corresponding flag that tracks whether the operation has been performed:
    • score / scored -- Setting score automatically sets scored = True
    • valid / validated -- Setting valid automatically sets validated = True
    • solved / compared_to_ground_truth -- Setting solved automatically sets compared_to_ground_truth = True
  • Cloning via from_thought(): A static factory method that creates a new Thought as a copy of an existing one. The new thought gets its own unique ID but inherits the state dict and all metadata. This is used by operations like Score and KeepBestN that need to propagate thoughts forward.

Usage

Import the Thought class when implementing custom operations or when you need to inspect or manipulate thought state programmatically. In most use cases, Thoughts are created automatically by Generate operations and flow through the graph without direct manipulation. Direct construction is useful for testing and for setting up initial state.

Code Reference

Source graph_of_thoughts/operations/thought.py
Repository https://github.com/spcl/graph-of-thoughts
Lines L15-117

Signature

class Thought:
    _ids: Iterator[int] = itertools.count(0)

    def __init__(self, state: Optional[Dict] = None) -> None: ...

    @staticmethod
    def from_thought(thought: Thought) -> Thought: ...

    @property
    def valid(self) -> bool: ...
    @valid.setter
    def valid(self, valid: bool) -> None: ...

    @property
    def score(self) -> float: ...
    @score.setter
    def score(self, new_score: float) -> None: ...

    @property
    def solved(self) -> bool: ...
    @solved.setter
    def solved(self, solved: bool) -> None: ...

Import

from graph_of_thoughts.operations import Thought

I/O Contract

Inputs

Parameter Type Required Description
state Optional[Dict] No Problem-specific state dictionary; defaults to None (empty state)

Outputs

Attribute Type Description
id int Unique auto-incrementing identifier for this thought
state Dict Problem-specific state dictionary
score float Numerical score assigned by a Score operation
scored bool Whether this thought has been scored
valid bool Whether this thought passed validation
validated bool Whether this thought has been validated
solved bool Whether this thought matches ground truth
compared_to_ground_truth bool Whether this thought has been compared to ground truth

Usage Examples

Creating a Thought

from graph_of_thoughts.operations import Thought

# Create a thought with initial state
t = Thought(state={"original": "3 1 2", "current": "3 1 2", "phase": 0})
print(t.id)     # 0 (first thought created)
print(t.state)  # {"original": "3 1 2", "current": "3 1 2", "phase": 0}
print(t.scored) # False

Cloning a Thought

from graph_of_thoughts.operations import Thought

original = Thought(state={"current": "1 2 3"})
clone = Thought.from_thought(original)

print(clone.id)     # Different ID from original
print(clone.state)  # {"current": "1 2 3"} -- same state as original

Setting Metadata Properties

from graph_of_thoughts.operations import Thought

t = Thought(state={"current": "1 2 3"})

# Score the thought
t.score = 0.95
print(t.scored)  # True (automatically set)
print(t.score)   # 0.95

# Validate the thought
t.valid = True
print(t.validated)  # True (automatically set)
print(t.valid)      # True

# Compare to ground truth
t.solved = True
print(t.compared_to_ground_truth)  # True (automatically set)
print(t.solved)                     # True

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment