Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
Adds validation for conditions
elijahbenizzy694de48997d7
diff --git a/burr/core/action.py b/burr/core/action.pyindex 9f25e6eb..a7e330fa 100644--- a/burr/core/action.py+++ b/burr/core/action.py@@ -216,7 +216,15 @@ class Condition(Function):return Condition(keys, condition_func, name=expr)+ def _validate(self, state: State):+ missing_keys = set(self._keys) - set(state.keys())+ if missing_keys:+ raise ValueError(+ f"Missing keys in state required by condition: {self} {', '.join(missing_keys)}"+ )+def run(self, state: State, **run_kwargs) -> dict:+ self._validate(state)return {Condition.KEY: self._resolver(state)}@propertydiff --git a/docs/concepts/transitions.rst b/docs/concepts/transitions.rstindex bbb7da11..5ab9d336 100644--- a/docs/concepts/transitions.rst+++ b/docs/concepts/transitions.rst@@ -38,6 +38,8 @@ Conditions have a few APIs, but the most common are the three convenience functiConditions are evaluated in the order they are specified, and the first one that evaluates to True will be the transition that is selectedwhen determining which action to run next. If no condition evaluates to ``True``, the application execution will stop early.+All keys present in the condition (E.G. ``foo`` and ``epochs`` above) must be present in the state for the condition to work. It will error otherwise.+.. note::The ``default`` condition is a special case, and will always evaluate to ``True``. It is useful for defining a "catch-all" transitiondiff --git a/tests/core/test_action.py b/tests/core/test_action.pyindex 652b0c79..da890afc 100644--- a/tests/core/test_action.py+++ b/tests/core/test_action.py@@ -127,6 +127,17 @@ def test_condition_expr_complex():assert cond.run(State({"foo": "baz", "baz": "corge"})) == {Condition.KEY: False}+def test_condition__validate_success():+ cond = Condition.when(foo="bar")+ cond._validate(State({"foo": "bar"}))+++def test_condition__validate_failure():+ cond = Condition.when(foo="bar")+ with pytest.raises(ValueError, match="foo"):+ cond._validate(State({"baz": "baz"}))++def test_result():result = Result("foo", "bar")assert result.run(State({"foo": "baz", "bar": "qux", "baz": "quux"})) == {