Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Implements asynchronous streaming

elijahbenizzyd6eda3d40a8a
diff --git a/burr/core/action.py b/burr/core/action.py
index c896cf3e..af585497 100644
--- a/burr/core/action.py
+++ b/burr/core/action.py
@@ -15,10 +15,16 @@ from typing import (
List,
Optional,
Protocol,
+ Tuple,
TypeVar,
Union,
)
+if sys.version_info <= (3, 11):
+ Self = Any
+else:
+ from typing import Self
+
from burr.core.state import State
@@ -126,7 +132,7 @@ class Action(Function, Reducer, abc.ABC):
"""
self._name = None
- def with_name(self, name: str) -> "Action":
+ def with_name(self, name: str) -> Self:
"""Returns a copy of the given action with the given name. Why do we need this?
We instantiate actions without names, and then set them later. This is a way to
make the API cleaner/consolidate it, and the ApplicationBuilder will end up handling it
@@ -477,7 +483,7 @@ class FunctionBasedAction(SingleStepAction):
class StreamingAction(Action, abc.ABC):
@abc.abstractmethod
- def stream_run(self, state: State, **run_kwargs) -> Generator[dict, None, dict]:
+ def stream_run(self, state: State, **run_kwargs) -> Generator[dict, None, Tuple[dict, State]]:
"""Streaming action ``stream_run`` is different than standard action run. It:
1. streams in a result (the dict output)
2. Returns the final result
@@ -517,6 +523,26 @@ class StreamingAction(Action, abc.ABC):
return True
+class AsyncStreamingAction(Action, abc.ABC):
+ @abc.abstractmethod
+ async def stream_run(self, state: State, **run_kwargs) -> typing.AsyncGenerator[dict, None]:
+ pass
+
+ async def run(self, state: State, **run_kwargs) -> dict:
+ gen = self.stream_run(state, **run_kwargs)
+ result = None
+ async for item in gen:
+ result = item
+ return result
+
+ @property
+ def streaming(self) -> bool:
+ return True
+
+ def is_async(self) -> bool:
+ return True
+
+
# TODO -- documentation for this
class StreamingResultContainer(Iterator[dict]):
"""Container for a streaming result. This allows you to:
@@ -618,6 +644,81 @@ class StreamingResultContainer(Iterator[dict]):
return self._result
+class AsyncStreamingResultContainer(typing.AsyncIterator[dict]):
+ def __init__(
+ self,
+ streaming_result_generator: typing.AsyncGenerator[
+ typing.Tuple[dict, Optional[State]], None
+ ],
+ initial_state: State,
+ process_result: Callable[[dict, State], tuple[dict, State]],
+ callback: Callable[
+ [Optional[dict], State, Optional[Exception]], typing.Coroutine[None, None, None]
+ ],
+ ):
+ self.streaming_result_generator = streaming_result_generator
+ self._initial_state = initial_state
+ self._process_result = process_result
+ self._callback = callback
+ self._result = None
+ self._callback_realized = False
+
+ #
+ # async def _cb(self):
+ # print("calling back")
+ # result = self._result if self._result is not None else None, self._initial_state
+ # if not self._callback_realized:
+ # exc = sys.exc_info()[1]
+ # self._callback_realized = True
+ # await self._callback(*result, exc)
+
+ async def __anext__(self):
+ # TODO -- ensure this works
+ if self._result is not None:
+ # we're done, and we've run through it
+ raise StopAsyncIteration
+ result, state = await self.streaming_result_generator.__anext__()
+ if state is not None: # we're done -- we've hit the last one
+ self._result = self._process_result(result, state)
+ raise StopAsyncIteration
+ return result
+
+ def __aiter__(self):
+ async def g():
+ try:
+ while True:
+ yield await self.__anext__()
+ except StopAsyncIteration:
+ return
+ finally:
+ if self._result is None:
+ self._result = None, self._initial_state
+ if not self._callback_realized:
+ exc = sys.exc_info()[1]
+ self._callback_realized = True
+ await self._callback(*self._result, exc)
+
+ return g()
+
+ async def get(self) -> tuple[Optional[dict], State]:
+ async for _ in self:
+ pass
+
+ return self._result
+
+ @staticmethod
+ def pass_through(results: dict, final_state: State) -> "AsyncStreamingResultContainer":
+ async def just_results() -> typing.AsyncGenerator[tuple[dict, State], None]:
+ yield results, final_state
+
+ async def empty_callback(result: Optional[dict], state: State, exc: Optional[Exception]):
+ pass
+
+ return AsyncStreamingResultContainer(
+ just_results(), final_state, lambda result, state: (result, state), empty_callback
+ )
+
+
class SingleStepStreamingAction(SingleStepAction, abc.ABC):
"""Class to represent a "single-step" streaming action. This is meant to
work with the functional API. Note this is not user-facing -- the user will
@@ -625,7 +726,7 @@ class SingleStepStreamingAction(SingleStepAction, abc.ABC):
"""
@abc.abstractmethod
- def stream_run_and_update(
+ async def stream_run_and_update(
self, state: State, **run_kwargs
) -> Generator[dict, None, tuple[dict, State]]:
"""Streaming version of the run and update function. This
@@ -651,6 +752,43 @@ class SingleStepStreamingAction(SingleStepAction, abc.ABC):
return True
+class SingleStepStreamingActionAsync(SingleStepAction, abc.ABC):
+ """Class to represent a "single-step" streaming action. This is meant to
+ work with the functional API. Note this is not user-facing -- the user will
+ only interact with this by using the ``@streaming_action`` decorator.
+ """
+
+ @abc.abstractmethod
+ async def stream_run_and_update(
+ self, state: State, **run_kwargs
+ ) -> typing.AsyncGenerator[tuple[dict, Optional[State]], None]:
+ """Streaming version of the run and update function. This
+ return type is a generator that streams in a result, has no "send"
+ value.
+ """
+ pass
+
+ async def run_and_update(self, state: State, **run_kwargs) -> tuple[dict, State]:
+ """Runs the action and returns the final result. This allows us to run this as a
+ single step action. This is helpful for when the streaming result needs to be
+ run as an intermediate.
+ """
+ gen = self.stream_run_and_update(state, **run_kwargs)
+ last_result = None
+ while True:
+ try:
+ last_result = await gen.__anext__()
+ except StopAsyncIteration:
+ return last_result
+
+ @property
+ def streaming(self) -> bool:
+ return True
+
+ def is_async(self) -> bool:
+ return True
+
+
class FunctionBasedStreamingAction(SingleStepStreamingAction):
_fn: Callable[..., Generator[dict, None, tuple[dict, State]]]
@@ -711,6 +849,68 @@ class FunctionBasedStreamingAction(SingleStepStreamingAction):
return self._fn
+# TODO -- merge this with the above
+class FunctionBasedStreamingActionAsync(SingleStepStreamingActionAsync):
+ _fn: Callable[..., typing.AsyncGenerator[typing.Tuple[dict, Optional[State]], None]]
+
+ def __init__(
+ self,
+ fn: Callable[..., typing.AsyncGenerator[typing.Tuple[dict, Optional[State]], None]],
+ reads: List[str],
+ writes: List[str],
+ bound_params: dict = None,
+ ):
+ """Instantiates a function-based streaming action with the given function, reads, and writes.
+ The function must take in a state (and inputs) and return a generator of (result, new_state).
+
+ :param fn: Function to use
+ :param reads:
+ :param writes:
+ """
+ super(FunctionBasedStreamingActionAsync, self).__init__()
+ self._fn = fn
+ self._reads = reads
+ self._writes = writes
+ self._bound_params = bound_params if bound_params is not None else {}
+
+ async def stream_run_and_update(
+ self, state: State, **run_kwargs
+ ) -> typing.AsyncGenerator[typing.Tuple[dict, Optional[State]], None]:
+ async for result in self._fn(state, **self._bound_params, **run_kwargs):
+ yield result
+
+ @property
+ def reads(self) -> list[str]:
+ return self._reads
+
+ @property
+ def writes(self) -> list[str]:
+ return self._writes
+
+ @property
+ def streaming(self) -> bool:
+ return True
+
+ def with_params(self, **kwargs: Any) -> "FunctionBasedStreamingActionAsync":
+ """Binds parameters to the function. This is not user-facing -- this is
+ meant to be used internally by the API.
+
+ :param kwargs:
+ :return:
+ """
+ return FunctionBasedStreamingActionAsync(
+ self._fn, self._reads, self._writes, {**self._bound_params, **kwargs}
+ )
+
+ @property
+ def inputs(self) -> tuple[list[str], list[str]]:
+ return _get_inputs(self._bound_params, self._fn)
+
+ @property
+ def fn(self) -> Callable:
+ return self._fn
+
+
def _validate_action_function(fn: Callable):
"""Validates that an action has the signature: (state: State) -> tuple[dict, State]
@@ -835,14 +1035,26 @@ def streaming_action(
full_response = ''.join(buffer)
# return the final result
return {'response': full_response}, state.update(response=full_response)
+
+ TODO -- async
"""
# TODO -- see if we want to consolidate this in some way with the above
# Currently this is our only other way to use a decorator to implement actions, so it is OK to duplicate
def decorator(fn) -> FunctionRepresentingAction:
- setattr(
- fn, FunctionBasedAction.ACTION_FUNCTION, FunctionBasedStreamingAction(fn, reads, writes)
- )
+ is_async = inspect.isasyncgenfunction(fn)
+ if not is_async:
+ setattr(
+ fn,
+ FunctionBasedAction.ACTION_FUNCTION,
+ FunctionBasedStreamingAction(fn, reads, writes),
+ )
+ else:
+ setattr(
+ fn,
+ FunctionBasedAction.ACTION_FUNCTION,
+ FunctionBasedStreamingActionAsync(fn, reads, writes),
+ )
setattr(fn, "bind", types.MethodType(bind, fn))
return fn
diff --git a/burr/core/application.py b/burr/core/application.py
index 16c8817f..086814a7 100644
--- a/burr/core/application.py
+++ b/burr/core/application.py
@@ -25,11 +25,14 @@ from burr.common import types as burr_types
from burr.core import persistence
from burr.core.action import (
Action,
+ AsyncStreamingAction,
+ AsyncStreamingResultContainer,
Condition,
Function,
Reducer,
SingleStepAction,
SingleStepStreamingAction,
+ SingleStepStreamingActionAsync,
StreamingAction,
StreamingResultContainer,
create_action,
@@ -236,6 +239,35 @@ def _run_single_step_streaming_action(
return result, state
+async def _run_single_step_streaming_action_async(
+ action: SingleStepStreamingActionAsync, state: State, inputs: Optional[Dict[str, Any]]
+) -> AsyncGenerator[Tuple[dict, Optional[State]], None]:
+ action.validate_inputs(inputs)
+ generator = action.stream_run_and_update(state, **inputs)
+ result = None
+ state_update = None
+ async for item in generator:
+ if not isinstance(item, tuple):
+ # TODO -- consider adding support for just returning a result.
+ raise ValueError(
+ f"Action {action.name} must yield a tuple of (result, state_update). "
+ f"For all non-final results (intermediate),"
+ f"the state update must be None"
+ )
+ result, state_update = item
+ if state_update is None:
+ yield result, None
+ if state_update is None:
+ raise ValueError(
+ f"Action {action.name} did not return a state update. For async actions, the last yield "
+ f"statement must be a tuple of (result, state_update). For example, yield dict(foo='bar'), state.update(foo='bar')"
+ )
+ _validate_result(result, action.name)
+ _validate_reducer_writes(action, state_update, action.name)
+ # TODO -- add guard against zero-length stream
+ yield result, state_update
+
+
def _run_multi_step_streaming_action(
action: StreamingAction, state: State, inputs: Optional[Dict[str, Any]]
) -> Generator[dict, None, Tuple[dict, State]]:
@@ -247,6 +279,21 @@ def _run_multi_step_streaming_action(
return result, _state_update(state, new_state)
+async def _run_multi_step_streaming_action_async(
+ action: AsyncStreamingAction, state: State, inputs: Optional[Dict[str, Any]]
+) -> AsyncGenerator[Tuple[dict, Optional[State]], None]:
+ action.validate_inputs(inputs)
+ generator = action.stream_run(state, **inputs)
+ result = None
+ async for item in generator:
+ result = item
+ yield item, None
+ state_update = _run_reducer(action, state, result, action.name)
+ _validate_result(result, action.name)
+ _validate_reducer_writes(action, state_update, action.name)
+ yield result, state_update
+
+
async def _arun_single_step_action(
action: SingleStepAction, state: State, inputs: Optional[Dict[str, Any]]
) -> Tuple[dict, State]:
@@ -466,23 +513,28 @@ class Application:
:return: Tuple[Function, dict, State] -- the action that was just ran, the result of running it, and the new state
"""
- # we want to increment regardless of failure
self._increment_sequence_id()
+ out = await self._astep(inputs=inputs, _run_hooks=True)
+ return out
+
+ async def _astep(self, inputs: Optional[Dict[str, Any]], _run_hooks: bool = True):
+ # we want to increment regardless of failure
next_action = self.get_next_action()
if next_action is None:
return None
if inputs is None:
inputs = {}
action_inputs = self._process_inputs(inputs, next_action)
- await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(
- "pre_run_step",
- action=next_action,
- state=self._state,
- inputs=action_inputs,
- sequence_id=self.sequence_id,
- app_id=self._uid,
- partition_key=self._partition_key,
- )
+ if _run_hooks:
+ await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(
+ "pre_run_step",
+ action=next_action,
+ state=self._state,
+ inputs=action_inputs,
+ sequence_id=self.sequence_id,
+ app_id=self._uid,
+ partition_key=self._partition_key,
+ )
exc = None
result = None
new_state = self._state
@@ -512,16 +564,17 @@ class Application:
logger.exception(_format_error_message(next_action, self._state, inputs))
raise e
finally:
- await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(
- "post_run_step",
- action=next_action,
- state=new_state,
- result=result,
- sequence_id=self.sequence_id,
- exception=exc,
- app_id=self._uid,
- partition_key=self._partition_key,
- )
+ if _run_hooks:
+ await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(
+ "post_run_step",
+ action=next_action,
+ state=new_state,
+ result=result,
+ sequence_id=self.sequence_id,
+ exception=exc,
+ app_id=self._uid,
+ partition_key=self._partition_key,
+ )
return next_action, result, new_state
@@ -947,12 +1000,122 @@ class Application:
halt_after: list[str],
halt_before: list[str] = None,
inputs: Optional[Dict[str, Any]] = None,
- ) -> Tuple[Action, ...]:
+ ) -> Tuple[Action, AsyncStreamingResultContainer]:
"""Placeholder for the async version of stream_result. This is not yet implemented."""
- raise NotImplementedError(
- "This has not yet been implemented! See the github issue: https://github.com/DAGWorks-Inc/burr/issues/64"
- " for details. Please comment or vote to get it implemented quickly!"
+ halt_before, halt_after, inputs = self._clean_iterate_params(
+ halt_before, halt_after, inputs
+ )
+ self._validate_halt_conditions(halt_before, halt_after)
+ next_action = self.get_next_action()
+ if next_action is None:
+ raise ValueError(
+ f"Cannot stream result -- no next action found! Prior action was: {self._state[PRIOR_STEP]}"
+ )
+ 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 here
+ next_action, results, state = await self.arun(
+ halt_before=halt_after + halt_before, inputs=inputs
+ )
+ # In this case, we are ready to halt and return an empty generator
+ # The results will be None, and the state will be the final state
+ # 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:
+ return next_action, AsyncStreamingResultContainer.pass_through(
+ results=results, final_state=state
+ )
+ self._increment_sequence_id()
+ await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(
+ "pre_run_step",
+ action=next_action,
+ state=self._state,
+ inputs=inputs,
+ sequence_id=self.sequence_id,
+ app_id=self._uid,
+ partition_key=self._partition_key,
+ )
+ try:
+
+ def process_result(result: dict, state: State) -> Tuple[Dict[str, Any], State]:
+ new_state = self._update_internal_state_value(state, next_action)
+ self._set_state(new_state)
+ return result, new_state
+
+ async def callback(
+ result: Optional[dict],
+ state: State,
+ exc: Optional[Exception] = None,
+ ):
+ await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(
+ "post_run_step",
+ app_id=self._uid,
+ partition_key=self._partition_key,
+ action=next_action,
+ state=state,
+ result=result,
+ sequence_id=self.sequence_id,
+ exception=exc,
+ )
+
+ 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
+ # TODO -- get this to work with async. Figure out how to run the async step...
+ action, result, state = await self._astep(inputs=inputs, _run_hooks=False)
+ await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(
+ "post_run_step",
+ app_id=self._uid,
+ partition_key=self._partition_key,
+ action=next_action,
+ state=self._state,
+ result=result,
+ sequence_id=self.sequence_id,
+ exception=None,
+ )
+ return action, AsyncStreamingResultContainer.pass_through(
+ results=result, final_state=state
+ )
+ if next_action.single_step:
+ next_action = cast(SingleStepStreamingActionAsync, next_action)
+ if not next_action.is_async():
+ raise ValueError("TODO -- convert")
+ generator = _run_single_step_streaming_action_async(
+ next_action, self._state, action_inputs
+ )
+ return next_action, AsyncStreamingResultContainer(
+ generator, self._state, process_result, callback
+ )
+ else:
+ if not next_action.is_async():
+ raise ValueError("TODO -- convert")
+ next_action = cast(AsyncStreamingAction, next_action)
+ generator = _run_multi_step_streaming_action_async(
+ 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 finally
+ # block of the streaming result container
+ self._adapter_set.call_all_lifecycle_hooks_sync(
+ "post_run_step",
+ app_id=self._uid,
+ partition_key=self._partition_key,
+ action=next_action,
+ state=self._state,
+ result=None,
+ sequence_id=self.sequence_id,
+ exception=e,
+ )
+ raise
+ return next_action, AsyncStreamingResultContainer(
+ generator, self._state, process_result, callback
)
+ # raise NotImplementedError(
+ # "This has not yet been implemented! See the github issue: https://github.com/DAGWorks-Inc/burr/issues/64"
+ # " for details. Please comment or vote to get it implemented quickly!"
+ # )
@telemetry.capture_function_usage
def visualize(
diff --git a/examples/streaming-overview/async_application.py b/examples/streaming-overview/async_application.py
new file mode 100644
index 00000000..3c491b6f
--- /dev/null
+++ b/examples/streaming-overview/async_application.py
@@ -0,0 +1,205 @@
+import asyncio
+from typing import Optional, Tuple
+
+import openai
+
+from burr.core import Application, ApplicationBuilder, State, default, when
+from burr.core.action import action, streaming_action
+
+MODES = {
+ "answer_question": "text",
+ "generate_image": "image",
+ "generate_code": "code",
+ "unknown": "text",
+}
+
+
+@action(reads=[], writes=["chat_history", "prompt"])
+def process_prompt(state: State, prompt: str) -> Tuple[dict, State]:
+ result = {"chat_item": {"role": "user", "content": prompt, "type": "text"}}
+ return result, state.wipe(keep=["prompt", "chat_history"]).append(
+ chat_history=result["chat_item"]
+ ).update(prompt=prompt)
+
+
+@action(reads=["prompt"], writes=["safe"])
+def check_safety(state: State) -> Tuple[dict, State]:
+ result = {"safe": "unsafe" not in state["prompt"]} # quick hack to demonstrate
+ return result, state.update(safe=result["safe"])
+
+
+def _get_openai_client():
+ return openai.AsyncClient()
+
+
+@action(reads=["prompt"], writes=["mode"])
+async def choose_mode(state: State) -> Tuple[dict, State]:
+ prompt = (
+ f"You are a chatbot. You've been prompted this: {state['prompt']}. "
+ f"You have the capability of responding in the following modes: {', '.join(MODES)}. "
+ "Please respond with *only* a single word representing the mode that most accurately "
+ "corresponds to the prompt. Fr instance, if the prompt is 'draw a picture of a cat', "
+ "the mode would be 'generate_image'. If the prompt is 'what is the capital of France', the mode would be 'answer_question'."
+ "If none of these modes apply, please respond with 'unknown'."
+ )
+
+ result = await _get_openai_client().chat.completions.create(
+ model="gpt-4",
+ messages=[
+ {"role": "system", "content": "You are a helpful assistant"},
+ {"role": "user", "content": prompt},
+ ],
+ )
+ content = result.choices[0].message.content
+ mode = content.lower()
+ if mode not in MODES:
+ mode = "unknown"
+ result = {"mode": mode}
+ return result, state.update(**result)
+
+
+@action(reads=["prompt", "chat_history"], writes=["response"])
+def prompt_for_more(state: State) -> Tuple[dict, State]:
+ """Not streaming, as we have the result immediately."""
+ result = {
+ "response": {
+ "content": "None of the response modes I support apply to your question. Please clarify?",
+ "type": "text",
+ "role": "assistant",
+ }
+ }
+ return result, state.update(**result).append(chat_history=result["response"])
+
+
+@streaming_action(reads=["prompt", "chat_history", "mode"], writes=["response"])
+async def chat_response(
+ state: State, prepend_prompt: str, model: str = "gpt-3.5-turbo"
+) -> Tuple[dict, State]:
+ """Streaming action, as we don't have the result immediately. This makes it more interactive"""
+ chat_history = state["chat_history"].copy()
+ chat_history[-1]["content"] = f"{prepend_prompt}: {chat_history[-1]['content']}"
+ chat_history_api_format = [
+ {
+ "role": chat["role"],
+ "content": chat["content"],
+ }
+ for chat in chat_history
+ ]
+ client = _get_openai_client()
+ result = await client.chat.completions.create(
+ model=model, messages=chat_history_api_format, stream=True
+ )
+ buffer = []
+ async for chunk in result:
+ chunk_str = chunk.choices[0].delta.content
+ if chunk_str is None:
+ continue # consider breaking...
+ buffer.append(chunk_str)
+ yield {
+ "response": {"content": chunk_str, "type": MODES[state["mode"]], "role": "assistant"}
+ }, None
+
+ result = {
+ "response": {"content": "".join(buffer), "type": MODES[state["mode"]], "role": "assistant"}
+ }
+ yield result, state.update(**result).append(chat_history=result["response"])
+
+
+@action(reads=["prompt", "chat_history"], writes=["response"])
+def unsafe_response(state: State) -> Tuple[dict, State]:
+ result = {
+ "response": {
+ "content": "I am afraid I can't respond to that...",
+ "type": "text",
+ "role": "assistant",
+ }
+ }
+ return result, state.update(**result).append(chat_history=result["response"])
+
+
+@action(reads=["prompt", "chat_history", "mode"], writes=["response"])
+def image_response(state: State, model: str = "dall-e-2") -> Tuple[dict, State]:
+ client = _get_openai_client()
+ result = client.images.generate(
+ model=model, prompt=state["prompt"], size="1024x1024", quality="standard", n=1
+ )
+ response = result.data[0].url
+ result = {"response": {"content": response, "type": MODES[state["mode"]], "role": "assistant"}}
+ return result, state.update(**result).append(chat_history=result["response"])
+
+
+def application(app_id: Optional[str] = None) -> Application:
+ return (
+ ApplicationBuilder()
+ .with_actions(
+ prompt=process_prompt,
+ check_safety=check_safety,
+ unsafe_response=unsafe_response,
+ decide_mode=choose_mode,
+ generate_image=image_response,
+ generate_code=chat_response.bind(
+ prepend_prompt="Please respond with *only* code and no other text (at all) to the following:",
+ ),
+ answer_question=chat_response.bind(
+ prepend_prompt="Please answer the following question:",
+ ),
+ prompt_for_more=prompt_for_more,
+ )
+ .with_entrypoint("prompt")
+ .with_state(chat_history=[])
+ .with_transitions(
+ ("prompt", "check_safety", default),
+ ("check_safety", "decide_mode", when(safe=True)),
+ ("check_safety", "unsafe_response", default),
+ ("decide_mode", "generate_image", when(mode="generate_image")),
+ ("decide_mode", "generate_code", when(mode="generate_code")),
+ ("decide_mode", "answer_question", when(mode="answer_question")),
+ ("decide_mode", "prompt_for_more", default),
+ (
+ [
+ "generate_image",
+ "answer_question",
+ "generate_code",
+ "prompt_for_more",
+ "unsafe_response",
+ ],
+ "prompt",
+ ),
+ )
+ .with_tracker(project="demo_chatbot_streaming_async")
+ .with_identifiers(app_id=app_id)
+ .build()
+ )
+
+
+# TODO -- replace these with action tags when we have the availability
+TERMINAL_ACTIONS = [
+ "generate_image",
+ "answer_question",
+ "generate_code",
+ "prompt_for_more",
+ "unsafe_response",
+]
+
+
+async def it(app):
+ action, result = await app.astream_result(
+ halt_after=TERMINAL_ACTIONS, inputs={"prompt": "What are the first 100 prime numbers?"}
+ )
+ async for item in result:
+ print(item["response"]["content"], end="")
+ result, state = await result.get()
+ print(result["response"]["content"])
+
+
+async def main():
+ app = application()
+ await it(app)
+ await it(app)
+
+
+if __name__ == "__main__":
+ # app.visualize(
+ # output_file_path="statemachine", include_conditions=False, view=True, format="png"
+ # )
+ asyncio.run(main())
diff --git a/examples/streaming-overview/statemachine.png b/examples/streaming-overview/statemachine.png
index 1f049315..428869db 100644
Binary files a/examples/streaming-overview/statemachine.png and b/examples/streaming-overview/statemachine.png differ
diff --git a/pyproject.toml b/pyproject.toml
index 8fa41804..1513e9eb 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "burr"
-version = "0.17.1"
+version = "0.18.0rc0"
dependencies = [] # yes, there are none
requires-python = ">=3.9"
authors = [
diff --git a/tests/core/test_action.py b/tests/core/test_action.py
index 01035f03..ecf8348a 100644
--- a/tests/core/test_action.py
+++ b/tests/core/test_action.py
@@ -1,17 +1,20 @@
import asyncio
-from typing import Generator, Tuple
+from typing import AsyncGenerator, Generator, Optional, Tuple
import pytest
from burr.core import State
from burr.core.action import (
Action,
+ AsyncStreamingAction,
+ AsyncStreamingResultContainer,
Condition,
Function,
Input,
Result,
SingleStepAction,
SingleStepStreamingAction,
+ SingleStepStreamingActionAsync,
StreamingAction,
StreamingResultContainer,
_validate_action_function,
@@ -386,6 +389,36 @@ def test_create_action_streaming_fn_api_with_bind():
assert out == list("prefix_foo")
+async def test_create_action_streaming_fn_api_with_bind_async():
+ @streaming_action(reads=["input_variable"], writes=["output_variable"])
+ async def test_action(
+ state: State, prefix: str
+ ) -> AsyncGenerator[Tuple[dict, Optional[State]], None]:
+ buffer = []
+ for c in prefix + state["input_variable"]:
+ buffer.append(c)
+ yield {"output_variable": c}, None # intermediate results
+ await asyncio.sleep(0.01)
+ joined = "".join(buffer)
+ yield {"output_variable": joined}, state.update(output_variable=joined)
+
+ created_action = create_action(test_action.bind(prefix="prefix_"), name="my_action")
+ assert created_action.streaming # streaming
+ assert created_action.is_async() # async
+ assert isinstance(created_action, SingleStepStreamingActionAsync)
+ assert created_action.name == "my_action"
+ assert created_action.reads == ["input_variable"]
+ assert created_action.writes == ["output_variable"]
+ assert created_action.single_step
+ gen = created_action.stream_run_and_update(State({"input_variable": "foo"}))
+ out = [item async for item in gen]
+ final_result, state = out[-1]
+ intermediate_results = [item[0]["output_variable"] for item in out[:-1]]
+ assert final_result == {"output_variable": "prefix_foo"}
+ assert state.get_all() == {"input_variable": "foo", "output_variable": "prefix_foo"}
+ assert intermediate_results == list("prefix_foo")
+
+
def test_create_action_undecorated_function():
def test_action(state: State) -> Tuple[dict, State]:
result = {"output_variable": state["input_variable"]}
@@ -427,12 +460,14 @@ def test__validate_action_function_invalid_signature_correct():
def test_streaming_action_stream_run():
class SimpleStreamingAction(StreamingAction):
- def stream_run(self, state: State, **run_kwargs) -> Generator[dict, None, dict]:
+ def stream_run(
+ self, state: State, **run_kwargs
+ ) -> Generator[dict, None, Tuple[dict, State]]:
buffer = []
for char in state["echo"]:
yield {"response": char}
buffer.append(char)
- return {"response": "".join(buffer)}
+ return {"response": "".join(buffer)}, state.update(response="".join(buffer))
@property
def reads(self) -> list[str]:
@@ -447,7 +482,39 @@ def test_streaming_action_stream_run():
action = SimpleStreamingAction()
STR = "test streaming action"
- assert action.run(State({"echo": STR}))["response"] == STR
+ result, state_update = action.run(State({"echo": STR}))
+ assert result["response"] == STR
+ assert state_update["response"] == STR
+
+
+async def test_streaming_action_stream_run_async():
+ class SimpleAsyncStreamingAction(AsyncStreamingAction):
+ async def stream_run(
+ self, state: State, **run_kwargs
+ ) -> AsyncGenerator[Tuple[dict, Optional[State]], None]:
+ buffer = []
+ for char in state["echo"]:
+ yield {"response": char}, None
+ await asyncio.sleep(0.01)
+ buffer.append(char)
+ yield {"response": "".join(buffer)}, state.update(response="".join(buffer))
+
+ @property
+ def reads(self) -> list[str]:
+ return []
+
+ @property
+ def writes(self) -> list[str]:
+ return ["response"]
+
+ def update(self, result: dict, state: State) -> State:
+ return state.update(**result)
+
+ action = SimpleAsyncStreamingAction()
+ STR = "test streaming action"
+ result, state_update = await action.run(State({"echo": STR}))
+ assert result["response"] == STR
+ assert state_update["response"] == STR
def sample_generator(chars: str) -> Generator[dict, None, Tuple[dict, State]]:
@@ -532,3 +599,112 @@ def test_streaming_result_callback_error():
# See note in StreamingResultContainer
except SentinelError:
pass
+
+
+async def sample_async_generator(chars: str) -> AsyncGenerator[Tuple[dict, Optional[State]], None]:
+ buffer = []
+ for c in chars:
+ buffer.append(c)
+ yield {"response": c}, None
+ await asyncio.sleep(0.01)
+ joined = "".join(buffer)
+ yield {"response": joined}, State({"response": joined})
+
+
+async def test_streaming_result_container_iterate_async():
+ async def callback(r: dict, s: State, e: Exception):
+ pass
+
+ string_value = "test streaming action"
+ container = AsyncStreamingResultContainer(
+ sample_async_generator(string_value),
+ initial_state=State(),
+ process_result=lambda r, s: (r, s),
+ callback=callback,
+ )
+ assert [item["response"] async for item in container] == list(string_value)
+ result, state = await container.get()
+ assert result["response"] == string_value
+
+
+async def test_streaming_result_get_runs_through_async():
+ async def callback(r: dict, s: State, e: Exception):
+ pass
+
+ string_value = "test streaming action"
+ container = AsyncStreamingResultContainer(
+ sample_async_generator(string_value),
+ initial_state=State(),
+ process_result=lambda r, s: (r, s),
+ callback=callback,
+ )
+ result, state = await container.get()
+ assert result["response"] == string_value
+
+
+async def test_streaming_result_callback_called_async():
+ called = []
+ string_value = "test streaming action"
+
+ async def callback(r: Optional[dict], s: State, e: Exception):
+ called.append((s, r, e))
+
+ container = AsyncStreamingResultContainer(
+ sample_async_generator(string_value),
+ # initial state is here solely for returning debugging so we can return an
+ # state to the user in the case of failure
+ initial_state=State({"foo": "bar"}),
+ process_result=lambda r, s: (r, s),
+ callback=callback,
+ )
+ await container.get()
+ assert len(called) == 1
+ result, state, error = called[0]
+ assert result["response"] == string_value
+ assert state["response"] == string_value
+ assert error is None
+
+
+def test_streaming_result_callback_error_async():
+ """Oi. This can't use pytest-asyncio because pytest-asyncio doesn't shutdown async gens.
+ I sure hope our customer's stuff shuts down async gens.
+
+ See https://github.com/pytest-dev/pytest-asyncio/issues/759 for more details.
+
+ """
+ called = []
+
+ async def test_fn():
+ class SentinelError(Exception):
+ pass
+
+ try:
+
+ async def callback(r: Optional[dict], s: State, e: Exception):
+ print("calling back")
+ called.append((r, s, e))
+
+ string_value = "test streaming action"
+ container = AsyncStreamingResultContainer(
+ sample_async_generator(string_value),
+ initial_state=State({"foo": "bar"}),
+ process_result=lambda r, s: (r, s),
+ callback=callback,
+ )
+ try:
+ async for _ in container:
+ raise SentinelError("error")
+ finally:
+ # pass
+ print("asserting")
+
+ # Exception is currently not exactly what we want, so won't assert on that.
+ # See note in StreamingResultContainer
+ except SentinelError:
+ pass
+
+ asyncio.run(test_fn())
+ assert len(called) == 1
+ ((result, state, error),) = called
+ assert state["foo"] == "bar"
+ assert result is None
diff --git a/tests/core/test_application.py b/tests/core/test_application.py
index 56688b3e..07e187fc 100644
--- a/tests/core/test_application.py
+++ b/tests/core/test_application.py
@@ -1,6 +1,7 @@
import asyncio
import collections
import logging
+import typing
from typing import Any, Awaitable, Callable, Dict, Generator, Literal, Optional, Tuple
import pytest
@@ -8,11 +9,13 @@ import pytest
from burr.core import State
from burr.core.action import (
Action,
+ AsyncStreamingAction,
Condition,
Reducer,
Result,
SingleStepAction,
SingleStepStreamingAction,
+ SingleStepStreamingActionAsync,
StreamingAction,
action,
default,
@@ -28,9 +31,11 @@ from burr.core.application import (
_assert_set,
_run_function,
_run_multi_step_streaming_action,
+ _run_multi_step_streaming_action_async,
_run_reducer,
_run_single_step_action,
_run_single_step_streaming_action,
+ _run_single_step_streaming_action_async,
_validate_actions,
_validate_start,
_validate_transitions,
@@ -394,6 +399,29 @@ def test_run_single_step_streaming_action_errors_missing_write():
collections.deque(gen, maxlen=0) # exhaust the generator
+def test_run_single_step_streaming_action_errors_missing_write_async():
+ class BrokenAction(SingleStepStreamingAction):
+ def stream_run_and_update(
+ self, state: State, **run_kwargs
+ ) -> Generator[dict, None, Tuple[dict, State]]:
+ yield {}
+ return {"present_value": 1}, state.update(present_value=1)
+
+ @property
+ def reads(self) -> list[str]:
+ return []
+
+ @property
+ def writes(self) -> list[str]:
+ return ["missing_value", "present_value"]
+
+ action = BrokenAction()
+ state = State()
+ with pytest.raises(ValueError, match="missing_value"):
+ gen = _run_single_step_streaming_action(action, state, inputs={})
+ collections.deque(gen, maxlen=0) # exhaust the generator
+
+
def test_run_multi_step_streaming_action_errors_missing_write():
class BrokenAction(StreamingAction):
def stream_run(self, state: State, **run_kwargs) -> Generator[dict, None, dict]:
@@ -478,7 +506,7 @@ class SingleStepCounterWithInputsAsync(SingleStepCounterAsync):
class StreamingCounter(StreamingAction):
def stream_run(self, state: State, **run_kwargs) -> Generator[dict, None, dict]:
- if "steps_per_couunt" in run_kwargs:
+ if "steps_per_count" in run_kwargs:
steps_per_count = run_kwargs["granularity"]
else:
steps_per_count = 10
@@ -499,6 +527,29 @@ class StreamingCounter(StreamingAction):
return state.update(**result).append(tracker=result["count"])
+class AsyncStreamingCounter(AsyncStreamingAction):
+ async def stream_run(self, state: State, **run_kwargs) -> typing.AsyncGenerator[dict, None]:
+ if "steps_per_count" in run_kwargs:
+ steps_per_count = run_kwargs["granularity"]
+ else:
+ steps_per_count = 10
+ count = state["count"]
+ for i in range(steps_per_count):
+ await asyncio.sleep(0.01)
+ yield {"count": count + (i + 1) / 10}
+
+ @property
+ def reads(self) -> list[str]:
+ return ["count"]
+
+ @property
+ def writes(self) -> list[str]:
+ return ["count", "tracker"]
+
+ def update(self, result: dict, state: State) -> State:
+ return state.update(**result).append(tracker=result["count"])
+
+
class SingleStepStreamingCounter(SingleStepStreamingAction):
def stream_run_and_update(
self, state: State, **run_kwargs
@@ -518,6 +569,27 @@ class SingleStepStreamingCounter(SingleStepStreamingAction):
return ["count", "tracker"]
+class SingleStepStreamingCounterAsync(SingleStepStreamingActionAsync):
+ async def stream_run_and_update(
+ self, state: State, **run_kwargs
+ ) -> typing.AsyncGenerator[Tuple[dict, Optional[State]], None]:
+ steps_per_count = run_kwargs.get("granularity", 10)
+ count = state["count"]
+ for i in range(steps_per_count):
+ await asyncio.sleep(0.01)
+ yield {"count": count + ((i + 1) / 10)}, None
+ await asyncio.sleep(0.01)
+ yield {"count": count + 1}, state.update(count=count + 1).append(tracker=count + 1)
+
+ @property
+ def reads(self) -> list[str]:
+ return ["count"]
+
+ @property
+ def writes(self) -> list[str]:
+ return ["count", "tracker"]
+
+
class StreamingActionIncorrectResultType(StreamingAction):
def stream_run(self, state: State, **run_kwargs) -> Generator[dict, None, dict]:
yield {}
@@ -535,6 +607,23 @@ class StreamingActionIncorrectResultType(StreamingAction):
return state
+class StreamingActionIncorrectResultTypeAsync(AsyncStreamingAction):
+ async def stream_run(self, state: State, **run_kwargs) -> typing.AsyncGenerator[dict, None]:
+ yield {}
+ yield "not a dict"
+
+ @property
+ def reads(self) -> list[str]:
+ return []
+
+ @property
+ def writes(self) -> list[str]:
+ return []
+
+ def update(self, result: dict, state: State) -> State:
+ return state
+
+
class StreamingSingleStepActionIncorrectResultType(SingleStepStreamingAction):
def stream_run_and_update(
self, state: State, **run_kwargs
@@ -551,6 +640,22 @@ class StreamingSingleStepActionIncorrectResultType(SingleStepStreamingAction):
return []
+class StreamingSingleStepActionIncorrectResultTypeAsync(SingleStepStreamingActionAsync):
+ async def stream_run_and_update(
+ self, state: State, **run_kwargs
+ ) -> typing.AsyncGenerator[Tuple[dict, Optional[State]], None]:
+ yield {}, None
+ yield "not a dict", state
+
+ @property
+ def reads(self) -> list[str]:
+ return []
+
+ @property
+ def writes(self) -> list[str]:
+ return []
+
+
base_single_step_counter = SingleStepCounter()
base_single_step_counter_async = SingleStepCounterAsync()
base_single_step_counter_with_inputs = SingleStepCounterWithInputs()
@@ -559,6 +664,9 @@ base_single_step_counter_with_inputs_async = SingleStepCounterWithInputsAsync()
base_streaming_counter = StreamingCounter()
base_streaming_single_step_counter = SingleStepStreamingCounter()
+base_streaming_counter_async = AsyncStreamingCounter()
+base_streaming_single_step_counter_async = SingleStepStreamingCounterAsync()
+
base_single_step_action_incorrect_result_type = SingleStepActionIncorrectResultType()
base_single_step_action_incorrect_result_type_async = SingleStepActionIncorrectResultTypeAsync()
@@ -661,6 +769,21 @@ def test__run_multistep_streaming_action():
assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}
+async def test__run_multistep_streaming_action_async():
+ action = base_streaming_counter_async.with_name("counter")
+ state = State({"count": 0, "tracker": []})
+ generator = _run_multi_step_streaming_action_async(action, state, inputs={})
+ last_result = -1
+ result = None
+ async for result, state in generator:
+ if last_result < 1:
+ # Otherwise you hit floating poit comparison problems
+ assert result["count"] > last_result
+ last_result = result["count"]
+ assert result == {"count": 1}
+ assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}
+
+
def test__run_streaming_action_incorrect_result_type():
action = StreamingActionIncorrectResultType()
state = State()
@@ -669,6 +792,15 @@ def test__run_streaming_action_incorrect_result_type():
collections.deque(gen, maxlen=0) # exhaust the generator
+async def test__run_streaming_action_incorrect_result_type_async():
+ action = StreamingActionIncorrectResultTypeAsync()
+ state = State()
+ with pytest.raises(ValueError, match="returned a non-dict"):
+ gen = _run_multi_step_streaming_action_async(action, state, inputs={})
+ async for item in gen:
+ print(item)
+
+
def test__run_single_step_streaming_action_incorrect_result_type():
action = StreamingSingleStepActionIncorrectResultType()
state = State()
@@ -677,6 +809,14 @@ def test__run_single_step_streaming_action_incorrect_result_type():
collections.deque(gen, maxlen=0) # exhaust the generator
+async def test__run_single_step_streaming_action_incorrect_result_type_async():
+ action = StreamingSingleStepActionIncorrectResultTypeAsync()
+ state = State()
+ with pytest.raises(ValueError, match="returned a non-dict"):
+ gen = _run_single_step_streaming_action_async(action, state, inputs={})
+ _ = [item async for item in gen]
+
+
def test__run_single_step_streaming_action():
action = base_streaming_single_step_counter.with_name("counter")
state = State({"count": 0, "tracker": []})
@@ -693,6 +833,22 @@ def test__run_single_step_streaming_action():
assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}
+async def test__run_single_step_streaming_action_async():
+ async_action = base_streaming_single_step_counter_async.with_name("counter")
+ state = State({"count": 0, "tracker": []})
+ generator = _run_single_step_streaming_action_async(async_action, state, inputs={})
+ last_result = -1
+ result, state = None, None
+ async for result, state in generator:
+ if last_result < 1:
+ # Otherwise you hit comparison issues
+ # This is because we get to the last one, which is the final result
+ assert result["count"] > last_result
+ last_result = result["count"]
+ assert result == {"count": 1}
+ assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}
+
+
class SingleStepActionWithDeletionAsync(SingleStepActionWithDeletion):
async def run_and_update(self, state: State, **run_kwargs) -> Tuple[dict, State]:
return {}, state.wipe(delete=["to_delete"])
@@ -1259,7 +1415,7 @@ async def test_app_a_run_async_and_sync():
assert result["count"] > 20
-def test_stream_result_halt_after_unique():
+def test_stream_result_halt_after_unique_ordered_sequence_id():
action_tracker = ActionTracker()
counter_action = base_streaming_counter.with_name("counter")
counter_action_2 = base_streaming_counter.with_name("counter_2")
@@ -1294,7 +1450,47 @@ def test_stream_result_halt_after_unique():
] # ensure sequence ID is respected
-def test_stream_result_halt_after_single_step():
+async def test_astream_result_halt_after_unique_ordered_sequence_id():
+ action_tracker = ActionTracker()
+ counter_action = base_streaming_counter_async.with_name("counter")
+ counter_action_2 = base_streaming_counter_async.with_name("counter_2")
+ app = Application(
+ actions=[counter_action, counter_action_2],
+ transitions=[
+ Transition(counter_action, counter_action_2, default),
+ ],
+ state=State({"count": 0}),
+ initial_step="counter",
+ adapter_set=LifecycleAdapterSet(action_tracker),
+ partition_key="test",
+ uid="test-123",
+ )
+ action_, streaming_async_container = await app.astream_result(halt_after=["counter_2"])
+ results = [
+ item async for item in streaming_async_container
+ ] # this should just have the intermediate results
+ # results = list(streaming_container)
+ assert len(results) == 10
+ result, state = await streaming_async_container.get()
+ assert result["count"] == state["count"] == 2
+ assert state["tracker"] == [1, 2]
+ assert len(action_tracker.pre_called) == 2
+ assert len(action_tracker.post_called) == 2
+ assert 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 respected
+
+
+def test_stream_result_halt_after_run_through_streaming():
+ """Tests that we can pass through streaming results,
+ fully realize them, then get to the streaming results at the end and return the stream"""
action_tracker = ActionTracker()
counter_action = base_streaming_single_step_counter.with_name("counter")
counter_action_2 = base_streaming_single_step_counter.with_name("counter_2")
@@ -1329,7 +1525,44 @@ def test_stream_result_halt_after_single_step():
] # ensure sequence ID is respected
-def test_stream_result_halt_after_run_through_final_streaming():
+async def test_astream_result_halt_after_run_through_streaming():
+ action_tracker = ActionTracker()
+ counter_action = base_streaming_single_step_counter_async.with_name("counter")
+ counter_action_2 = base_streaming_single_step_counter_async.with_name("counter_2")
+ app = Application(
+ actions=[counter_action, counter_action_2],
+ transitions=[
+ Transition(counter_action, counter_action_2, default),
+ ],
+ state=State({"count": 0}),
+ initial_step="counter",
+ adapter_set=LifecycleAdapterSet(action_tracker),
+ partition_key="test",
+ uid="test-123",
+ )
+ action_, streaming_container = await app.astream_result(halt_after=["counter_2"])
+ results = [
+ item async for item in streaming_container
+ ] # this should just have the intermediate results
+ assert len(results) == 10
+ result, state = await streaming_container.get()
+ assert result["count"] == state["count"] == 2
+ assert state["tracker"] == [1, 2]
+ assert len(action_tracker.pre_called) == 2
+ assert len(action_tracker.post_called) == 2
+ assert 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 respected
+
+
+def test_stream_result_halt_after_run_through_non_streaming():
"""Tests what happens when we have an app that runs through non-streaming
results before hitting a final streaming result specified by halt_after"""
action_tracker = ActionTracker()
@@ -1371,7 +1604,50 @@ def test_stream_result_halt_after_run_through_final_streaming():
) # ensure sequence ID is respected
+async def test_astream_result_halt_after_run_through_non_streaming():
+ action_tracker = ActionTracker()
+ counter_non_streaming = base_counter_action_async.with_name("counter_non_streaming")
+ counter_streaming = base_streaming_single_step_counter_async.with_name("counter_streaming")
+
+ app = Application(
+ actions=[counter_non_streaming, counter_streaming],
+ transitions=[
+ Transition(counter_non_streaming, counter_non_streaming, expr("count < 10")),
+ Transition(counter_non_streaming, counter_streaming, default),
+ ],
+ state=State({"count": 0}),
+ initial_step="counter_non_streaming",
+ adapter_set=LifecycleAdapterSet(action_tracker),
+ partition_key="test",
+ uid="test-123",
+ )
+ action_, async_streaming_container = await app.astream_result(halt_after=["counter_streaming"])
+ results = [
+ item async for item in async_streaming_container
+ ] # this should just have the intermediate results
+ assert len(results) == 10
+ result, state = await async_streaming_container.get()
+ assert result["count"] == state["count"] == 11
+ assert len(action_tracker.pre_called) == 11
+ assert len(action_tracker.post_called) == 11
+ assert set(dict(action_tracker.pre_called).keys()) == {
+ "counter_streaming",
+ "counter_non_streaming",
+ }
+ assert set(dict(action_tracker.post_called).keys()) == {
+ "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 respected
+
+
def test_stream_result_halt_after_run_through_final_non_streaming():
+ """Tests that we can pass through non-streaming results when streaming is called"""
action_tracker = ActionTracker()
counter_non_streaming = base_counter_action.with_name("counter_non_streaming")
counter_final_non_streaming = base_counter_action.with_name("counter_final_non_streaming")
@@ -1411,6 +1687,51 @@ def test_stream_result_halt_after_run_through_final_non_streaming():
) # ensure sequence ID is respected
+async def test_astream_result_halt_after_run_through_final_streaming():
+ """Tests that we can pass through non-streaming results when streaming is called"""
+ action_tracker = ActionTracker()
+ counter_non_streaming = base_counter_action_async.with_name("counter_non_streaming")
+ counter_final_non_streaming = base_counter_action_async.with_name("counter_final_non_streaming")
+
+ app = Application(
+ actions=[counter_non_streaming, counter_final_non_streaming],
+ transitions=[
+ Transition(counter_non_streaming, counter_non_streaming, expr("count < 10")),
+ Transition(counter_non_streaming, counter_final_non_streaming, default),
+ ],
+ state=State({"count": 0}),
+ initial_step="counter_non_streaming",
+ adapter_set=LifecycleAdapterSet(action_tracker),
+ partition_key="test",
+ uid="test-123",
+ )
+ action, streaming_container = await app.astream_result(
+ halt_after=["counter_final_non_streaming"]
+ )
+ results = [
+ item async for item in streaming_container
+ ] # this should just have the intermediate results
+ assert len(results) == 0 # nothing to stream
+ result, state = await streaming_container.get()
+ assert result["count"] == state["count"] == 11
+ assert len(action_tracker.pre_called) == 11
+ assert len(action_tracker.post_called) == 11
+ assert set(dict(action_tracker.pre_called).keys()) == {
+ "counter_non_streaming",
+ "counter_final_non_streaming",
+ }
+ assert set(dict(action_tracker.post_called).keys()) == {
+ "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 respected
+
+
def test_stream_result_halt_before():
action_tracker = ActionTracker()
counter_non_streaming = base_counter_action.with_name("counter_non_streaming")
@@ -1443,6 +1764,42 @@ def test_stream_result_halt_before():
) # ensure sequence ID is respected
+async def test_astream_result_halt_before():
+ action_tracker = ActionTracker()
+ counter_non_streaming = base_counter_action_async.with_name("counter_non_streaming")
+ counter_streaming = base_streaming_single_step_counter_async.with_name("counter_final")
+
+ app = Application(
+ actions=[counter_non_streaming, counter_streaming],
+ transitions=[
+ Transition(counter_non_streaming, counter_non_streaming, expr("count < 10")),
+ Transition(counter_non_streaming, counter_streaming, default),
+ ],
+ state=State({"count": 0}),
+ initial_step="counter_non_streaming",
+ partition_key="test",
+ uid="test-123",
+ adapter_set=LifecycleAdapterSet(action_tracker),
+ )
+ action, streaming_container = await app.astream_result(
+ halt_after=[], halt_before=["counter_final"]
+ )
+ results = [
+ item async for item in streaming_container
+ ] # this should just have the intermediate results
+ assert len(results) == 0 # nothing to stream
+ result, state = await streaming_container.get()
+ assert action.name == "counter_final" # halt before this one
+ assert result is None
+ assert 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 respected
+
+
def test_app_set_state():
counter_action = base_counter_action.with_name("counter")
app = Application(