pub enum Reducer {
Overwrite,
Append,
DeepMerge,
AddMessages,
}Expand description
Per-channel merge semantics.
A reducer is conceptually a binary function
reduce(current: Option<&Value>, update: Value) -> Value, mirroring
LangGraph’s Annotated[T, reducer] channel annotations.
Variants§
Overwrite
LastValue semantics: the update replaces the current value.
At most one write per super-step — a second write to the same
channel within one super-step fails with
RustyError::InvalidUpdate. This is the default for any
channel and the classic production failure mode in parallel graphs;
if you need fan-in, use Reducer::Append, Reducer::DeepMerge,
or Reducer::AddMessages.
Append
List-concat semantics: the current value is treated as an array
(a missing current value starts as []). If the update is an
array it is extended onto the current array; otherwise the update is
pushed as a single element.
A non-array current value is rejected by
StateSpec::apply_super_step with RustyError::InvalidUpdate;
only direct calls to Reducer::apply coerce it to [].
DeepMerge
Recursive object merge: two JSON objects are merged key-by-key (nested objects merge recursively); any non-object pair resolves to the update value. A missing current value resolves to the update.
AddMessages
LangGraph add_messages semantics over a message array.
The current value is treated as an array of message objects. The
update may be a single message object or an array of messages. Each
incoming message is upserted: if it has an "id" field equal to
an existing message’s "id", the existing message is replaced in
place; otherwise the message is appended. Messages whose "id" is
present but not a string are treated as id-less (always appended).
Like Reducer::Append, a non-array current value is rejected by
StateSpec::apply_super_step.
Implementations§
Source§impl Reducer
impl Reducer
Sourcepub fn allows_multiple_writes(self) -> bool
pub fn allows_multiple_writes(self) -> bool
Whether this channel accepts multiple writes within one super-step.
Only LastValue-style channels (Reducer::Overwrite) are
single-write; aggregating reducers exist precisely to support
parallel fan-in.
Sourcepub fn apply(&self, current: Option<&Value>, update: Value) -> Value
pub fn apply(&self, current: Option<&Value>, update: Value) -> Value
Apply one update to a channel’s current value.
current is None when the channel has never been written.
For Reducer::Append and Reducer::AddMessages, a non-array
current is treated as [] (the update starts a fresh array).
StateSpec::apply_super_step rejects that case with
RustyError::InvalidUpdate before reducers run; the coercion
here exists only for direct callers of this method.