GEPA.StopCondition behaviour (GEPA v0.3.0)

Copy Markdown View Source

Behavior for stop conditions that control when optimization terminates.

Stop conditions are predicates over the optimization state that return true when the optimization should stop.

Example Implementations

defmodule TimeoutStop do
  @behaviour GEPA.StopCondition

  defstruct [:start_time, :timeout_ms]

  @impl true
  def should_stop?(%__MODULE__{start_time: start, timeout_ms: timeout}, _state) do
    System.monotonic_time(:millisecond) - start > timeout
  end
end

Composing Stop Conditions

Use GEPA.StopCondition.Composite to combine multiple conditions with AND/OR logic.

Summary

Callbacks

Check if optimization should stop based on current state.

Functions

Evaluate a stop condition, callable, or module against the current state.

Update a stateful stop condition after an iteration.

Types

t()

@type t() :: term()

Callbacks

should_stop?(t, t)

@callback should_stop?(t(), GEPA.State.t()) :: boolean()

Check if optimization should stop based on current state.

Parameters

  • condition: The stop condition struct/state
  • gepa_state: Current GEPA optimization state

Returns

true if optimization should stop, false otherwise

Contract

  • Should be pure function (no side effects except reading state)
  • Should be monotonic: once true, should stay true
  • Should be fast (<1ms) to check

Functions

should_stop?(condition, state)

@spec should_stop?(term(), GEPA.State.t()) :: boolean()

Evaluate a stop condition, callable, or module against the current state.

update(condition, state)

@spec update(term(), GEPA.State.t()) :: term()

Update a stateful stop condition after an iteration.

Stateless conditions are returned unchanged. Composite conditions update each nested condition recursively.