Lesson 2.1 The Problem: The "Overwrite" Bug

Let’s look at what happens when two nodes try to write to the same field in the state using the method from Module 1.

The Broken Code

We want to collect a list of topics. Node 1 finds “AI”. Node 2 finds “Crypto”.

from typing import TypedDict
from langgraph.graph import StateGraph, END

# --- 1. The Standard State ---
class BrokenState(TypedDict):
    topics: list[str]

# --- 2. The Nodes ---
def node_a(state):
    # Node A finds "AI" and tries to add it
    return {"topics": ["AI"]}

def node_b(state):
    # Node B finds "Crypto" and tries to add it
    return {"topics": ["Crypto"]}

# --- 3. Build Graph ---
builder = StateGraph(BrokenState)
builder.add_node("A", node_a)
builder.add_node("B", node_b)

builder.set_entry_point("A")
builder.add_edge("A", "B")
builder.add_edge("B", END)

app = builder.compile()

# --- 4. Run ---
result = app.invoke({"topics": []})
print(f"Final Topics: {result['topics']}")

The Result

Final Topics: ['Crypto']

What happened to “AI”? It was deleted. When Node B returned {"topics": ["Crypto"]}, LangGraph performed a standard dictionary update: state["topics"] = ["Crypto"]. The previous value was lost.

Leave a Comment