Skip to content

Default reads/writes in actions #268

@elijahbenizzy

Description

@elijahbenizzy

Is your feature request related to a problem? Please describe.

There are cases in which you only want to write one thing to state. Take, for example, this:

@action(reads=["prompt"], writes=["response", "error"])
def respond(state: State) -> State:
    try:
        return state.update(response=_query(state["prompt"]))
    except SpecificException as e:
        return state.update(error=e)

Currently this would break in both cases with the following error:

File [~/dev/dagworks/os/burr/burr/core/application.py:155](http://localhost:8888/lab/workspaces/auto-3/tree/~/dev/dagworks/os/burr/burr/core/application.py#line=154), in _validate_reducer_writes(reducer, state, name)
    153 missing_writes = set(reducer.writes) - state.keys()
    154 if len(missing_writes) > 0:
--> 155     raise ValueError(
    156         f"State is missing write keys after running: {name}. Missing keys are: {missing_writes}. "
    157         f"Has writes: {required_writes}"
    158     )

ValueError: State is missing write keys after running: respond. Missing keys are: {'response'}. Has writes: ['response', 'error']`

Describe the solution you'd like
In most cases, people will end up padding with None, or some other sentinel value. What if we made it a specific exception?

@action(reads=["prompt"], writes=["response", "error"], default_writes={"response" : None, "error" : None})
def respond(state: State) -> State:
    try:
        return state.update(response=_query(state["prompt"]))
    except SpecificException as e:
        return state.update(error=e)

Describe alternatives you've considered
We could leverage the typing system with Optional see #139, or not enable this. That said, I think this is a fairly simple pattern.

Other Q is how to specify that just one of these is required. Tooling like pydantic requires custom validation for this, which feels a little overboard... Defaults is a nice but clean way to do this I think.

Additional context

Based on this discord question: https://discord.com/channels/1221891403253288970/1221892061842772018/1260680704002494514.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions