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 up typing for streaming result containers
elijahbenizzy0baaaf78acc0
diff --git a/burr/core/action.py b/burr/core/action.pyindex 598d2263..288422ba 100644--- a/burr/core/action.py+++ b/burr/core/action.py@@ -6,6 +6,7 @@ import inspectimport sysimport typesimport typing+from collections.abc import AsyncIteratorfrom typing import (Any,AsyncGenerator,@@ -199,6 +200,11 @@ class Action(Function, Reducer, abc.ABC):to display a different source"""return inspect.getsource(self.__class__)+ def input_schema(self) -> Any:+ """Returns the input schema for the action.+ The input schema is a type that can be used to validate the input to the action"""+ return None+def __repr__(self):read_repr = ", ".join(self.reads) if self.reads else "{}"write_repr = ", ".join(self.writes) if self.writes else "{}"@@ -732,7 +738,10 @@ class AsyncStreamingAction(Action, abc.ABC):return True-class StreamingResultContainer(Iterator[dict], Generic[StateType]):+StreamResultType = TypeVar("StreamResultType")+++class StreamingResultContainer(Generic[StateType, StreamResultType], Iterator[StreamResultType]):"""Container for a streaming result. This allows you to:1. Iterate over the result as it comes in@@ -758,13 +767,15 @@ class StreamingResultContainer(Iterator[dict], Generic[StateType]):@staticmethoddef pass_through(- results: dict, final_state: State[StateType]- ) -> "StreamingResultContainer[StateType]":+ results: StreamResultType, final_state: State[StateType]+ ) -> "StreamingResultContainer[StreamResultType, StateType]":"""Instantiates a streaming result container that just passes through the given resultsThis is to be used internally -- it allows us to wrap non-streaming action results in a streamingresult container."""- def empty_generator() -> Generator[Tuple[dict, Optional[State[StateType]]], None, None]:+ def empty_generator() -> (+ Generator[Tuple[StreamResultType, Optional[State[StateType]]], None, None]+ ):yield results, final_statereturn StreamingResultContainer(@@ -798,7 +809,7 @@ class StreamingResultContainer(Iterator[dict], Generic[StateType]):self._result = Noneself._callback_realized = False- def __next__(self):+ def __next__(self) -> StreamResultType:if self._result is not None:# we're done, and we've run through itraise StopIteration@@ -808,7 +819,7 @@ class StreamingResultContainer(Iterator[dict], Generic[StateType]):raise StopIterationreturn result- def __iter__(self):+ def __iter__(self) -> Iterator[StreamResultType]:def gen_fn():try:while True:@@ -836,7 +847,10 @@ class StreamingResultContainer(Iterator[dict], Generic[StateType]):return self._result-class AsyncStreamingResultContainer(typing.AsyncIterator[dict], Generic[StateType]):+class AsyncStreamingResultContainer(+ Generic[StateType, StreamResultType],+ AsyncIterator[StreamResultType],+):"""Container for an async streaming result. This allows you to:1. Iterate over the result as it comes in2. Await the final result/state at the end@@ -863,9 +877,11 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict], Generic[StateTypself,streaming_result_generator: AsyncGeneratorReturnType,initial_state: State[StateType],- process_result: Callable[[dict, State[StateType]], tuple[dict, State[StateType]]],+ process_result: Callable[+ [StreamResultType, State[StateType]], tuple[StreamResultType, State[StateType]]+ ],callback: Callable[- [Optional[dict], State[StateType], Optional[Exception]],+ [Optional[StreamResultType], State[StateType], Optional[Exception]],typing.Coroutine[None, None, None],],):@@ -885,7 +901,7 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict], Generic[StateTypself._result = Noneself._callback_realized = False- async def __anext__(self):+ async def __anext__(self) -> StreamResultType:"""Moves to the next state in the streaming result"""if self._result is not None:# we're done, and we've run through it@@ -896,7 +912,7 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict], Generic[StateTypraise StopAsyncIterationreturn result- def __aiter__(self):+ def __aiter__(self) -> AsyncIterator[StreamResultType]:"""Gives the iterator. Just calls anext, assigning the result in the finally block.Note this may not be perfect due to the complexity of callbacks for async generators,but it works in most cases."""@@ -918,7 +934,7 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict], Generic[StateTyp# return it as `__aiter__` cannot be async/have awaits :/return gen_fn()- async def get(self) -> tuple[Optional[dict], State[StateType]]:+ async def get(self) -> tuple[Optional[StreamResultType], State[StateType]]:# exhaust the generatorasync for _ in self:pass@@ -927,7 +943,7 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict], Generic[StateTyp@staticmethoddef pass_through(- results: dict, final_state: State[StateType]+ results: StreamResultType, final_state: State[StateType]) -> "AsyncStreamingResultContainer[StateType]":"""Creates a streaming result container that just passes through the given results.This is not a public facing API."""@@ -935,10 +951,12 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict], Generic[StateTypasync def just_results() -> AsyncGeneratorReturnType:yield results, final_state- async def empty_callback(result: Optional[dict], state: State, exc: Optional[Exception]):+ async def empty_callback(+ result: Optional[StreamResultType], state: State, exc: Optional[Exception]+ ):pass- return AsyncStreamingResultContainer[StateType](+ return AsyncStreamingResultContainer[StateType, StreamResultType](just_results(), final_state, lambda result, state: (result, state), empty_callback)diff --git a/burr/core/application.py b/burr/core/application.pyindex 0b4c8c77..2cdd2303 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -74,7 +74,7 @@ def _validate_result(result: Any, name: str, schema: ActionSchema = DEFAULT_SCHEresult_type = schema.intermediate_result_type()if not isinstance(result, result_type):raise ValueError(- f"Action {name} returned a non-dict result: {result}. "+ f"Action {name} returned a non-{result_type.__name__} result: {result}. "f"All results must be dictionaries.")@@ -682,6 +682,7 @@ class TracerFactoryContextHook(PreRunStepHook, PostRunStepHook):ApplicationStateType = TypeVar("ApplicationStateType")+StreamResultType = TypeVar("StreamResultType", bound=Union[dict, Any])class Application(Generic[ApplicationStateType]):@@ -1205,9 +1206,9 @@ class Application(Generic[ApplicationStateType]):def stream_result(self,halt_after: list[str],- halt_before: list[str] = None,+ halt_before: Optional[list[str]] = None,inputs: Optional[Dict[str, Any]] = None,- ) -> Tuple[Action, StreamingResultContainer[ApplicationStateType]]:+ ) -> Tuple[Action, StreamingResultContainer[ApplicationStateType, Union[dict, Any]]]:"""Streams a result out.:param halt_after: The list of actions to halt after execution of. It will halt on the first one.@@ -1454,9 +1455,9 @@ class Application(Generic[ApplicationStateType]):async def astream_result(self,halt_after: list[str],- halt_before: list[str] = None,+ halt_before: Optional[list[str]] = None,inputs: Optional[Dict[str, Any]] = None,- ) -> Tuple[Action, AsyncStreamingResultContainer[ApplicationStateType]]:+ ) -> Tuple[Action, AsyncStreamingResultContainer[ApplicationStateType, Union[dict, Any]]]:"""Streams a result out in an asynchronous manner.:param halt_after: The list of actions to halt after execution of. It will halt on the first one.diff --git a/burr/integrations/pydantic.py b/burr/integrations/pydantic.pyindex 6665ab1a..bcc76453 100644--- a/burr/integrations/pydantic.py+++ b/burr/integrations/pydantic.py@@ -362,7 +362,7 @@ def pydantic_streaming_action(schema=PydanticActionSchema(input_type=SubsetInputType,output_type=SubsetOutputType,- intermediate_result_type=dict,+ intermediate_result_type=stream_type_processed,),),)diff --git a/examples/youtube-to-social-media-post/application.py b/examples/youtube-to-social-media-post/application.pyindex 3a7f80c1..a3a0a8e9 100644--- a/examples/youtube-to-social-media-post/application.py+++ b/examples/youtube-to-social-media-post/application.py@@ -9,6 +9,7 @@ from rich.console import Consolefrom youtube_transcript_api import YouTubeTranscriptApifrom burr.core import Application, ApplicationBuilder+from burr.core.action import AsyncStreamingResultContainer, StreamingResultContainerfrom burr.integrations.pydantic import (PydanticTypingSystem,pydantic_action,@@ -289,10 +290,13 @@ def build_streaming_application_async() -> Application[ApplicationState]:async def run_async():console = Console()app = build_streaming_application_async()+action, streaming_container = await app.astream_result(halt_after=["generate_post"],inputs={"youtube_url": "https://www.youtube.com/watch?v=hqutVJyd3TI"},- )+ ) # type: ignore+ streaming_container: AsyncStreamingResultContainer[ApplicationState, SocialMediaPost]+async for post in streaming_container:obj = post.model_dump()console.clear()@@ -305,7 +309,8 @@ if __name__ == "__main__":action, streaming_container = app.stream_result(halt_after=["generate_post"],inputs={"youtube_url": "https://www.youtube.com/watch?v=hqutVJyd3TI"},- )+ ) # type: ignore+ streaming_container: StreamingResultContainer[ApplicationState, SocialMediaPost]for post in streaming_container:obj = post.model_dump()console.clear()