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.

Adds more tests for pydantic typed state implementation

elijahbenizzy25835e20b17d
diff --git a/burr/core/action.py b/burr/core/action.py
index ab80b2b0..7f99f48e 100644
--- a/burr/core/action.py
+++ b/burr/core/action.py
@@ -534,7 +534,14 @@ 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 derive_inputs_from_fn(bound_params: dict, fn: Callable) -> tuple[list[str], list[str]]:
+ """Derives inputs from the function, given the bound parameters. This assumes that the function
+ has inputs named `state`, as well as any number of other kwarg-boundable parameters.
+
+ :param bound_params: Parameters that are already bound to the function
+ :param fn: Function to derive inputs from
+ :return: Required and optional inputs
+ """
sig = inspect.signature(fn)
required_inputs, optional_inputs = [], []
for param_name, param in sig.parameters.items():
@@ -580,7 +587,7 @@ class FunctionBasedAction(SingleStepAction):
self._writes = writes
self._bound_params = bound_params if bound_params is not None else {}
self._inputs = (
- get_inputs(self._bound_params, self._fn)
+ derive_inputs_from_fn(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],
@@ -852,7 +859,7 @@ class StreamingResultContainer(Generic[StateType, StreamResultType], Iterator[St
# as the async version
return gen_fn()
- def get(self) -> StreamType:
+ def get(self) -> Tuple[StreamResultType, State[StateType]]:
# exhaust the generator
for _ in self:
pass
@@ -1054,7 +1061,7 @@ class FunctionBasedStreamingAction(SingleStepStreamingAction):
self._writes = writes
self._bound_params = bound_params if bound_params is not None else {}
self._inputs = (
- get_inputs(self._bound_params, self._fn)
+ derive_inputs_from_fn(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],
diff --git a/burr/integrations/pydantic.py b/burr/integrations/pydantic.py
index 3b237266..4b458e7b 100644
--- a/burr/integrations/pydantic.py
+++ b/burr/integrations/pydantic.py
@@ -21,7 +21,12 @@ 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.action import (
+ FunctionBasedAction,
+ FunctionBasedStreamingAction,
+ bind,
+ derive_inputs_from_fn,
+)
from burr.core.typing import ActionSchema, TypingSystem
Inputs = ParamSpec("Inputs")
@@ -227,7 +232,7 @@ def pydantic_action(
async_action_function if is_async else action_function,
reads,
writes,
- input_spec=get_inputs({}, fn),
+ input_spec=derive_inputs_from_fn({}, fn),
originating_fn=fn,
schema=PydanticActionSchema(
input_type=SubsetInputType,
@@ -357,7 +362,7 @@ def pydantic_streaming_action(
async_action_generator if is_async else action_generator,
reads,
writes,
- input_spec=get_inputs({}, fn),
+ input_spec=derive_inputs_from_fn({}, fn),
originating_fn=fn,
schema=PydanticActionSchema(
input_type=SubsetInputType,
diff --git a/tests/core/test_action.py b/tests/core/test_action.py
index 6a477973..675f5dd3 100644
--- a/tests/core/test_action.py
+++ b/tests/core/test_action.py
@@ -19,6 +19,7 @@ from burr.core.action import (
action,
create_action,
default,
+ derive_inputs_from_fn,
streaming_action,
)
@@ -743,3 +744,53 @@ def test_streaming_result_callback_error_async():
((result, state, error),) = called
assert state["foo"] == "bar"
assert result is None
+
+
+def test_derive_inputs_from_fn_state_only():
+ def fn(state):
+ ...
+
+ bound_params = {}
+ required, optional = derive_inputs_from_fn(bound_params, fn)
+ assert required == []
+ assert optional == []
+
+
+def test_derive_inputs_from_fn_state_and_required():
+ def fn(state, a, b):
+ ...
+
+ bound_params = {"state": 1}
+ required, optional = derive_inputs_from_fn(bound_params, fn)
+ assert required == ["a", "b"]
+ assert optional == []
+
+
+def test_derive_inputs_from_fn_state_required_and_optional():
+ def fn(state, a, b=2):
+ ...
+
+ bound_params = {"state": 1}
+ required, optional = derive_inputs_from_fn(bound_params, fn)
+ assert required == ["a"]
+ assert optional == ["b"]
+
+
+def test_derive_inputs_from_fnh_state_and_all_bound_except_state():
+ def fn(state, a, b):
+ ...
+
+ bound_params = {"a": 1, "b": 2}
+ required, optional = derive_inputs_from_fn(bound_params, fn)
+ assert required == []
+ assert optional == []
+
+
+def test_non_existent_bound_parameters():
+ def fn(state, a):
+ ...
+
+ bound_params = {"a": 1, "non_existent": 2}
+ required, optional = derive_inputs_from_fn(bound_params, fn)
+ assert required == []
+ assert optional == []