Skip to content

Commit b8cb4b6

Browse files
authored
docs(langgraph): add docs on Overwrite (#1202)
Add docs on new LangGraph `Overwrite` type, which is used to bypass a reducer in a BinaryOperatorAggregate channel and write directly to the channel. [Overwrite LG permalink](https://github.com/langchain-ai/langgraph/blob/c1661dd07fa6dca871e327b48caefc585e48e36e/libs/langgraph/langgraph/types.py#L522) There is a problem with rendering the Overwrite example in the reference doc: <img width="500" alt="image" src="https://github.com/user-attachments/assets/a9a6dc63-93f2-43f6-95f7-b7a9eeb1e168" />
1 parent 8ecccf6 commit b8cb4b6

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

reference/python/docs/langgraph/types.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
- StateSnapshot
1212
- Send
1313
- Command
14+
- Overwrite
1415
- interrupt

src/oss/langgraph/graph-api.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ const State = z.object({
287287
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.
288288
:::
289289

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).
294+
</Tip>
295+
:::
296+
290297
### Working with Messages in Graph State
291298

292299
#### Why use messages?

src/oss/langgraph/use-graph-api.mdx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,58 @@ const State = z.object({
446446
```
447447
:::
448448

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+
class State(TypedDict):
463+
messages: Annotated[list, operator.add]
464+
465+
def add_message(state: State):
466+
return {"messages": ["first message"]}
467+
468+
def replace_messages(state: State):
469+
# Bypass the reducer and replace the entire messages list
470+
return {"messages": Overwrite(["replacement message"])}
471+
472+
builder = StateGraph(State)
473+
builder.add_node("add_message", add_message)
474+
builder.add_node("replace_messages", replace_messages)
475+
builder.add_edge(START, "add_message")
476+
builder.add_edge("add_message", "replace_messages")
477+
builder.add_edge("replace_messages", END)
478+
479+
graph = builder.compile()
480+
481+
result = graph.invoke({"messages": ["initial"]})
482+
print(result["messages"])
483+
```
484+
485+
```
486+
['replacement message']
487+
```
488+
489+
You can also use JSON format with the special key `"__overwrite__"`:
490+
491+
```python
492+
def replace_messages(state: State):
493+
return {"messages": {"__overwrite__": ["replacement message"]}}
494+
```
495+
496+
<Warning>
497+
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+
449501
### Define input and output schemas
450502

451503
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

Comments
 (0)