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 typed state support
elijahbenizzyd999f5d3e435
diff --git a/burr/core/__init__.py b/burr/core/__init__.pyindex 3d61c144..c371ac47 100644--- a/burr/core/__init__.py+++ b/burr/core/__init__.py@@ -5,6 +5,7 @@ from burr.core.application import (ApplicationContext,ApplicationGraph,)+from burr.core.graph import Graphfrom burr.core.state import State__all__ = [@@ -20,4 +21,5 @@ __all__ = ["Result","State","when",+ "Graph",]diff --git a/burr/core/action.py b/burr/core/action.pyindex 61381afc..9ab00a86 100644--- a/burr/core/action.py+++ b/burr/core/action.py@@ -13,6 +13,7 @@ from typing import (Coroutine,Dict,Generator,+ Generic,Iterator,List,Optional,@@ -96,9 +97,11 @@ class Function(abc.ABC):f"Inputs to function {self} are invalid. "+ f"Missing the following inputs: {', '.join(missing_inputs)}."if missing_inputs- else "" f"Additional inputs: {','.join(additional_inputs)}."- if additional_inputs- else ""+ else (+ "" f"Additional inputs: {','.join(additional_inputs)}."+ if additional_inputs+ else ""+ ))def is_async(self) -> bool:@@ -493,7 +496,7 @@ class SingleStepAction(Action, abc.ABC):# the following exist to share implementation between FunctionBasedStreamingAction and FunctionBasedAction# TODO -- think through the class hierarchy to simplify, for now this is OK-def _get_inputs(bound_params: dict, fn: Callable) -> tuple[list[str], list[str]]:+def get_inputs(bound_params: dict, fn: Callable) -> tuple[list[str], list[str]]:sig = inspect.signature(fn)required_inputs, optional_inputs = [], []for param_name, param in sig.parameters.items():@@ -507,9 +510,7 @@ def _get_inputs(bound_params: dict, fn: Callable) -> tuple[list[str], list[str]]return required_inputs, optional_inputs-FunctionBasedActionType = TypeVar(- "FunctionBasedActionType", bound=Union["FunctionBasedAction", "FunctionBasedStreamingAction"]-)+FunctionBasedActionType = Union["FunctionBasedAction", "FunctionBasedStreamingAction"]class FunctionBasedAction(SingleStepAction):@@ -520,21 +521,33 @@ class FunctionBasedAction(SingleStepAction):fn: Callable,reads: List[str],writes: List[str],- bound_params: dict = None,+ bound_params: Optional[dict] = None,+ input_spec: Optional[tuple[list[str], list[str]]] = None,+ originating_fn: Optional[Callable] = None,):"""Instantiates a function-based action with the given function, reads, and writes.The function must take in a state and return a tuple of (result, new_state).- :param fn:- :param reads:- :param writes:+ :param fn: Function to run+ :param reads: Keys that the function reads from the state+ :param writes: Keys that the function writes to the state+ :param bound_params: Prior bound parameters+ :param input_spec: Specification for inputs. Will derive from function if not provided."""super(FunctionBasedAction, self).__init__()+ self._originating_fn = originating_fn if originating_fn is not None else fnself._fn = fnself._reads = readsself._writes = writesself._bound_params = bound_params if bound_params is not None else {}- self._inputs = _get_inputs(self._bound_params, self._fn)+ self._inputs = (+ get_inputs(self._bound_params, self._fn)+ if input_spec is None+ else (+ [item for item in input_spec[0] if item not in self._bound_params],+ [item for item in input_spec[1] if item not in self._bound_params],+ )+ )@propertydef fn(self) -> Callable:@@ -562,7 +575,11 @@ class FunctionBasedAction(SingleStepAction)::return:"""return FunctionBasedAction(- self._fn, self._reads, self._writes, {**self._bound_params, **kwargs}+ self._fn,+ self._reads,+ self._writes,+ {**self._bound_params, **kwargs},+ input_spec=self._inputs,)def run_and_update(self, state: State, **run_kwargs) -> tuple[dict, State]:@@ -573,10 +590,12 @@ class FunctionBasedAction(SingleStepAction):def get_source(self) -> str:"""Return the source of the code for this action."""- return inspect.getsource(self._fn)+ return inspect.getsource(self._originating_fn)-StreamType = Tuple[dict, Optional[State]]+StateType = TypeVar("StateType")++StreamType = Tuple[dict, Optional[State[StateType]]]GeneratorReturnType = Generator[StreamType, None, None]AsyncGeneratorReturnType = AsyncGenerator[StreamType, None]@@ -590,7 +609,7 @@ class StreamingAction(Action, abc.ABC):they run in multiple passes (run -> update)"""@abc.abstractmethod- def stream_run(self, state: State, **run_kwargs) -> Generator[dict, None, None]:+ def stream_run(self, state: State[StateType], **run_kwargs) -> Generator[dict, None, None]:"""Streaming action ``stream_run`` is different than standard action run. It:1. streams in an intermediate result (the dict output)2. yields the final result at the end@@ -617,7 +636,7 @@ class StreamingAction(Action, abc.ABC):"""pass- def run(self, state: State, **run_kwargs) -> dict:+ def run(self, state: State[StateType], **run_kwargs) -> dict:"""Runs the streaming action through to completion."""gen = self.stream_run(state, **run_kwargs)last_result = None@@ -636,7 +655,7 @@ class AsyncStreamingAction(Action, abc.ABC):Note this is the "multi-step" variant, in which run/update are separate."""@abc.abstractmethod- async def stream_run(self, state: State, **run_kwargs) -> AsyncGenerator[dict, None]:+ async def stream_run(self, state, **run_kwargs) -> AsyncGenerator[dict, None]:"""Asynchronous streaming action ``stream_run`` is different than the standard action run. It:1. streams in an intermediate result (the dict output)2. yields the final result at the end@@ -663,7 +682,7 @@ class AsyncStreamingAction(Action, abc.ABC):"""pass- async def run(self, state: State, **run_kwargs) -> dict:+ async def run(self, state: State[StateType], **run_kwargs) -> dict:"""Runs the streaming action through to completion.Returns the final result. This is used if we want a streaming actionas an intermediate.@@ -686,7 +705,7 @@ class AsyncStreamingAction(Action, abc.ABC):return True-class StreamingResultContainer(Iterator[dict]):+class StreamingResultContainer(Iterator[dict], Generic[StateType]):"""Container for a streaming result. This allows you to:1. Iterate over the result as it comes in@@ -711,12 +730,14 @@ class StreamingResultContainer(Iterator[dict]):"""@staticmethod- def pass_through(results: dict, final_state: State) -> "StreamingResultContainer":+ def pass_through(+ results: dict, final_state: State[StateType]+ ) -> "StreamingResultContainer[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() -> GeneratorReturnType:+ def empty_generator() -> Generator[Tuple[dict, Optional[State[StateType]]], None, None]:yield results, final_statereturn StreamingResultContainer(@@ -729,7 +750,7 @@ class StreamingResultContainer(Iterator[dict]):def __init__(self,streaming_result_generator: GeneratorReturnType,- initial_state: State,+ initial_state: State[StateType],process_result: Callable[[dict, State], tuple[dict, State]],callback: Callable[[Optional[dict], State, Optional[Exception]], None],):@@ -788,7 +809,7 @@ class StreamingResultContainer(Iterator[dict]):return self._result-class AsyncStreamingResultContainer(typing.AsyncIterator[dict]):+class AsyncStreamingResultContainer(typing.AsyncIterator[dict], Generic[StateType]):"""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@@ -814,10 +835,11 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict]):def __init__(self,streaming_result_generator: AsyncGeneratorReturnType,- initial_state: State,- process_result: Callable[[dict, State], tuple[dict, State]],+ initial_state: State[StateType],+ process_result: Callable[[dict, State[StateType]], tuple[dict, State[StateType]]],callback: Callable[- [Optional[dict], State, Optional[Exception]], typing.Coroutine[None, None, None]+ [Optional[dict], State[StateType], Optional[Exception]],+ typing.Coroutine[None, None, None],],):"""Initializes an async streaming result container. User will never call directly.@@ -869,7 +891,7 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict]):# return it as `__aiter__` cannot be async/have awaits :/return gen_fn()- async def get(self) -> tuple[Optional[dict], State]:+ async def get(self) -> tuple[Optional[dict], State[StateType]]:# exhaust the generatorasync for _ in self:pass@@ -877,7 +899,9 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict]):return self._result@staticmethod- def pass_through(results: dict, final_state: State) -> "AsyncStreamingResultContainer":+ def pass_through(+ results: dict, 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."""@@ -887,7 +911,7 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict]):async def empty_callback(result: Optional[dict], state: State, exc: Optional[Exception]):pass- return AsyncStreamingResultContainer(+ return AsyncStreamingResultContainer[StateType](just_results(), final_state, lambda result, state: (result, state), empty_callback)@@ -954,7 +978,9 @@ class FunctionBasedStreamingAction(SingleStepStreamingAction):],reads: List[str],writes: List[str],- bound_params: dict = None,+ bound_params: Optional[dict] = None,+ input_spec: Optional[tuple[list[str], list[str]]] = None,+ originating_fn: Optional[Callable] = 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).@@ -968,6 +994,15 @@ class FunctionBasedStreamingAction(SingleStepStreamingAction):self._reads = readsself._writes = writesself._bound_params = bound_params if bound_params is not None else {}+ self._inputs = (+ get_inputs(self._bound_params, self._fn)+ if input_spec is None+ else (+ [item for item in input_spec[0] if item not in self._bound_params],+ [item for item in input_spec[1] if item not in self._bound_params],+ )+ )+ self._originating_fn = originating_fn if originating_fn is not None else fnasync def _a_stream_run_and_update(self, state: State, **run_kwargs@@ -1005,12 +1040,17 @@ class FunctionBasedStreamingAction(SingleStepStreamingAction)::return:"""return FunctionBasedStreamingAction(- self._fn, self._reads, self._writes, {**self._bound_params, **kwargs}+ self._fn,+ self._reads,+ self._writes,+ {**self._bound_params, **kwargs},+ input_spec=self._inputs,+ originating_fn=self._originating_fn,)@propertydef inputs(self) -> tuple[list[str], list[str]]:- return _get_inputs(self._bound_params, self._fn)+ return self._inputs@propertydef fn(self) -> Union[StreamingFn, StreamingFnAsync]:@@ -1021,7 +1061,7 @@ class FunctionBasedStreamingAction(SingleStepStreamingAction):def get_source(self) -> str:"""Return the source of the code for this action"""- return inspect.getsource(self._fn)+ return inspect.getsource(self._originating_fn)C = TypeVar("C", bound=Callable) # placeholder for any Callablediff --git a/burr/core/application.py b/burr/core/application.pyindex 6ae01f7d..dd13d968 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -1,3 +1,5 @@+from __future__ import annotations+import contextvarsimport dataclassesimport functools@@ -13,6 +15,7 @@ from typing import (Callable,Dict,Generator,+ Generic,List,Literal,Optional,@@ -41,6 +44,7 @@ from burr.core.action import (from burr.core.graph import Graph, GraphBuilderfrom burr.core.persistence import BaseStateLoader, BaseStateSaverfrom burr.core.state import State+from burr.core.typing import DictBasedTypingSystemfrom burr.core.validation import BASE_ERROR_MESSAGEfrom burr.lifecycle.base import ExecuteMethod, LifecycleAdapter, PostRunStepHook, PreRunStepHookfrom burr.lifecycle.internal import LifecycleAdapterSet@@ -48,6 +52,7 @@ from burr.visibility import tracingfrom burr.visibility.tracing import tracer_factory_context_varif TYPE_CHECKING:+ from burr.core.typing import TypingSystemfrom burr.tracking.base import TrackingClientlogger = logging.getLogger(__name__)@@ -55,6 +60,9 @@ logger = logging.getLogger(__name__)PRIOR_STEP = "__PRIOR_STEP"SEQUENCE_ID = "__SEQUENCE_ID"+StateType = TypeVar("StateType")+StateTypeToSet = TypeVar("StateTypeToSet")+def _validate_result(result: dict, name: str) -> None:if not isinstance(result, dict):@@ -292,7 +300,8 @@ def _run_single_step_streaming_action(f"Action {action.name} did not return a state update. For streaming 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)+ # TODO -- get this back in and use the action's schema (still not set) to validate the result...+ # _validate_result(result, action.name)_validate_reducer_writes(action, state_update, action.name)yield result, state_update@@ -344,7 +353,8 @@ async def _arun_single_step_streaming_action(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)+ # TODO -- add back in validation when we have a schema+ # _validate_result(result, action.name)_validate_reducer_writes(action, state_update, action.name)# TODO -- add guard against zero-length streamyield result, state_update@@ -664,11 +674,14 @@ class TracerFactoryContextHook(PreRunStepHook, PostRunStepHook):del self.token_pointer_map[(app_id, sequence_id)]-class Application:+ApplicationStateType = TypeVar("ApplicationStateType")+++class Application(Generic[ApplicationStateType]):def __init__(self,graph: Graph,- state: State,+ state: State[ApplicationStateType],partition_key: Optional[str],uid: str,entrypoint: str,@@ -824,7 +837,9 @@ class Application:in your graph, but this will do the trick if you need it!"""self._set_state(self._state.wipe(delete=[PRIOR_STEP]))- def _update_internal_state_value(self, new_state: State, next_action: Action) -> State:+ def _update_internal_state_value(+ self, new_state: State[ApplicationStateType], next_action: Action+ ) -> State[ApplicationStateType]:"""Updates the internal state values of the new state."""new_state = new_state.update(**{@@ -889,7 +904,9 @@ class Application:# @telemetry.capture_function_usage# ditto with step()@_call_execute_method_pre_post(ExecuteMethod.astep)- async def astep(self, inputs: Dict[str, Any] = None) -> Optional[Tuple[Action, dict, State]]:+ async def astep(+ self, inputs: Optional[Dict[str, Any]] = None+ ) -> Optional[Tuple[Action, dict, State[ApplicationStateType]]]:"""Asynchronous version of step.:param inputs: Inputs to the action -- this is if this action@@ -1022,7 +1039,7 @@ class Application:halt_after: list[str],prior_action: Optional[Action],result: Optional[dict],- ) -> Tuple[Optional[Action], Optional[dict], State]:+ ) -> Tuple[Optional[Action], Optional[dict], State[ApplicationStateType]]:"""Utility function to decide what to return for iterate/arun. Note that run() will delegate to the return value ofiterate, whereas arun cannot delegate to the return value of aiterate (as async generators cannot return a value).We put the code centrally to clean up the logic.@@ -1057,7 +1074,11 @@ class Application:halt_before: list[str] = None,halt_after: list[str] = None,inputs: Optional[Dict[str, Any]] = None,- ) -> Generator[Tuple[Action, dict, State], None, Tuple[Action, Optional[dict], State]]:+ ) -> Generator[+ Tuple[Action, dict, State[ApplicationStateType]],+ None,+ Tuple[Action, Optional[dict], State[ApplicationStateType]],+ ]:"""Returns a generator that calls step() in a row, enabling you to see the stateof the system as it updates. Note this returns a generator, and also the final result(for convenience).@@ -1095,7 +1116,7 @@ class Application:halt_before: list[str] = None,halt_after: list[str] = None,inputs: Optional[Dict[str, Any]] = None,- ) -> AsyncGenerator[Tuple[Action, dict, State], None]:+ ) -> AsyncGenerator[Tuple[Action, dict, State[ApplicationStateType]], None]:"""Returns a generator that calls step() in a row, enabling you to see the stateof the system as it updates. This is the asynchronous version so it has no capability of t@@ -1125,7 +1146,7 @@ class Application:halt_before: list[str] = None,halt_after: list[str] = None,inputs: Optional[Dict[str, Any]] = None,- ) -> Tuple[Action, Optional[dict], State]:+ ) -> Tuple[Action, Optional[dict], State[ApplicationStateType]]:"""Runs your application through until completion. Doesnot give access to the state along the way -- if you want that, use iterate().@@ -1179,7 +1200,7 @@ class Application:halt_after: list[str],halt_before: list[str] = None,inputs: Optional[Dict[str, Any]] = None,- ) -> Tuple[Action, StreamingResultContainer]:+ ) -> Tuple[Action, StreamingResultContainer[ApplicationStateType]]:"""Streams a result out.:param halt_after: The list of actions to halt after execution of. It will halt on the first one.@@ -1428,7 +1449,7 @@ class Application:halt_after: list[str],halt_before: list[str] = None,inputs: Optional[Dict[str, Any]] = None,- ) -> Tuple[Action, AsyncStreamingResultContainer]:+ ) -> Tuple[Action, AsyncStreamingResultContainer[ApplicationStateType]]:"""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.@@ -1680,7 +1701,7 @@ class Application:sequence_id=self.sequence_id,exception=e,)- await call_execute_method_wrapper.acall_post(self, None, e)+ await call_execute_method_wrapper.acall_post(self, e)await self._adapter_set.call_all_lifecycle_hooks_sync_and_async("post_end_stream",action=next_action.name,@@ -1726,13 +1747,13 @@ class Application:**engine_kwargs,)- def _set_state(self, new_state: State):+ def _set_state(self, new_state: State[ApplicationStateType]):self._state = new_statedef get_next_action(self) -> Optional[Action]:return self._graph.get_next_node(self._state.get(PRIOR_STEP), self._state, self.entrypoint)- def update_state(self, new_state: State):+ def update_state(self, new_state: State[ApplicationStateType]):"""Updates state -- this is meant to be called if you need to doanything with the state. For example:1. Reset it (after going through a loop)@@ -1744,7 +1765,7 @@ class Application:self._state = new_state@property- def state(self) -> State:+ def state(self) -> State[ApplicationStateType]:"""Gives the state. Recall that state is purely immutable-- anything you do with this state will not be persisted unless yousubsequently call update_state.@@ -1837,7 +1858,7 @@ class Application:return self._partition_key@property- def builder(self) -> Optional["ApplicationBuilder"]:+ def builder(self) -> Optional["ApplicationBuilder[ApplicationStateType]"]:"""Returns the application builder that was used to build this application.Note that this asusmes the application was built using the builder. Otherwise,@@ -1873,10 +1894,10 @@ def _validate_start(start: Optional[str], actions: Set[str]):)-class ApplicationBuilder:+class ApplicationBuilder(Generic[StateType]):def __init__(self):self.start = None- self.state: Optional[State] = None+ self.state: Optional[State[StateType]] = Noneself.lifecycle_adapters: List[LifecycleAdapter] = list()self.app_id: str = str(uuid.uuid4())self.partition_key: Optional[str] = None@@ -1894,10 +1915,11 @@ class ApplicationBuilder:self.tracker = Noneself.graph_builder = Noneself.prebuilt_graph = None+ self.typing_system = Nonedef with_identifiers(self, app_id: str = None, partition_key: str = None, sequence_id: int = None- ) -> "ApplicationBuilder":+ ) -> "ApplicationBuilder[StateType]":"""Assigns various identifiers to the application. This is used for tracking, persistence, etc...:param app_id: Application ID -- this will be assigned to a uuid if not set.@@ -1915,7 +1937,21 @@ class ApplicationBuilder:self.sequence_id = sequence_idreturn self- def with_state(self, **kwargs) -> "ApplicationBuilder":+ def with_typing(+ self, typing_system: TypingSystem[StateTypeToSet]+ ) -> "ApplicationBuilder[StateTypeToSet]":+ """Sets the typing system for the application. This is used to enforce typing on the state.++ :param typing_system: Typing system to use+ :return: The application builder for future chaining.+ """+ if typing_system is not None:+ self.typing_system = typing_system+ return self # type: ignore++ def with_state(+ self, state: Optional[Union[State, StateTypeToSet]] = None, **kwargs+ ) -> "ApplicationBuilder[StateType]":"""Sets initial values in the state. If you want to load from a prior state,you can do so here and pass the values in.@@ -1930,13 +1966,30 @@ class ApplicationBuilder:"the .initialize_from() API. Either allow the persister to set the ""state, or set the state manually.")- if self.state is not None:+ if state is not None:+ if self.state is not None:+ raise ValueError(+ BASE_ERROR_MESSAGE+ + "State items have already been set -- you cannot use the type-based API as well."+ " Either set state with with_state(**kwargs) or pass in a state/typed object."+ )+ if isinstance(state, State):+ self.state = state+ elif self.typing_system is not None:+ self.state = self.typing_system.construct_state(state)+ else:+ raise ValueError(+ BASE_ERROR_MESSAGE+ + "You have not set a typing system, and you are passing in a typed state object."+ " Please set a typing system using with_typing before doing so."+ )+ elif self.state is not None:self.state = self.state.update(**kwargs)else:self.state = State(kwargs)return self- def with_graph(self, graph: Graph) -> "ApplicationBuilder":+ def with_graph(self, graph: Graph) -> "ApplicationBuilder[StateType]":"""Adds a prebuilt graph -- this is an alternative to using the with_actions and with_transitions methods.While you will likely use with_actions and with_transitions, you may want this in a few cases:@@ -1969,7 +2022,7 @@ class ApplicationBuilder:if self.graph_builder is None:self.graph_builder = GraphBuilder()- def with_entrypoint(self, action: str) -> "ApplicationBuilder":+ def with_entrypoint(self, action: str) -> "ApplicationBuilder[StateType]":"""Adds an entrypoint to the application. This is the action that will be run first.This can only be called once.@@ -1988,7 +2041,7 @@ class ApplicationBuilder:def with_actions(self, *action_list: Union[Action, Callable], **action_dict: Union[Action, Callable]- ) -> "ApplicationBuilder":+ ) -> "ApplicationBuilder[StateType]":"""Adds an action to the application. The actions are granted names (using the with_name)method post-adding, using the kw argument. If it already has a name (or you wish to use the function name, raw, andit is a function-based-action), then you can use the *args* parameter. This is the only supported way to add actions.@@ -2007,7 +2060,7 @@ class ApplicationBuilder:*transitions: Union[Tuple[Union[str, list[str]], str], Tuple[Union[str, list[str]], str, Condition]],- ) -> "ApplicationBuilder":+ ) -> "ApplicationBuilder[StateType]":"""Adds transitions to the application. Transitions are specified as tuples of either:1. (from, to, condition)2. (from, to) -- condition is set to DEFAULT (which is a fallback)@@ -2024,7 +2077,7 @@ class ApplicationBuilder:self.graph_builder = self.graph_builder.with_transitions(*transitions)return self- def with_hooks(self, *adapters: LifecycleAdapter) -> "ApplicationBuilder":+ def with_hooks(self, *adapters: LifecycleAdapter) -> "ApplicationBuilder[StateType]":"""Adds a lifecycle adapter to the application. This is a way to add hooks to the application so thatthey are run at the appropriate times. You can use this to synchronize state out, log results, etc...@@ -2093,7 +2146,7 @@ class ApplicationBuilder:fork_from_app_id: str = None,fork_from_partition_key: str = None,fork_from_sequence_id: int = None,- ) -> "ApplicationBuilder":+ ) -> "ApplicationBuilder[StateType]":"""Initializes the application we will build from some prior state object.Note (1) that you can *either* call this or use `with_state` and `with_entrypoint`.@@ -2133,7 +2186,7 @@ class ApplicationBuilder:def with_state_persister(self, persister: Union[BaseStateSaver, LifecycleAdapter], on_every: str = "step"- ) -> "ApplicationBuilder":+ ) -> "ApplicationBuilder[StateType]":"""Adds a state persister to the application. This is a way to persist state out to a database, file, etc...at the specified interval. This is one of two options:@@ -2156,7 +2209,7 @@ class ApplicationBuilder:def with_spawning_parent(self, app_id: str, sequence_id: int, partition_key: Optional[str] = None- ) -> "ApplicationBuilder":+ ) -> "ApplicationBuilder[StateType]":"""Sets the 'spawning' parent application that created this app.This is used for tracking purposes. Doing this creates a parent/child relationship.There can be many spawned children from a single sequence ID (just as there can be many forks of an app).@@ -2251,14 +2304,14 @@ class ApplicationBuilder:raise ValueError(BASE_ERROR_MESSAGE+ "You must set the graph using with_graph, or use with_entrypoint, with_actions, and with_transitions"- "to build the graph."+ " to build the graph.")if self.graph_builder is not None:return self.graph_builder.build()return self.prebuilt_graph- @telemetry.capture_function_usage- def build(self) -> Application:+ # @telemetry.capture_function_usage+ def build(self) -> Application[StateType]:"""Builds the application.This function is a bit messy as we iron out the exact logic and rigor we want around things.@@ -2274,6 +2327,10 @@ class ApplicationBuilder:self._load_from_persister()graph = self._get_built_graph()_validate_start(self.start, {action.name for action in graph.actions})+ typing_system: TypingSystem[StateType] = (+ self.typing_system if self.typing_system is not None else DictBasedTypingSystem()+ ) # type: ignore+ self.state = self.state.with_typing_system(typing_system=typing_system)return Application(graph=graph,state=self.state,@@ -2283,19 +2340,38 @@ class ApplicationBuilder:entrypoint=self.start,adapter_set=LifecycleAdapterSet(*self.lifecycle_adapters),builder=self,- fork_parent_pointer=burr_types.ParentPointer(- app_id=self.fork_from_app_id,- partition_key=self.fork_from_partition_key,- sequence_id=self.fork_from_sequence_id,- )- if self.loaded_from_fork- else None,+ fork_parent_pointer=(+ burr_types.ParentPointer(+ app_id=self.fork_from_app_id,+ partition_key=self.fork_from_partition_key,+ sequence_id=self.fork_from_sequence_id,+ )+ if self.loaded_from_fork+ else None+ ),tracker=self.tracker,- spawning_parent_pointer=burr_types.ParentPointer(- app_id=self.spawn_from_app_id,- partition_key=self.spawn_from_partition_key,- sequence_id=self.spawn_from_sequence_id,- )- if self.spawn_from_app_id is not None- else None,+ spawning_parent_pointer=(+ burr_types.ParentPointer(+ app_id=self.spawn_from_app_id,+ partition_key=self.spawn_from_partition_key,+ sequence_id=self.spawn_from_sequence_id,+ )+ if self.spawn_from_app_id is not None+ else None+ ),)+++if __name__ == "__main__":+ import pydantic++ class Foo(pydantic.BaseModel):+ a: int+ b: str++ from burr.integrations import pydantic++ app = ApplicationBuilder().with_typing(pydantic.PydanticTypingSystem(Foo)).build()++ _, _, foo = app.run(inputs={"a": 1, "b": "hello"})+ mod = foo.datadiff --git a/burr/core/state.py b/burr/core/state.pyindex 62c9a583..67037b9f 100644--- a/burr/core/state.py+++ b/burr/core/state.py@@ -4,9 +4,10 @@ import dataclassesimport importlibimport inspectimport logging-from typing import Any, Callable, Dict, Iterator, Mapping, Union+from typing import Any, Callable, Dict, Generic, Iterator, Mapping, Optional, TypeVar, Unionfrom burr.core import serde+from burr.core.typing import DictBasedTypingSystem, TypingSystemlogger = logging.getLogger(__name__)@@ -142,7 +143,7 @@ class AppendFields(StateDelta):if key not in inputs:inputs[key] = []if not isinstance(inputs[key], list):- raise ValueError(f"Cannot append to non-list value {key}={inputs[self.key]}")+ raise ValueError(f"Cannot append to non-list value {key}={inputs[key]}")inputs[key].append(value)def validate(self, input_state: Dict[str, Any]):@@ -211,22 +212,51 @@ class DeleteField(StateDelta):inputs.pop(key, None)-class State(Mapping):+StateType = TypeVar("StateType", bound=Union[Dict[str, Any], Any])+AssignedStateType = TypeVar("AssignedStateType")+++class State(Mapping, Generic[StateType]):"""An immutable state object. This is the only way to interact with state in Burr."""- def __init__(self, initial_values: Dict[str, Any] = None):+ def __init__(+ self,+ initial_values: Optional[Dict[str, Any]] = None,+ typing_system: Optional[TypingSystem[StateType]] = None,+ ):if initial_values is None:initial_values = dict()+ self._typing_system = (+ typing_system if typing_system is not None else DictBasedTypingSystem() # type: ignore+ )self._state = initial_values- def apply_operation(self, operation: StateDelta) -> "State":+ @property+ def typing_system(self) -> TypingSystem[StateType]:+ return self._typing_system++ def with_typing_system(+ self, typing_system: TypingSystem[AssignedStateType]+ ) -> "State[AssignedStateType]":+ """Copies state with a specific typing system"""+ return State(self._state, typing_system=typing_system)++ @property+ def data(self) -> StateType:+ return self._typing_system.construct_data(self) # type: ignore++ def apply_operation(self, operation: StateDelta) -> "State[StateType]":"""Applies a given operation to the state, returning a new state"""- new_state = copy.deepcopy(self._state) # TODO -- restrict to just the read keys++ # Moved to copy.copy instead of copy.deepcopy+ # TODO -- just copy the ones that have changed+ # And if they can't be copied then we use the same ones...+ new_state = copy.copy(self._state) # TODO -- restrict to just the read keysoperation.validate(new_state)operation.apply_mutate(new_state) # todo -- validate that the write keys are the only different ones- return State(new_state)+ return State(new_state, typing_system=self._typing_system)def get_all(self) -> Dict[str, Any]:"""Returns the entire state, realize as a dictionary. This is a copy."""@@ -251,7 +281,7 @@ class State(Mapping):return {k: _serialize(k, v, **kwargs) for k, v in _dict.items()}@classmethod- def deserialize(cls, json_dict: dict, **kwargs) -> "State":+ def deserialize(cls, json_dict: dict, **kwargs) -> "State[StateType]":"""Converts a dictionary representing a JSON object back into a state"""def _deserialize(k, v: Union[str, dict], **extrakwargs) -> Callable:@@ -262,7 +292,7 @@ class State(Mapping):return State({k: _deserialize(k, v, **kwargs) for k, v in json_dict.items()})- def update(self, **updates: Any) -> "State":+ def update(self, **updates: Any) -> "State[StateType]":"""Updates the state with a set of key-value pairsDoes an upsert operation (if the keys exist their value will be overwritten,otherwise they will be created)@@ -277,7 +307,7 @@ class State(Mapping):"""return self.apply_operation(SetFields(updates))- def append(self, **updates: Any) -> "State":+ def append(self, **updates: Any) -> "State[StateType]":"""Appends to the state with a set of key-value pairs. Each onemust correspond to a list-like object, or an error will be raised.@@ -295,7 +325,7 @@ class State(Mapping):return self.apply_operation(AppendFields(updates))- def increment(self, **updates: int) -> "State":+ def increment(self, **updates: int) -> "State[StateType]":"""Increments the state with a set of key-value pairs. Each onemust correspond to an integer, or an error will be raised.@@ -304,7 +334,7 @@ class State(Mapping):""" ""return self.apply_operation(IncrementFields(updates))- def wipe(self, delete: list[str] = None, keep: list[str] = None):+ def wipe(self, delete: Optional[list[str]] = None, keep: Optional[list[str]] = None):"""Wipes the state, either by deleting the keys in delete and keeping everything elseor keeping the keys in keep. and deleting everything else. If you pass nothing init will delete the whole thing.@@ -324,14 +354,17 @@ class State(Mapping):fields_to_delete = [key for key in self._state if key not in keep]return self.apply_operation(DeleteField(fields_to_delete))- def merge(self, other: "State") -> "State":+ def merge(self, other: "State") -> "State[StateType]":"""Merges two states together, overwriting the values in selfwith those in other."""- return State({**self.get_all(), **other.get_all()})+ return State({**self.get_all(), **other.get_all()}, self.typing_system)- def subset(self, *keys: str, ignore_missing: bool = True) -> "State":+ def subset(self, *keys: str, ignore_missing: bool = True) -> "State[StateType]":"""Returns a subset of the state, with only the given keys"""- return State({key: self[key] for key in keys if key in self or not ignore_missing})+ return State(+ {key: self[key] for key in keys if key in self or not ignore_missing},+ self.typing_system,+ )def __getitem__(self, __k: str) -> Any:return self._state[__k]diff --git a/burr/core/typing.py b/burr/core/typing.pynew file mode 100644index 00000000..c0c9cdb3--- /dev/null+++ b/burr/core/typing.py@@ -0,0 +1,89 @@+from __future__ import annotations++import abc+from typing import TYPE_CHECKING, Generic, Type, TypeVar++BaseType = TypeVar("BaseType")+# SpecificType = TypeVar('SpecificType', bound=BaseType)++if TYPE_CHECKING:+ from burr.core import Action, Graph, State++try:+ from typing import Self+except ImportError:+ Self = "TypingSystem"+++class TypingSystem(abc.ABC, Generic[BaseType]):+ @abc.abstractmethod+ def state_type(self) -> Type[BaseType]:+ """Gives the type that represents the state of the+ application at any given time. Note that this must have+ adequate support for Optionals (E.G. non-required values).++ :return:+ """++ @abc.abstractmethod+ def state_pre_action_run_type(self, action: Action, graph: Graph) -> Type[BaseType]:+ """Gives the type that represents the state after an action has completed.+ Note that this could be smart -- E.g. it should have all possible upstream+ types filled in.++ :param action:+ :return:+ """++ @abc.abstractmethod+ def state_post_action_run_type(self, action: Action, graph: Graph) -> Type[BaseType]:+ """Gives the type that represents the state after an action has completed.+ Note that this could be smart -- E.g. it should have all possible upstream+ types filled in.++ :param action:+ :return:+ """++ def validate_state(self, state: State) -> None:+ """Validates the state to ensure it is valid.++ :param state:+ :return:+ """++ @abc.abstractmethod+ def construct_data(self, state: State[BaseType]) -> BaseType:+ """Constructs a type based on the arguments passed in.++ :param kwargs:+ :return:+ """++ @abc.abstractmethod+ def construct_state(self, data: BaseType) -> State[BaseType]:+ """Constructs a state based on the arguments passed in.++ :param kwargs:+ :return:+ """+++class DictBasedTypingSystem(TypingSystem[dict]):+ """Effectively a no-op. State is backed by a dictionary, which allows every state item+ to... be a dictionary."""++ def state_type(self) -> Type[dict]:+ return dict++ def state_pre_action_run_type(self, action: Action, graph: Graph) -> Type[dict]:+ return dict++ def state_post_action_run_type(self, action: Action, graph: Graph) -> Type[dict]:+ return dict++ def construct_data(self, state: State[dict]) -> dict:+ return state.get_all()++ def construct_state(self, data: dict) -> State[dict]:+ return State(data, typing_system=self)diff --git a/burr/core/validation.py b/burr/core/validation.pyindex e1e95514..16baf2a0 100644--- a/burr/core/validation.py+++ b/burr/core/validation.py@@ -12,5 +12,5 @@ def assert_set(value: Optional[Any], field: str, method: str):if value is None:raise ValueError(BASE_ERROR_MESSAGE- + f"Must set `{field}` before building application! Do so with ApplicationBuilder.{method}"+ + f"Must call `{method}` before building application! Do so with ApplicationBuilder.")diff --git a/burr/integrations/pydantic.py b/burr/integrations/pydantic.pynew file mode 100644index 00000000..22a82a48--- /dev/null+++ b/burr/integrations/pydantic.py@@ -0,0 +1,360 @@+from __future__ import annotations++import copy+import inspect+import types+from typing import (+ AsyncGenerator,+ Awaitable,+ Callable,+ Generator,+ List,+ Optional,+ ParamSpec,+ Tuple,+ Type,+ TypeVar,+ Union,+)++import pydantic+from pydantic_core import PydanticUndefined++from burr.core import Action, Graph, State+from burr.core.action import FunctionBasedAction, FunctionBasedStreamingAction, bind, get_inputs+from burr.core.typing import TypingSystem++Inputs = ParamSpec("Inputs")++PydanticActionFunction = Callable[..., Union[pydantic.BaseModel, Awaitable[pydantic.BaseModel]]]+++def model_to_dict(model: pydantic.BaseModel, include: Optional[List[str]] = None) -> dict:+ """Utility function to convert a pydantic model to a dictionary."""+ keys = model.model_fields.keys()+ keys = keys if include is None else [item for item in include if item in model.model_fields]+ return {key: getattr(model, key) for key in keys}+++ModelType = TypeVar("ModelType", bound=pydantic.BaseModel)+++def subset_model(+ model: Type[ModelType],+ fields: List[str],+ force_optional_fields: List[str],+ model_name_suffix: str,+) -> Type[ModelType]:+ """Creates a new pydantic model that is a subset of the original model.+ This is just to make it more efficient, as we can dynamically alter pydantic models++ :param fields: Fields that we want to include in the new model.+ :param force_optional_fields: Fields that we want to include in the new model, but that will always be optional.+ :param model: The model type to subset.+ :param model_name_suffix: The suffix to add to the model name.+ :return: The new model type.+ """+ new_fields = {}++ for name, field_info in model.model_fields.items():+ if name in fields:+ # copy directly+ # TODO -- handle cross-field validation+ new_fields[name] = (field_info.annotation, field_info)+ elif name in force_optional_fields:+ new_field_info = copy.deepcopy(field_info)+ if new_field_info.default_factory is None and (+ new_field_info.default is PydanticUndefined+ ):+ # in this case we can set to None+ new_field_info.default = None+ annotation = field_info.annotation+ if annotation is not None:+ new_field_info.annotation = Optional[annotation] # type: ignore+ new_fields[name] = (new_field_info.annotation, new_field_info)+ return pydantic.create_model(model.__name__ + model_name_suffix, **new_fields) # type: ignore+++def merge_to_state(model: pydantic.BaseModel, write_keys: List[str], state: State) -> State:+ """Merges a pydantic model that is a subset of the new state back into the state+ TODO -- implement+ TODO -- consider validating that the entire state is correct+ TODO -- consider validating just the deltas (if that's possible)+ """+ write_dict = model_to_dict(model=model, include=write_keys)+ return state.update(**write_dict)+++def model_from_state(model: Type[ModelType], state: State) -> ModelType:+ """Creates a model from the state object -- capturing just the fields that are relevant to the model itself.++ :param model: model type to create+ :param state: state object to create from+ :return: model object+ """+ keys = [item for item in model.model_fields.keys() if item in state]+ return model(**{key: state[key] for key in keys})+++def _validate_and_extract_signature_types(+ fn: PydanticActionFunction,+) -> Tuple[Type[pydantic.BaseModel], Type[pydantic.BaseModel]]:+ sig = inspect.signature(fn)+ if "state" not in sig.parameters:+ raise ValueError(+ f"Function fn: {fn.__qualname__} is not a valid pydantic action. "+ "The first argument of a pydantic "+ "action must be the state object. Got signature: {sig}."+ )+ state_model = sig.parameters["state"].annotation+ if state_model is inspect.Parameter.empty or not issubclass(state_model, pydantic.BaseModel):+ raise ValueError(+ f"Function fn: {fn.__qualname__} is not a valid pydantic action. "+ "a type annotation of a type extending: pydantic.BaseModel. Got parameter "+ "state: {state_model.__qualname__}."+ )+ if sig.return_annotation is inspect.Parameter.empty or not issubclass(+ sig.return_annotation, pydantic.BaseModel+ ):+ raise ValueError(+ f"Function fn: {fn.__qualname__} is not a valid pydantic action. "+ "The return type must be a subclass of pydantic"+ ".BaseModel. Got return type: {sig.return_annotation}."+ )+ return state_model, sig.return_annotation+++def _validate_keys(model: Type[pydantic.BaseModel], keys: List[str], fn: Callable) -> None:+ missing_keys = [key for key in keys if key not in model.model_fields]+ if missing_keys:+ raise ValueError(+ f"Function fn: {fn.__qualname__} is not a valid pydantic action. "+ f"The keys: {missing_keys} are not present in the model: {model.__qualname__}."+ )+++def pydantic_action(+ reads: List[str], writes: List[str]+) -> Callable[[PydanticActionFunction], PydanticActionFunction]:+ """Action that specifies inputs/outputs using pydantic models.+ This should make it easier to develop with guardrails.++ :param reads: keys that this model reads. Note that this will be a subset of the pydantic model with which this is decorated.+ We will be validating that the keys are present in the model.+ :param writes: keys that this model writes. Note that this will be a subset of the pydantic model with which this is decorated.+ We will be validating that the keys are present in the model.+ :return:+ """++ def decorator(fn: PydanticActionFunction) -> PydanticActionFunction:+ itype, otype = _validate_and_extract_signature_types(fn)+ _validate_keys(model=itype, keys=reads, fn=fn)+ _validate_keys(model=otype, keys=writes, fn=fn)+ SubsetInputType = subset_model(+ model=itype,+ fields=reads,+ force_optional_fields=[item for item in writes if item not in reads],+ model_name_suffix=f"{fn.__name__}_input",+ )+ SubsetOutputType = subset_model(+ model=otype,+ fields=writes,+ force_optional_fields=[],+ model_name_suffix=f"{fn.__name__}_input",+ )++ def action_function(state: State, **kwargs) -> State:+ model_to_use = model_from_state(model=SubsetInputType, state=state)+ result = fn(state=model_to_use, **kwargs)+ # TODO -- validate that we can always construct this from the dict...+ # We really want a copy-type function+ output = SubsetOutputType(**model_to_dict(result, include=writes))+ return merge_to_state(model=output, write_keys=writes, state=state)++ async def async_action_function(state: State, **kwargs) -> State:+ model_to_use = model_from_state(model=SubsetInputType, state=state)+ result = await fn(state=model_to_use, **kwargs)+ output = SubsetOutputType(**model_to_dict(result, include=writes))+ return merge_to_state(model=output, write_keys=writes, state=state)++ is_async = inspect.iscoroutinefunction(fn)+ # This recreates the @action decorator+ # TODO -- use the @action decorator directly+ # TODO -- ensure that the function is the right one -- specifically it probably won't show code in the UI+ # now+ setattr(+ fn,+ FunctionBasedAction.ACTION_FUNCTION,+ FunctionBasedAction(+ async_action_function if is_async else action_function,+ reads,+ writes,+ input_spec=get_inputs({}, fn),+ originating_fn=fn,+ ),+ )+ setattr(fn, "bind", types.MethodType(bind, fn))+ # TODO -- figure out typing+ # It's not smart enough to know that we have satisfied the type signature,+ # as we dynamically apply it using setattr+ return fn++ return decorator+++PartialType = Union[Type[pydantic.BaseModel], Type[dict]]++PydanticStreamingActionFunctionSync = Callable[+ ..., Generator[Tuple[Union[pydantic.BaseModel, dict], Optional[pydantic.BaseModel]], None, None]+]++PydanticStreamingActionFunctionAsync = Callable[+ ..., AsyncGenerator[Tuple[Union[pydantic.BaseModel, dict], Optional[pydantic.BaseModel]], None]+]++PydanticStreamingActionFunction = Union[+ PydanticStreamingActionFunctionSync, PydanticStreamingActionFunctionAsync+]++PydanticStreamingActionFunctionVar = TypeVar(+ "PydanticStreamingActionFunctionVar", bound=PydanticStreamingActionFunction+)+++def _validate_and_extract_signature_types_streaming(+ fn: PydanticStreamingActionFunction,+ stream_type: Optional[Union[Type[pydantic.BaseModel], Type[dict]]],+ state_input_type: Optional[Type[pydantic.BaseModel]] = None,+ state_output_type: Optional[Type[pydantic.BaseModel]] = None,+) -> Tuple[+ Type[pydantic.BaseModel], Type[pydantic.BaseModel], Union[Type[dict], Type[pydantic.BaseModel]]+]:+ if stream_type is None:+ # TODO -- derive from the signature+ raise ValueError(f"stream_type is required for function: {fn.__qualname__}")+ if state_input_type is None:+ # TODO -- derive from the signature+ raise ValueError(f"state_input_type is required for function: {fn.__qualname__}")+ if state_output_type is None:+ # TODO -- derive from the signature+ raise ValueError(f"state_output_type is required for function: {fn.__qualname__}")+ return state_input_type, state_output_type, stream_type+++def pydantic_streaming_action(+ reads: List[str],+ writes: List[str],+ state_input_type: Optional[Type[pydantic.BaseModel]],+ state_output_type: Optional[Type[pydantic.BaseModel]],+ stream_type: Optional[PartialType],+) -> Callable[[PydanticStreamingActionFunction], PydanticStreamingActionFunction]:+ """Creates a streaming action that uses pydantic models.++ :param reads: The fields this consumes from the state.+ :param writes: The fields this writes to the state.+ :param stream_type: The pydantic model or dictionary type that is used to represent the partial results. If this is None it will attempt to derive from the signature.+ Use a dict if you want this untyped.+ :param state_input_type: The pydantic model type that is used to represent the input state. If this is None it will attempt to derive from the signature.+ :param state_output_type: The pydantic model type that is used to represent the output state. If this is None it will attempt to derive from the signature.+ :return: The same function, decorated function.+ """++ def decorator(fn: PydanticStreamingActionFunctionVar) -> PydanticStreamingActionFunctionVar:+ itype, otype, stream_type_processed = _validate_and_extract_signature_types_streaming(+ fn, stream_type, state_input_type=state_input_type, state_output_type=state_output_type+ )+ _validate_keys(model=itype, keys=reads, fn=fn)+ _validate_keys(model=otype, keys=writes, fn=fn)+ SubsetInputType = subset_model(+ model=itype,+ fields=reads,+ force_optional_fields=[item for item in writes if item not in reads],+ model_name_suffix=f"{fn.__name__}_input",+ )+ SubsetOutputType = subset_model(+ model=otype,+ fields=writes,+ force_optional_fields=[],+ model_name_suffix=f"{fn.__name__}_input",+ )+ # PartialModelType = stream_type_processed # TODO -- attach to action+ # We don't currently use this, but we will be passing to the action to validate++ def action_generator(+ state: State, **kwargs+ ) -> Generator[tuple[PartialType, Optional[State]], None, None]:+ model_to_use = model_from_state(model=SubsetInputType, state=state)+ for partial, state_update in fn(state=model_to_use, **kwargs):+ if state_update is None:+ yield partial, None+ else:+ output = SubsetOutputType(**model_to_dict(state_update, include=writes))+ yield partial, merge_to_state(model=output, write_keys=writes, state=state)++ async def async_action_generator(+ state: State, **kwargs+ ) -> AsyncGenerator[tuple[dict, Optional[State]], None]:+ model_to_use = model_from_state(model=SubsetInputType, state=state)+ async for partial, state_update in fn(state=model_to_use, **kwargs):+ if state_update is None:+ yield partial, None+ else:+ output = SubsetOutputType(**model_to_dict(state_update, include=writes))+ yield partial, merge_to_state(model=output, write_keys=writes, state=state)++ is_async = inspect.isasyncgenfunction(fn)+ # This recreates the @streaming_action decorator+ # TODO -- use the @streaming_action decorator directly+ # TODO -- ensure that the function is the right one -- specifically it probably won't show code in the UI+ # now+ setattr(+ fn,+ FunctionBasedAction.ACTION_FUNCTION,+ FunctionBasedStreamingAction(+ async_action_generator if is_async else action_generator,+ reads,+ writes,+ input_spec=get_inputs({}, fn),+ originating_fn=fn,+ ),+ )+ setattr(fn, "bind", types.MethodType(bind, fn))+ return fn++ return decorator+++StateModel = TypeVar("StateModel", bound=pydantic.BaseModel)+++class PydanticTypingSystem(TypingSystem[StateModel]):+ """Typing system for pydantic models.++ :param TypingSystem: Parameterized on the state model type.+ """++ def __init__(self, model_type: Type[StateModel]):+ self.model_type = model_type++ def state_type(self) -> Type[StateModel]:+ return self.model_type++ def state_pre_action_run_type(self, action: Action, graph: Graph) -> Type[pydantic.BaseModel]:+ raise NotImplementedError(+ "TODO -- crawl through"+ "the graph to figure out what can possibly be optional and what can't..."+ "First get all "+ )++ def state_post_action_run_type(self, action: Action, graph: Graph) -> Type[pydantic.BaseModel]:+ raise NotImplementedError(+ "TODO -- crawl through"+ "the graph to figure out what can possibly be optional and what can't..."+ "First get all "+ )++ def construct_data(self, state: State) -> StateModel:+ return model_from_state(model=self.model_type, state=state)++ def construct_state(self, data: StateModel) -> State:+ return State(model_to_dict(data))diff --git a/burr/telemetry.py b/burr/telemetry.pyindex 57879eed..0fb47fc0 100644--- a/burr/telemetry.py+++ b/burr/telemetry.py@@ -24,7 +24,7 @@ import osimport platformimport threadingimport uuid-from typing import TYPE_CHECKING, Callable, List+from typing import TYPE_CHECKING, Callable, List, TypeVarfrom urllib import requestif TYPE_CHECKING:@@ -256,7 +256,10 @@ def create_and_send_cli_event(command: str):send_event_json(event)-def capture_function_usage(call_fn: Callable) -> Callable:+CallableT = TypeVar("CallableT", bound=Callable)+++def capture_function_usage(call_fn: CallableT) -> CallableT:"""Decorator to wrap some application functions for telemetry capture.We want to use this for non-execute functions.diff --git a/docs/concepts/index.rst b/docs/concepts/index.rstindex d5f76c6d..42405173 100644--- a/docs/concepts/index.rst+++ b/docs/concepts/index.rst@@ -18,6 +18,7 @@ Overview of the concepts -- read these to get a mental model for how Burr works.state-persistenceserdestreaming-actions+ state-typinghooksadditional-visibilityrecursiondiff --git a/examples/pydantic/centralized_state.py b/examples/pydantic/centralized_state.pynew file mode 100644index 00000000..5a57f891--- /dev/null+++ b/examples/pydantic/centralized_state.py@@ -0,0 +1,217 @@+import copy+import os+from typing import List, Optional++import openai+import pydantic++from burr.core import ApplicationBuilder, State, action, default, graph, when+from burr.integrations.pydantic import PydanticTypingSystem, pydantic_action+from burr.lifecycle import LifecycleAdapter++MODES = {+ "answer_question": "text",+ "generate_image": "image",+ "generate_code": "code",+ "unknown": "text",+}+++class ApplicationState(pydantic.BaseModel):+ chat_history: List[dict[str, str]] = pydantic.Field(default_factory=list)+ prompt: Optional[str]+ has_openai_key: Optional[bool]+ safe: Optional[bool]+ mode: Optional[str]+ response: dict[str, str]+++@pydantic_action(reads=[], writes=["chat_history", "prompt"])+def process_prompt(state: ApplicationState, prompt: str) -> ApplicationState:+ state.chat_history.append({"role": "user", "content": prompt, "type": "text"})+ state.prompt = prompt+ return state+++@pydantic_action(reads=["prompt"], writes=["safe"])+def check_safety(state: ApplicationState) -> ApplicationState:+ state.safe = "unsafe" not in state.prompt+ return state+++def _get_openai_client():+ return openai.Client()+++@pydantic_action(reads=["prompt"], writes=["mode"])+def choose_mode(state: ApplicationState) -> ApplicationState:+ 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 = _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"+ state.mode = mode+ return state+++@pydantic_action(reads=["prompt", "chat_history"], writes=["response"])+def prompt_for_more(state: ApplicationState) -> ApplicationState:+ state.response = {+ "content": "None of the response modes I support apply to your question. Please clarify?",+ "type": "text",+ "role": "assistant",+ }+ return state+++@action(reads=[], writes=["has_openai_key"])+def check_openai_key(state: State) -> State:+ result = {"has_openai_key": "OPENAI_API_KEY" in os.environ}+ return state.update(**result)+++@pydantic_action(reads=["prompt", "chat_history", "mode"], writes=["response"])+def chat_response(+ state: ApplicationState,+ prepend_prompt: str,+ model: str = "gpt-3.5-turbo",+) -> ApplicationState:+ chat_history = copy.deepcopy(state.chat_history)+ 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 = client.chat.completions.create(+ model=model,+ messages=chat_history_api_format,+ )+ response = result.choices[0].message.content+ state.response = {"content": response, "type": MODES[state.mode], "role": "assistant"}+ return state+++@pydantic_action(reads=["prompt", "chat_history", "mode"], writes=["response"])+def image_response(state: ApplicationState, model: str = "dall-e-2") -> ApplicationState:+ client = _get_openai_client()+ result = client.images.generate(+ model=model, prompt=state.prompt, size="1024x1024", quality="standard", n=1+ )+ response = result.data[0].url+ state.response = {"content": response, "type": MODES[state.mode], "role": "assistant"}+ return state+++@pydantic_action(reads=["response", "mode", "safe", "has_openai_key"], writes=["chat_history"])+def response(state: ApplicationState) -> ApplicationState:+ if not state.has_openai_key:+ chat_item = {+ "role": "assistant",+ "content": "You have not set an API key for [OpenAI](https://www.openai.com). Do this "+ "by setting the environment variable `OPENAI_API_KEY` to your key. "+ "You can get a key at [OpenAI](https://platform.openai.com). "+ "You can still look at chat history/examples.",+ "type": "error",+ }+ elif not state.safe:+ chat_item = {+ "role": "assistant",+ "content": "I'm sorry, I can't respond to that.",+ "type": "error",+ }+ else:+ chat_item = state.response+ state.chat_history.append(chat_item)+ return state+++graph_object = (+ graph.GraphBuilder()+ .with_actions(+ prompt=process_prompt,+ check_openai_key=check_openai_key,+ check_safety=check_safety,+ 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,+ response=response,+ )+ .with_transitions(+ ("prompt", "check_openai_key", default),+ ("check_openai_key", "check_safety", when(has_openai_key=True)),+ ("check_openai_key", "response", default),+ ("check_safety", "decide_mode", when(safe=True)),+ ("check_safety", "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"],+ "response",+ ),+ ("response", "prompt", default),+ )+ .build()+)+++def application(+ hooks: Optional[List[LifecycleAdapter]] = None,+ project_id: str = "test_centralized_state",+):+ if hooks is None:+ hooks = []+ # we're initializing above so we can load from this as well+ # we could also use `with_tracker("local", project=project_id, params={"storage_dir": storage_dir})`+ return (+ ApplicationBuilder()+ .with_graph(graph_object)+ # initializes from the tracking log if it does not already exist+ .with_hooks(*hooks)+ .with_tracker("local", project=project_id)+ .with_entrypoint("prompt")+ .with_state(+ ApplicationState(+ chat_history=[],+ )+ )+ .with_typing(PydanticTypingSystem(model_type=ApplicationState))+ .build()+ )+++if __name__ == "__main__":+ app = application()+ # app.visualize(+ # output_file_path="statemachine", include_conditions=False, view=True, format="png"+ # )+ action, result, state = app.run(+ halt_after=["response"], inputs={"prompt": "Who was Aaron Burr, sir?"}+ )+ state.datadiff --git a/examples/pydantic/decentralized_state.py b/examples/pydantic/decentralized_state.pynew file mode 100644index 00000000..5f40da57--- /dev/null+++ b/examples/pydantic/decentralized_state.py@@ -0,0 +1,224 @@+import copy+import os+from typing import List, Optional++import openai++from burr.core import Application, ApplicationBuilder, State, default, graph, when+from burr.core.action import action+from burr.lifecycle import LifecycleAdapter+from burr.tracking import LocalTrackingClient++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) -> State:+ result = {"chat_item": {"role": "user", "content": prompt, "type": "text"}}+ return (+ state.wipe(keep=["prompt", "chat_history"])+ .append(chat_history=result["chat_item"])+ .update(prompt=prompt)+ )+++@action(reads=[], writes=["has_openai_key"])+def check_openai_key(state: State) -> State:+ result = {"has_openai_key": "OPENAI_API_KEY" in os.environ}+ return state.update(**result)+++@action(reads=["prompt"], writes=["safe"])+def check_safety(state: State) -> State:+ result = {"safe": "unsafe" not in state["prompt"]} # quick hack to demonstrate+ return state.update(safe=result["safe"])+++def _get_openai_client():+ return openai.Client()+++@action(reads=["prompt"], writes=["mode"])+def choose_mode(state: State) -> 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 = _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 state.update(**result)+++@action(reads=["prompt", "chat_history"], writes=["response"])+def prompt_for_more(state: State) -> State:+ result = {+ "response": {+ "content": "None of the response modes I support apply to your question. Please clarify?",+ "type": "text",+ "role": "assistant",+ }+ }+ return state.update(**result)+++@action(reads=["prompt", "chat_history", "mode"], writes=["response"])+def chat_response(+ state: State, prepend_prompt: str, display_type: str = "text", model: str = "gpt-3.5-turbo"+) -> State:+ chat_history = copy.deepcopy(state["chat_history"])+ 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 = client.chat.completions.create(+ model=model,+ messages=chat_history_api_format,+ )+ response = result.choices[0].message.content+ result = {"response": {"content": response, "type": MODES[state["mode"]], "role": "assistant"}}+ return state.update(**result)+++@action(reads=["prompt", "chat_history", "mode"], writes=["response"])+def image_response(state: State, model: str = "dall-e-2") -> State:+ """Generates an image response to the prompt. Optional save function to save the image to a URL."""+ 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 state.update(**result)+++@action(reads=["response", "mode", "safe", "has_openai_key"], writes=["chat_history"])+def response(state: State) -> State:+ if not state["has_openai_key"]:+ result = {+ "chat_item": {+ "role": "assistant",+ "content": "You have not set an API key for [OpenAI](https://www.openai.com). Do this "+ "by setting the environment variable `OPENAI_API_KEY` to your key. "+ "You can get a key at [OpenAI](https://platform.openai.com). "+ "You can still look at chat history/examples.",+ "type": "error",+ }+ }+ elif not state["safe"]:+ result = {+ "chat_item": {+ "role": "assistant",+ "content": "I'm sorry, I can't respond to that.",+ "type": "error",+ }+ }+ else:+ result = {"chat_item": state["response"]}+ return state.append(chat_history=result["chat_item"])+++graph = (+ graph.GraphBuilder()+ .with_actions(+ prompt=process_prompt,+ check_openai_key=check_openai_key,+ check_safety=check_safety,+ 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,+ response=response,+ )+ .with_transitions(+ ("prompt", "check_openai_key", default),+ ("check_openai_key", "check_safety", when(has_openai_key=True)),+ ("check_openai_key", "response", default),+ ("check_safety", "decide_mode", when(safe=True)),+ ("check_safety", "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"],+ "response",+ ),+ ("response", "prompt", default),+ )+ .build()+)+++def base_application(+ hooks: List[LifecycleAdapter],+ app_id: str,+ storage_dir: str,+ project_id: str,+):+ if hooks is None:+ hooks = []+ # we're initializing above so we can load from this as well+ # we could also use `with_tracker("local", project=project_id, params={"storage_dir": storage_dir})`+ tracker = LocalTrackingClient(project=project_id, storage_dir=storage_dir)+ return (+ ApplicationBuilder()+ .with_graph(graph)+ # initializes from the tracking log if it does not already exist+ .initialize_from(+ tracker,+ resume_at_next_action=False, # always resume from entrypoint in the case of failure+ default_state={"chat_history": []},+ default_entrypoint="prompt",+ )+ .with_hooks(*hooks)+ .with_tracker(tracker)+ .with_identifiers(app_id=app_id)+ .build()+ )+++def application(+ app_id: Optional[str] = None,+ project_id: str = "demo_chatbot",+ storage_dir: Optional[str] = "~/.burr",+ hooks: Optional[List[LifecycleAdapter]] = None,+) -> Application:+ return base_application(hooks, app_id, storage_dir, project_id=project_id)+++if __name__ == "__main__":+ app = application()+ app.visualize(+ output_file_path="statemachine", include_conditions=False, view=True, format="png"+ )+ print(app.run(halt_after=["response"], inputs={"prompt": "Who was Aaron Burr, sir?"}))diff --git a/examples/youtube-to-social-media-post/application.py b/examples/youtube-to-social-media-post/application.pyindex 683cec3e..3a7f80c1 100644--- a/examples/youtube-to-social-media-post/application.py+++ b/examples/youtube-to-social-media-post/application.py@@ -1,14 +1,19 @@import textwrap-from typing import Union+from typing import Any, Generator, Optional, Tuple, Unionimport instructorimport openaifrom pydantic import BaseModel, Fieldfrom pydantic.json_schema import SkipJsonSchema+from rich.console import Consolefrom youtube_transcript_api import YouTubeTranscriptApi-from burr.core import Application, ApplicationBuilder, State, action-from burr.core.persistence import SQLLitePersister+from burr.core import Application, ApplicationBuilder+from burr.integrations.pydantic import (+ PydanticTypingSystem,+ pydantic_action,+ pydantic_streaming_action,+)class Concept(BaseModel):@@ -32,14 +37,16 @@ class SocialMediaPost(BaseModel):description="The body of the social media post. It should be informative and make the reader curious about viewing the video.")concepts: list[Concept] = Field(- description="Important concepts about Hamilton or Burr mentioned in this post.",- min_items=1,+ description="Important concepts about Hamilton or Burr mentioned in this post -- please have at least 1",+ min_items=0,max_items=3,+ validate_default=False,)key_takeaways: list[str] = Field(- description="A list of informative key takeways for the reader.",- min_items=1,+ description="A list of informative key takeways for the reader -- please have at least 1",+ min_items=0,max_items=4,+ validate_default=False,)youtube_url: SkipJsonSchema[Union[str, None]] = None@@ -66,24 +73,72 @@ class SocialMediaPost(BaseModel):)-@action(reads=[], writes=["transcript"])-def get_youtube_transcript(state: State, youtube_url: str) -> State:+class ApplicationState(BaseModel):+ # Make these have defaults as they are only set in actions+ transcript: Optional[str] = Field(+ description="The full transcript of the YouTube video.", default=None+ )+ post: Optional[SocialMediaPost] = Field(+ description="The generated social media post.", default=None+ )+++class ApplicationStateStream(ApplicationState):+ # Make these have defaults as they are only set in actions+ post_generator: Optional[Generator[SocialMediaPost, None, None]] = None++ class Config:+ arbitrary_types_allowed = True++ def __copy__(self, memo: dict[int, Any] | None = None):+ post_generator = self.post_generator+ self.post_generator = None+ out = self.model_copy(deep=True, update={"post_generator": post_generator})+ self.post_generator = post_generator+ return out+ # # TODO -- ensure that post_generator is copied by reference, not value...+ # # Ignore this for now -- this is specifically dealing with a copy() issue+ # # then delegate to the superclass+ # if memo is None:+ # memo = {}+ # if id(self) in memo:+ # return memo[id(self)]++ # # Create a shallow copy to modify+ # new_obj = copy.copy(self)++ # # Copy each attribute except the generator which should be shared+ # for k, v in self.__dict__.items():+ # if k != "post_generator":+ # setattr(new_obj, k, copy.deepcopy(v, memo))++ # # Reference the same generator instance+ # new_obj.post_generator = self.post_generator++ # # Store in memoization dictionary+ # memo[id(self)] = new_obj++ # return new_obj+++@pydantic_action(reads=[], writes=["transcript"])+def get_youtube_transcript(state: ApplicationState, youtube_url: str) -> ApplicationState:"""Get the official YouTube transcript for a video given it's URL"""_, _, video_id = youtube_url.partition("?v=")transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=["en"])- full_transcript = " ".join([f"ts={entry['start']} - {entry['text']}" for entry in transcript])+ state.transcript = " ".join([f"ts={entry['start']} - {entry['text']}" for entry in transcript])+ return state# store the transcript in state- return state.update(transcript=full_transcript)-@action(reads=["transcript"], writes=["post"])-def generate_post(state: State, llm_client) -> State:+@pydantic_action(reads=["transcript"], writes=["post"])+def generate_post(state: ApplicationState, llm_client) -> ApplicationState:"""Use the Instructor LLM client to generate `SocialMediaPost` from the YouTube transcript."""# read the transcript from state- transcript = state["transcript"]+ transcript = state.transcriptresponse = llm_client.chat.completions.create(model="gpt-4o-mini",@@ -96,59 +151,162 @@ def generate_post(state: State, llm_client) -> State:{"role": "user", "content": transcript},],)+ state.post = response# store the chapters in state- return state.update(post=response)---@action(reads=["post"], writes=["post"])-def rewrite_post(state: State, llm_client, user_prompt: str):- post = state["post"]+ return state+++@pydantic_streaming_action(+ reads=["transcript"],+ writes=["post"],+ state_input_type=ApplicationState,+ state_output_type=ApplicationState,+ stream_type=SocialMediaPost,+)+def generate_post_streaming(+ state: ApplicationStateStream, llm_client+) -> Generator[Tuple[SocialMediaPost, Optional[ApplicationState]], None, None]:+ """Use the Instructor LLM client to generate `SocialMediaPost` from the YouTube transcript."""- response = llm_client.chat.completions.create(+ transcript = state.transcript+ response = llm_client.chat.completions.create_partial(model="gpt-4o-mini",response_model=SocialMediaPost,messages=[{"role": "system",- "content": f"Take the previously generated social media post and modify it according to the following instructions: {user_prompt}",+ "content": "Analyze the given YouTube transcript and generate a compelling social media post.",},- {"role": "user", "content": post.model_dump_json()},+ {"role": "user", "content": transcript},],+ stream=True,)+ final_post = None+ for post in response:+ final_post = post+ yield post, None++ yield final_post, state+++@pydantic_streaming_action(+ reads=["transcript"],+ writes=["post"],+ state_input_type=ApplicationState,+ state_output_type=ApplicationState,+ stream_type=SocialMediaPost,+)+async def generate_post_streaming_async(+ state: ApplicationStateStream, llm_client+) -> Generator[Tuple[SocialMediaPost, Optional[ApplicationState]], None, None]:+ """Use the Instructor LLM client to generate `SocialMediaPost` from the YouTube transcript."""- # pass the youtube_url from the previous post version- response.youtube_url = post.youtube_url+ transcript = state.transcript+ response = llm_client.chat.completions.create_partial(+ model="gpt-4o-mini",+ response_model=SocialMediaPost,+ messages=[+ {+ "role": "system",+ "content": "Analyze the given YouTube transcript and generate a compelling social media post.",+ },+ {"role": "user", "content": transcript},+ ],+ stream=True,+ )+ final_post = None+ async for post in response:+ final_post = post+ yield post, None- return state.update(post=response)+ yield final_post, state-def build_application() -> Application:+def build_application() -> Application[ApplicationState]:llm_client = instructor.from_openai(openai.OpenAI())- return (+ app = (ApplicationBuilder().with_actions(get_youtube_transcript,generate_post.bind(llm_client=llm_client),- rewrite_post.bind(llm_client=llm_client),).with_transitions(("get_youtube_transcript", "generate_post"),- ("generate_post", "rewrite_post"),- ("rewrite_post", "rewrite_post"),)- .with_state_persister(SQLLitePersister(db_path=".burr.db", table_name="state"))+ # .with_state_persister(SQLLitePersister(db_path=".burr.db", table_name="state")).with_entrypoint("get_youtube_transcript")+ .with_typing(PydanticTypingSystem(ApplicationState))+ .with_state(ApplicationState()).with_tracker(project="youtube-post").build())+ return app-if __name__ == "__main__":- app = build_application()- app.visualize(output_file_path="statemachine.png")+def build_streaming_application() -> Application[ApplicationState]:+ llm_client = instructor.from_openai(openai.OpenAI())+ app = (+ ApplicationBuilder()+ .with_actions(+ get_youtube_transcript,+ generate_post=generate_post_streaming.bind(llm_client=llm_client),+ )+ .with_transitions(+ ("get_youtube_transcript", "generate_post"),+ )+ # .with_state_persister(SQLLitePersister(db_path=".burr.db", table_name="state"))+ .with_entrypoint("get_youtube_transcript")+ .with_typing(PydanticTypingSystem(ApplicationState))+ .with_state(ApplicationState())+ .with_tracker(project="youtube-post")+ .build()+ )+ return app- _, _, state = app.run(++def build_streaming_application_async() -> Application[ApplicationState]:+ llm_client = instructor.from_openai(openai.AsyncOpenAI())+ app = (+ ApplicationBuilder()+ .with_actions(+ get_youtube_transcript,+ generate_post=generate_post_streaming_async.bind(llm_client=llm_client),+ )+ .with_transitions(+ ("get_youtube_transcript", "generate_post"),+ )+ # .with_state_persister(SQLLitePersister(db_path=".burr.db", table_name="state"))+ .with_entrypoint("get_youtube_transcript")+ .with_typing(PydanticTypingSystem(ApplicationState))+ .with_state(ApplicationState())+ .with_tracker(project="test-youtube-post")+ .build()+ )+ return app+++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"},+ )+ async for post in streaming_container:+ obj = post.model_dump()+ console.clear()+ console.print(obj)+++if __name__ == "__main__":+ console = Console()+ app = build_streaming_application()+ action, streaming_container = app.stream_result(halt_after=["generate_post"],inputs={"youtube_url": "https://www.youtube.com/watch?v=hqutVJyd3TI"},)+ for post in streaming_container:+ obj = post.model_dump()+ console.clear()+ console.print(obj)diff --git a/examples/youtube-to-social-media-post/server.py b/examples/youtube-to-social-media-post/server.pyindex 75b8a4d4..4c0fd20f 100644--- a/examples/youtube-to-social-media-post/server.py+++ b/examples/youtube-to-social-media-post/server.py@@ -1,45 +1,59 @@import contextlibimport logging-from typing import Optional-import applicationimport fastapiimport uvicorn+from application import (+ ApplicationState,+ ApplicationStateStream,+ build_application,+ build_application_iterator_streaming,+)+from fastapi.responses import StreamingResponsefrom burr.core import Applicationlogger = logging.getLogger(__name__)# define a global `burr_app` variable-burr_app: Optional[Application] = None---def get_burr_app() -> Application:- """Retrieve the global Burr app."""- if burr_app is None:- raise RuntimeError("Burr app wasn't instantiated.")- return burr_app+burr_app: Application[ApplicationState] = None+# Second variant -- this uses a stream + a self-loop+# Note this will save a *lot* to the tracker, each stream!+burr_app_streaming_iterator: Application[ApplicationStateStream] = None@contextlib.asynccontextmanagerasync def lifespan(app: fastapi.FastAPI):"""Instantiate the Burr application on FastAPI startup."""# set value for the global `burr_app` variable- global burr_app- burr_app = application.build_application()+ global burr_app, burr_app_streaming_iterator+ burr_app = build_application()+ burr_app_streaming_iterator = build_application_iterator_streaming()yieldapp = fastapi.FastAPI(lifespan=lifespan)[email protected]("/social_media_post")-def social_media_post(youtube_url: str, burr_app: Application = fastapi.Depends(get_burr_app)):[email protected]("/social_media_post", response_model=ApplicationState)+def social_media_post(youtube_url: str) -> ApplicationState:"""Creates a completion for the chat message"""_, _, state = burr_app.run(halt_after=["generate_post"], inputs={"youtube_url": youtube_url})- post = state["post"]- return {"formatted_post": post.display(), "post": post.model_dump()}+ return state.data++[email protected]("/social_media_post_streaming_1", response_model=StreamingResponse)+def social_media_post_streaming(youtube_url: str) -> StreamingResponse:+ """Creates a completion for the chat message"""++ def gen():+ for action, _, state in burr_app_streaming_iterator.iterate(+ halt_after=["final"], inputs={"youtube_url": youtube_url}+ ):+ yield state.data.model_dump_json()++ return StreamingResponse(gen())if __name__ == "__main__":diff --git a/examples/youtube-to-social-media-post/statemachine.png b/examples/youtube-to-social-media-post/statemachine.pngindex bdf72f90..e9ae4314 100644Binary files a/examples/youtube-to-social-media-post/statemachine.png and b/examples/youtube-to-social-media-post/statemachine.png differdiff --git a/pyproject.toml b/pyproject.tomlindex ceff5445..ebcb755c 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"[project]name = "burr"-version = "0.29.2"+version = "0.30.0rc4"dependencies = [] # yes, there are nonerequires-python = ">=3.9"authors = [diff --git a/tests/core/test_application.py b/tests/core/test_application.pyindex 53daa0e6..b6fbb967 100644--- a/tests/core/test_application.py+++ b/tests/core/test_application.py@@ -41,6 +41,7 @@ from burr.core.application import ()from burr.core.graph import Graph, GraphBuilder, Transitionfrom burr.core.persistence import BaseStatePersister, DevNullPersister, PersistedStateData+from burr.core.typing import TypingSystemfrom burr.lifecycle import (PostRunStepHook,PostRunStepHookAsync,@@ -3184,3 +3185,44 @@ def test_application_recursive_action_lifecycle_hooks():len(hook.pre_called) == 62) # 63 - the initial one from the call to recursive_action outside the applicationassert len(hook.post_called) == 62 # ditto+++class CounterState(State):+ count: int+++class SimpleTypingSystem(TypingSystem[CounterState]):+ def state_type(self) -> type[CounterState]:+ return CounterState++ def state_pre_action_run_type(self, action: Action, graph: Graph) -> type[Any]:+ raise NotImplementedError++ def state_post_action_run_type(self, action: Action, graph: Graph) -> type[Any]:+ raise NotImplementedError++ def construct_data(self, state: State[Any]) -> CounterState:+ return CounterState({"count": state["count"]})++ def construct_state(self, data: Any) -> State[Any]:+ raise NotImplementedError+++def test_builder_captures_typing_system():+ """Tests that the typing system is captured correctly"""+ counter_action = base_counter_action.with_name("counter")+ result_action = Result("count").with_name("result")+ app = (+ ApplicationBuilder()+ .with_actions(counter_action, result_action)+ .with_transitions(("counter", "counter", expr("count < 10")))+ .with_transitions(("counter", "result", default))+ .with_entrypoint("counter")+ .with_state(count=0)+ .with_typing(SimpleTypingSystem())+ .build()+ )+ assert isinstance(app.state.data, CounterState)+ _, _, state = app.run(halt_after=["result"])+ assert isinstance(state.data, CounterState)+ assert state.data["count"] == 10diff --git a/tests/core/test_state.py b/tests/core/test_state.pyindex 4572f8e4..30374d2a 100644--- a/tests/core/test_state.py+++ b/tests/core/test_state.py@@ -1,6 +1,10 @@+from typing import Any+import pytest+from burr.core import Action, Graphfrom burr.core.state import State, register_field_serde+from burr.core.typing import TypingSystemdef test_state_access():@@ -158,3 +162,26 @@ def test_register_field_serde_check_no_kwargs():with pytest.raises(ValueError):# deserializer still badregister_field_serde("my_field", my_field_serializer, my_field_deserializer)+++class SimpleTypingSystem(TypingSystem[Any]):+ def state_type(self) -> type[Any]:+ raise NotImplementedError++ def state_pre_action_run_type(self, action: Action, graph: Graph) -> type[Any]:+ raise NotImplementedError++ def state_post_action_run_type(self, action: Action, graph: Graph) -> type[Any]:+ raise NotImplementedError++ def construct_data(self, state: State[Any]) -> Any:+ raise NotImplementedError++ def construct_state(self, data: State[Any]) -> State[Any]:+ raise NotImplementedError+++def test_state_apply_keeps_typing_system():+ state = State({"foo": "bar"}, typing_system=SimpleTypingSystem())+ assert state.update(foo="baz").typing_system is state.typing_system+ assert state.subset("foo").typing_system is state.typing_systemdiff --git a/tests/core/test_validation.py b/tests/core/test_validation.pyindex 21866be6..8303edf9 100644--- a/tests/core/test_validation.py+++ b/tests/core/test_validation.py@@ -8,5 +8,5 @@ def test__assert_set():def test__assert_set_unset():- with pytest.raises(ValueError, match="foo"):+ with pytest.raises(ValueError, match="bar"):assert_set(None, "foo", "bar")diff --git a/tests/integrations/test_burr_opentelemetry.py b/tests/integrations/test_burr_opentelemetry.pyindex d19ed6ea..e3789894 100644--- a/tests/integrations/test_burr_opentelemetry.py+++ b/tests/integrations/test_burr_opentelemetry.py@@ -7,7 +7,7 @@ from burr.core import serdefrom burr.integrations.opentelemetry import convert_to_otel_attribute-class TestModel(pydantic.BaseModel):+class SampleModel(pydantic.BaseModel):foo: intbar: bool@@ -21,7 +21,7 @@ class TestModel(pydantic.BaseModel):((1.0, 1.0), [1.0, 1.0]),((True, True), [True, True]),(("hello", "hello"), ["hello", "hello"]),- (TestModel(foo=1, bar=True), json.dumps(serde.serialize(TestModel(foo=1, bar=True)))),+ (SampleModel(foo=1, bar=True), json.dumps(serde.serialize(SampleModel(foo=1, bar=True)))),],)def test_convert_to_otel_attribute(value, expected):