Skip to content

Conditions

Classes:

  • Expression

    Abstract base class for defining deferred logical expressions. It captures the association of logic with Facts so

  • Condition

    A Condition is a container to defer logical expressions against a supplied Fact. The expression can be inverted

  • Operator

    Represents the logical operation of a CompoundCondition

  • CompoundCondition

    Represents a compound logical condition composed of two sub-conditions, an operator, and an negation flag. This

  • MissingFactError

    Raised when and AI condition has no declared facts for context.

  • AIDecisionError

    Raised when an AI detrmines an error with the inquiry during evaluation.

  • DeferredFormatter

    A specialized string formatter that defers the evaluation of Similarity objects during field resolution.

  • LiteralFormatter

    A formatter that does not inspect attributes of the object being formatted.

  • AICondition
  • OnFactChanged

    A condition that always returns True. It is used to trigger rules when a Fact is updated. It is useful for rules

Functions:

  • condition

    Creates a Condition object from a lambda or function. It performs limited static analysis of the code to ensure

Expression dataclass

Bases: DeclaresFacts

Abstract base class for defining deferred logical expressions. It captures the association of logic with Facts so that upon a Fact update, the logical expression can be selectively evaluated. It also provides a set of logical operators for combining conditions, resulting in a new CompoundCondition.

Methods:

  • last_result

    Returns the last evaluated result of the expression. Could return none if a Fact value is None.

  • evaluated

    Returns True if the expression has been evaluated at least once.

last_result() -> bool | None

Returns the last evaluated result of the expression. Could return none if a Fact value is None.

evaluated() -> bool

Returns True if the expression has been evaluated at least once.

Condition dataclass

Bases: FactHandler[ConditionCallable, bool], Expression

A Condition is a container to defer logical expressions against a supplied Fact. The expression can be inverted using the ~ operator. Conditions also support the '&', '|', and '^' operators for combinatorial logic.

Attributes:

  • facts (tuple[str, ...]) –

    A tuple of strings representing the facts/attributes this condition depends upon. Each string should be in the format "ClassName.attribute" without nesting.

  • func (Callable[..., bool]) –

    A callable that implements the actual condition logic. It should return a boolean value indicating whether the condition is satisfied.

  • is_inverted (bool) –

    Flag indicating whether the condition result should be inverted.

Methods:

  • last_result

    Returns the last evaluated result of the expression. Could return none if a Fact value is None.

  • evaluated

    Returns True if the expression has been evaluated at least once.

last_result() -> bool | None

Returns the last evaluated result of the expression. Could return none if a Fact value is None.

evaluated() -> bool

Returns True if the expression has been evaluated at least once.

Operator

Bases: Enum

Represents the logical operation of a CompoundCondition

CompoundCondition dataclass

Bases: Expression

Represents a compound logical condition composed of two sub-conditions, an operator, and an negation flag. This class allows for the deferred evaluation of complex logical expressions by combining simpler conditions using logical operators such as &, |, and ^.

CompoundConditions are chain evaluated from left to right. For example, a | b | c is equivalent to: (a | b) | c but ordering can be overriden with parenthesis: a | (b | c) which is equivalent to: (a) | (b | c).

This clas should not be used directly in favor of the logical operators.

Methods:

  • last_result

    Returns the last evaluated result of the expression. Could return none if a Fact value is None.

  • evaluated

    Returns True if the expression has been evaluated at least once.

last_result() -> bool | None

Returns the last evaluated result of the expression. Could return none if a Fact value is None.

evaluated() -> bool

Returns True if the expression has been evaluated at least once.

MissingFactError

Bases: Exception

Raised when and AI condition has no declared facts for context.

AIDecisionError

Bases: Exception

Raised when an AI detrmines an error with the inquiry during evaluation.

DeferredFormatter

Bases: Formatter

A specialized string formatter that defers the evaluation of Similarity objects during field resolution.

This implementation enables AI RAG use-cases by detecting Similarity objects during field replacement and deferring their evaluation. Instead of immediately resolving vector similarity searches, it captures them for later processing with the non-Similarity objects replaced to provide vector searches with more context for RAG operations.

Attributes:

  • found_lookups (dict[str, Similarity]) –

    Registry of Similarity objects found during field resolution, mapped by their field names for deferred evaluation.

Methods:

  • get_field

    Resolves field references with special handling for Similarity objects.

get_field(field_name, args, kwargs) -> tuple[str, str]

Resolves field references with special handling for Similarity objects.

Traverses dotted field names to resolve values. When a Similarity object is encountered, it defers evaluation by recording the lookup and returning a placeholder.

Parameters:

  • field_name (str) –

    Field name to resolve (e.g., 'user.name')

  • args (tuple) –

    Positional arguments for the formatter

  • kwargs (dict) –

    Keyword arguments for the formatter

Returns:

  • tuple[str, str]

    tuple[Any, str]: (resolved_value_or_placeholder, root_field_name)

LiteralFormatter

Bases: Formatter

A formatter that does not inspect attributes of the object being formatted.

AICondition dataclass

Bases: Condition

Methods:

  • last_rationale

    Get the last AI decision rationale.

  • last_result

    Returns the last evaluated result of the expression. Could return none if a Fact value is None.

  • evaluated

    Returns True if the expression has been evaluated at least once.

last_rationale() -> str | None

Get the last AI decision rationale.

last_result() -> bool | None

Returns the last evaluated result of the expression. Could return none if a Fact value is None.

evaluated() -> bool

Returns True if the expression has been evaluated at least once.

OnFactChanged dataclass

Bases: Condition

A condition that always returns True. It is used to trigger rules when a Fact is updated. It is useful for rules that need to simply update a Fact when another fact is updated.

Methods:

  • last_result

    Returns the last evaluated result of the expression. Could return none if a Fact value is None.

  • evaluated

    Returns True if the expression has been evaluated at least once.

last_result() -> bool | None

Returns the last evaluated result of the expression. Could return none if a Fact value is None.

evaluated() -> bool

Returns True if the expression has been evaluated at least once.

condition(func: ConditionCallable | str, retries: int = 3, model: BaseChatModel | None = None) -> Condition

Creates a Condition object from a lambda or function. It performs limited static analysis of the code to ensure proper usage and discover the facts/attributes accessed by the condition. This allows the rule engine to track dependencies between conditions and facts with minimal boilerplate code.

Lambda usage requires Fact access via accessing static class attributes (e.g., User.age). Whereas functions are not allowed to access class attributes statically, and must only access attributes via parameter instances. Neither lambdas or functions are allowed to access instances outside of their scope.

Parameters:

  • func (Callable[..., bool]) –

    A lambda or function that returns a boolean value. For regular functions, parameters must be properly type-hinted with Fact subclasses. For lambdas, no parameters are allowed.

Returns:

  • Condition ( Condition ) –

    A Condition object containing: - facts: A tuple of fact identifiers in the form "FactClass.attribute" - func: The transformed callable that will evaluate the condition

Raises:

Example

Will be transformed to accept instances of User:

is_user_adult = condition(lambda: User.age >= User.max_age)

As with the lambda, decorated functions will be analyzed for which Facts attributes are accessed:

@condition def is_user_adult(user: User) -> bool: return user.age >= user.max_age

Notes
  • Async functions are not supported
  • Nested attribute access (e.g., a.b.c) is not allowed