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
Fixes issues with streaming + sequence ID
elijahbenizzy766581b38ac4
diff --git a/burr/core/application.py b/burr/core/application.pyindex 01def373..c0c22d72 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -54,7 +54,6 @@ class Transition:PRIOR_STEP = "__PRIOR_STEP"SEQUENCE_ID = "__SEQUENCE_ID"-ERROR_MESSAGE = ("-------------------------------------------------------------------\n""Oh no an error! Need help with Burr?\n"@@ -282,10 +281,23 @@ class Application:initial_step: str,partition_key: Optional[str],uid: str,- sequence_id: Optional[int],+ sequence_id: Optional[int] = None,adapter_set: Optional[LifecycleAdapterSet] = None,builder: Optional["ApplicationBuilder"] = None,):+ """Instantiates an Application. This is an internal API -- use the builder!++ :param actions: Actions to run+ :param transitions: Transitions between actions+ :param state: State to run with+ :param initial_step: Step name to start at+ :param partition_key: Partition key for the application (optional)+ :param uid: Unique identifier for the application+ :param sequence_id: Sequence ID for the application. Note this will be incremented every run.+ So if this starts at 0, the first one you will see will be 1.+ :param adapter_set: Set of lifecycle adapters+ :param builder: Builder that created this application+ """self._partition_key = partition_keyself._uid = uidself._action_map = {action.name: action for action in actions}@@ -821,7 +833,6 @@ class Application:halt_before, halt_after, inputs)self._validate_halt_conditions(halt_before, halt_after)- self._increment_sequence_id()next_action = self.get_next_action()if next_action is None:raise ValueError(@@ -829,6 +840,7 @@ class Application:)if next_action.name not in halt_after:# fast forward until we get to the action+ # run already handles incrementing sequence IDs, nothing to worry about herenext_action, results, state = self.run(halt_before=halt_after + halt_before, inputs=inputs)@@ -837,18 +849,10 @@ class Application:# For context, this is specifically for the case in which you want to have# multiple terminal points with a unified API, where some are streaming, and some are not.if next_action.name in halt_before and next_action.name not in halt_after:- self._adapter_set.call_all_lifecycle_hooks_sync(- "post_run_step",- action=next_action,- state=self._state,- result=None,- sequence_id=self.sequence_id,- exception=None,- )return next_action, StreamingResultContainer.pass_through(results=results, final_state=state)- inputs = {} # inputs always go to the first action, we want to wipe them afterwards+ self._increment_sequence_id()self._adapter_set.call_all_lifecycle_hooks_sync("pre_run_step",action=next_action,@@ -868,18 +872,18 @@ class Application:result: Optional[dict],state: State,exc: Optional[Exception] = None,- seq_id=self.sequence_id,+ # seq_id=self.sequence_id,):self._adapter_set.call_all_lifecycle_hooks_sync("post_run_step",action=next_action,state=state,result=result,- sequence_id=seq_id,+ sequence_id=self.sequence_id,exception=exc,)- # we want to increment regardless of failure+ action_inputs = self._process_inputs(inputs, next_action)if not next_action.streaming:# In this case we are halting at a non-streaming condition# This is allowed as we want to maintain a more consistent API@@ -895,16 +899,19 @@ class Application:return action, StreamingResultContainer.pass_through(results=result, final_state=state)-if next_action.single_step:next_action = cast(SingleStepStreamingAction, next_action)- generator = _run_single_step_streaming_action(next_action, self._state, inputs)+ generator = _run_single_step_streaming_action(+ next_action, self._state, action_inputs+ )return next_action, StreamingResultContainer(generator, self._state, process_result, callback)else:next_action = cast(StreamingAction, next_action)- generator = _run_multi_step_streaming_action(next_action, self._state, inputs)+ generator = _run_multi_step_streaming_action(+ next_action, self._state, action_inputs+ )except Exception as e:# We only want to raise this in the case of an exception# otherwise, this will get delegated to the finallydiff --git a/tests/core/test_application.py b/tests/core/test_application.pyindex 2ffbcb5f..e63f9981 100644--- a/tests/core/test_application.py+++ b/tests/core/test_application.py@@ -1237,7 +1237,7 @@ async def test_app_a_run_async_and_sync():assert result["count"] > 20-def test_stream_result_halt_after():+def test_stream_result_halt_after_unique():action_tracker = ActionTracker()counter_action = base_streaming_counter.with_name("counter")counter_action_2 = base_streaming_counter.with_name("counter_2")@@ -1251,7 +1251,6 @@ def test_stream_result_halt_after():adapter_set=LifecycleAdapterSet(action_tracker),partition_key="test",uid="test-123",- sequence_id=0,)action_, streaming_container = app.stream_result(halt_after=["counter_2"])results = list(streaming_container)@@ -1263,6 +1262,14 @@ def test_stream_result_halt_after():assert len(action_tracker.post_called) == 2assert set(dict(action_tracker.pre_called).keys()) == {"counter", "counter_2"}assert set(dict(action_tracker.post_called).keys()) == {"counter", "counter_2"}+ assert [item["sequence_id"] for _, item in action_tracker.pre_called] == [+ 0,+ 1,+ ] # ensure sequence ID is respected+ assert [item["sequence_id"] for _, item in action_tracker.post_called] == [+ 0,+ 1,+ ] # ensure sequence ID is respecteddef test_stream_result_halt_after_single_step():@@ -1279,7 +1286,6 @@ def test_stream_result_halt_after_single_step():adapter_set=LifecycleAdapterSet(action_tracker),partition_key="test",uid="test-123",- sequence_id=0,)action_, streaming_container = app.stream_result(halt_after=["counter_2"])results = list(streaming_container)@@ -1291,6 +1297,14 @@ def test_stream_result_halt_after_single_step():assert len(action_tracker.post_called) == 2assert set(dict(action_tracker.pre_called).keys()) == {"counter", "counter_2"}assert set(dict(action_tracker.post_called).keys()) == {"counter", "counter_2"}+ assert [item["sequence_id"] for _, item in action_tracker.pre_called] == [+ 0,+ 1,+ ] # ensure sequence ID is respected+ assert [item["sequence_id"] for _, item in action_tracker.post_called] == [+ 0,+ 1,+ ] # ensure sequence ID is respecteddef test_stream_result_halt_after_run_through_final_streaming():@@ -1311,7 +1325,6 @@ def test_stream_result_halt_after_run_through_final_streaming():adapter_set=LifecycleAdapterSet(action_tracker),partition_key="test",uid="test-123",- sequence_id=0,)action_, streaming_container = app.stream_result(halt_after=["counter_streaming"])results = list(streaming_container)@@ -1328,6 +1341,12 @@ def test_stream_result_halt_after_run_through_final_streaming():"counter_streaming","counter_non_streaming",}+ assert [item["sequence_id"] for _, item in action_tracker.pre_called] == list(+ range(0, 11)+ ) # ensure sequence ID is respected+ assert [item["sequence_id"] for _, item in action_tracker.post_called] == list(+ range(0, 11)+ ) # ensure sequence ID is respecteddef test_stream_result_halt_after_run_through_final_non_streaming():@@ -1346,7 +1365,6 @@ def test_stream_result_halt_after_run_through_final_non_streaming():adapter_set=LifecycleAdapterSet(action_tracker),partition_key="test",uid="test-123",- sequence_id=0,)action, streaming_container = app.stream_result(halt_after=["counter_final_non_streaming"])results = list(streaming_container)@@ -1363,9 +1381,16 @@ def test_stream_result_halt_after_run_through_final_non_streaming():"counter_non_streaming","counter_final_non_streaming",}+ assert [item["sequence_id"] for _, item in action_tracker.pre_called] == list(+ range(0, 11)+ ) # ensure sequence ID is respected+ assert [item["sequence_id"] for _, item in action_tracker.post_called] == list(+ range(0, 11)+ ) # ensure sequence ID is respecteddef test_stream_result_halt_before():+ action_tracker = ActionTracker()counter_non_streaming = base_counter_action.with_name("counter_non_streaming")counter_streaming = base_streaming_single_step_counter.with_name("counter_final")@@ -1379,7 +1404,7 @@ def test_stream_result_halt_before():initial_step="counter_non_streaming",partition_key="test",uid="test-123",- sequence_id=0,+ adapter_set=LifecycleAdapterSet(action_tracker),)action, streaming_container = app.stream_result(halt_after=[], halt_before=["counter_final"])results = list(streaming_container)@@ -1388,6 +1413,12 @@ def test_stream_result_halt_before():assert action.name == "counter_final" # halt before this oneassert result is Noneassert state["count"] == 10+ assert [item["sequence_id"] for _, item in action_tracker.pre_called] == list(+ range(0, 10)+ ) # ensure sequence ID is respected+ assert [item["sequence_id"] for _, item in action_tracker.post_called] == list(+ range(0, 10)+ ) # ensure sequence ID is respecteddef test_app_set_state():