You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/oss/langgraph/graph-api.mdx
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -287,6 +287,13 @@ const State = z.object({
287
287
In this example, we've used [Zod 4 registries](https://zod.dev/metadata) to specify a reducer function for the second key (`bar`). Note that the first key remains unchanged. Let's assume the input to the graph is `{ foo: 1, bar: ["hi"] }`. Let's then assume the first `Node` returns `{ foo: 2 }`. This is treated as an update to the state. Notice that the `Node` does not need to return the whole `State` schema - just an update. After applying this update, the `State` would then be `{ foo: 2, bar: ["hi"] }`. If the second node returns `{ bar: ["bye"] }` then the `State` would then be `{ foo: 2, bar: ["hi", "bye"] }`. Notice here that the `bar` key is updated by adding the two arrays together.
288
288
:::
289
289
290
+
:::python
291
+
#### Overwrite
292
+
<Tip>
293
+
In some cases, you may want to bypass a reducer and directly overwrite a state value. LangGraph provides the [`Overwrite`](https://reference.langchain.com/python/langgraph/types/) type for this purpose. [Learn how to use `Overwrite` here](/oss/langgraph/use-graph-api#bypass-reducers-with-overwrite).
Copy file name to clipboardExpand all lines: src/oss/langgraph/use-graph-api.mdx
+52Lines changed: 52 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -446,6 +446,58 @@ const State = z.object({
446
446
```
447
447
:::
448
448
449
+
:::python
450
+
### Bypass reducers with `Overwrite`
451
+
452
+
In some cases, you may want to bypass a reducer and directly overwrite a state value. LangGraph provides the [`Overwrite`](https://reference.langchain.com/python/langgraph/types/) type for this purpose. When a node returns a value wrapped with `Overwrite`, the reducer is bypassed and the channel is set directly to that value.
453
+
454
+
This is useful when you want to reset or replace accumulated state rather than merge it with existing values.
455
+
456
+
```python
457
+
from langgraph.graph import StateGraph, START, END
458
+
from langgraph.types import Overwrite
459
+
from typing_extensions import Annotated, TypedDict
460
+
import operator
461
+
462
+
classState(TypedDict):
463
+
messages: Annotated[list, operator.add]
464
+
465
+
defadd_message(state: State):
466
+
return {"messages": ["first message"]}
467
+
468
+
defreplace_messages(state: State):
469
+
# Bypass the reducer and replace the entire messages list
When nodes execute in parallel, only one node can use `Overwrite` on the same state key in a given super-step. If multiple nodes attempt to overwrite the same key in the same super-step, an `InvalidUpdateError` will be raised.
498
+
</Warning>
499
+
:::
500
+
449
501
### Define input and output schemas
450
502
451
503
By default, `StateGraph` operates with a single schema, and all nodes are expected to communicate using that schema. However, it's also possible to define distinct input and output schemas for a graph.
0 commit comments