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 non-existent nodes in halt conditions
elijahbenizzyfd8ea00d9f88
diff --git a/burr/core/application.py b/burr/core/application.pyindex fd45938c..21c4c4ea 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -529,6 +529,17 @@ class Application:inputs = {}return halt_before, halt_after, inputs+ def _validate_halt_conditions(self, halt_before: list[str], halt_after: list[str]) -> None:+ """Utility function to validate halt conditions"""+ missing_actions = set(halt_before + halt_after) - set(self._action_map.keys())+ if len(missing_actions) > 0:+ raise ValueError(+ ERROR_MESSAGE+ + f"Halt conditions {missing_actions} are not registered actions. Please ensure that they have been "+ f"registered as actions in the application and that you've spelled them correctly!"+ f"Valid actions are: {self._action_map.keys()}"+ )+def has_next_action(self) -> bool:"""Returns whether or not there is a next action to run.@@ -606,6 +617,7 @@ class Application:halt_before, halt_after, inputs = self._clean_iterate_params(halt_before, halt_after, inputs)+ self._validate_halt_conditions(halt_before, halt_after)result = Noneprior_action: Optional[Action] = None@@ -638,7 +650,7 @@ class Application:halt_before, halt_after, inputs = self._clean_iterate_params(halt_before, halt_after, inputs)-+ self._validate_halt_conditions(halt_before, halt_after)while self.has_next_action():# self.step will only return None if there is no next action, so we can rely on tuple unpackingprior_action, result, state = await self.astep(inputs=inputs)@@ -691,22 +703,13 @@ class Application:halt_before, halt_after, inputs = self._clean_iterate_params(halt_before, halt_after, inputs)+ self._validate_halt_conditions(halt_before, halt_after)async for prior_action, result, state in self.aiterate(halt_before=halt_before, halt_after=halt_after, inputs=inputs):passreturn self._return_value_iterate(halt_before, halt_after, prior_action, result)- def _validate_streaming_inputs(self, halt_after: list[str]):- missing_actions = set(halt_after) - set([action.name for action in self._actions])- # TODO -- implement this check elsewhere as well, break out into further utility functions- if len(missing_actions) > 0:- raise ValueError(- ERROR_MESSAGE- + f"Actions {missing_actions} were passed in as halt_after conditions, but not found in the actions list! "- f"Actions found: {[action.name for action in self._actions]}"- )-@telemetry.capture_function_usagedef stream_result(self,@@ -816,8 +819,7 @@ class Application:halt_before, halt_after, inputs = self._clean_iterate_params(halt_before, halt_after, inputs)-- self._validate_streaming_inputs(halt_after)+ self._validate_halt_conditions(halt_before, halt_after)self._increment_sequence_id()next_action = self.get_next_action()if next_action is None:diff --git a/tests/core/test_application.py b/tests/core/test_application.pyindex c5d8d8e0..2ffbcb5f 100644--- a/tests/core/test_application.py+++ b/tests/core/test_application.py@@ -1790,3 +1790,23 @@ def test_application_builder_assigns_correct_actions_with_dual_api():)graph = app.graphassert {a.name for a in graph.actions} == {"counter", "result", "test_action"}+++def test__validate_halt_conditions():+ counter_action = base_counter_action.with_name("counter")+ result_action = Result("count")++ @action(reads=[], writes=[])+ def test_action(state: State) -> Tuple[State, Dict[str, Any]]:+ return state, {}++ app = (+ ApplicationBuilder()+ .with_state(count=0)+ .with_actions(counter_action, test_action, result=result_action)+ .with_transitions()+ .with_entrypoint("counter")+ .build()+ )+ with pytest.raises(ValueError, match="(?=.*no_exist_1)(?=.*no_exist_2)"):+ app._validate_halt_conditions(halt_after=["no_exist_1"], halt_before=["no_exist_2"])