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 KeepValid Operation

From Leeroopedia
Knowledge Sources
Domains Graph_Reasoning, Thought_Operations
Last Updated 2026-02-14
Implements Principle:Spcl_Graph_of_thoughts_Validity_Filtering

Overview

Implementation of the validity filtering pattern that retains only valid thoughts from predecessors, passing through unvalidated thoughts as well.

Description

The KeepValid class is a concrete operation in the Graph of Thoughts framework that filters thoughts based on their validation status. It is implemented as a subclass of Operation with operation type OperationType.keep_valid.

The execution flow is:

  1. Assert at least one predecessor exists
  2. Retrieve all predecessor thoughts via get_previous_thoughts()
  3. Filter using the condition: keep thoughts where not thought.validated or thought.valid
  4. Clone each kept thought via Thought.from_thought()
  5. If any kept thought has validated == False, log a warning about unvalidated thoughts passing through
  6. Store the kept thoughts and log the count

This is a pure filtering operation that does not interact with the language model, Prompter, or Parser.

Usage

from graph_of_thoughts.operations import KeepValid

# Create a KeepValid operation (no parameters needed)
keep_valid = KeepValid()

# Wire into graph after a ValidateAndImprove operation
keep_valid.add_predecessor(validate_and_improve_op)

Code Reference

Source Location

  • File: graph_of_thoughts/operations/operations.py, Lines 712-773
  • Import: from graph_of_thoughts.operations import KeepValid

Class Signature

class KeepValid(Operation):
    operation_type: OperationType = OperationType.keep_valid

    def __init__(self) -> None:
        """
        Initializes a new KeepValid operation.
        """

Key Methods

  • __init__(self) -> None -- Initializes the operation with an empty thoughts list. Takes no configuration parameters.
  • get_thoughts(self) -> List[Thought] -- Returns the list of kept (valid or unvalidated) thoughts after execution.
  • _execute(self, lm, prompter, parser, **kwargs) -> None -- Core execution logic: filters predecessor thoughts by validation status and clones the kept thoughts.

Internal State

  • self.thoughts: List[Thought] -- Stores the filtered thoughts after execution.

I/O Contract

Input Output Side Effects
Predecessor thoughts from one or more predecessor operations. Each thought may or may not have been validated (i.e., validated may be True or False). Only valid or unvalidated thoughts (cloned) -- new Thought objects for each thought that passes the filter condition. Thoughts with validated == True and valid == False are discarded. No language model interaction. Logs a WARNING if unvalidated thoughts pass through. Logs kept thoughts at DEBUG level and count at INFO level.

Filtering logic:

self.thoughts = [
    Thought.from_thought(thought)
    for thought in self.get_previous_thoughts()
    if not thought.validated or thought.valid
]

Filter truth table:

validated valid Result
False (any) Kept (with warning)
True True Kept
True False Discarded

Assertions:

  • At least one predecessor must exist (len(self.predecessors) >= 1)

Usage Examples

After ValidateAndImprove: Filter Failed Thoughts

from graph_of_thoughts.operations import Generate, ValidateAndImprove, KeepValid

gen = Generate(num_branches_prompt=5, num_branches_response=1)

vai = ValidateAndImprove(
    improve=True,
    num_tries=3,
    validate_function=my_validation_fn,
)
vai.add_predecessor(gen)

# Only keep thoughts that passed validation (or were not validated)
keep_valid = KeepValid()
keep_valid.add_predecessor(vai)

Quality Gate in Pipeline

from graph_of_thoughts.operations import KeepValid, Aggregate

# Insert KeepValid before Aggregate to ensure only valid
# thoughts participate in the merge
keep_valid = KeepValid()
keep_valid.add_predecessor(branch_vai_op)

agg = Aggregate(num_responses=1)
agg.add_predecessor(keep_valid)

Related Pages

Page Connections

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