Lesson 1.2: The "Broken Telephone" (Why State Matters)

The most important concept in Module 1 is the State Schema.

In Python, you can pass any variable to any function. In LangGraph, the State is strict. If you try to pass data that isn’t defined in your TypedDict, it vanishes (or causes errors depending on validation).

Let’s see what happens when we try to sneak extra data through the graph without declaring it.

The Code (The Mistake)

from typing import TypedDict
from langgraph.graph import StateGraph, END
# --- 1. The Strict State ---
class StrictState(TypedDict):
    visible_data: str
    # Note: We are NOT defining "secret_data" here

# --- 2. The Nodes ---
def sender_node(state):
    print("--- SENDER ---")
    return {
        "visible_data": "I am seen", 
        "secret_data": "I am hidden"  # <--- Trying to pass undeclared data
    }

def receiver_node(state):
    print("--- RECEIVER ---")
    # Let's try to access the secret data
    secret = state.get("secret_data", "NOT FOUND")
    print(f"Received secret: {secret}")
    return {"visible_data": state["visible_data"]}

# --- 3. Build Graph ---
builder = StateGraph(StrictState)
builder.add_node("sender", sender_node)
builder.add_node("receiver", receiver_node)

builder.set_entry_point("sender")
builder.add_edge("sender", "receiver")
builder.add_edge("receiver", END)

graph = builder.compile()

# --- 4. Run ---
try:
    graph.invoke({"visible_data": ""})
except Exception as e:
    print(f"\nCRASHED: {e}")

The Result

--- SENDER ---
--- RECEIVER ---
Received secret: NOT FOUND

This code will often crash or produce a validation warning because secret_data is not a valid key for StrictState.

Why is this a feature, not a bug? When building complex agents, you will have 5 or 6 different nodes (Researcher, Writer, Reviewer).

  • If the Researcher generates references, but the State doesn’t have a references field, the Writer will never see them.
  • The State Schema acts as a Contract. It forces you to plan exactly what your agents are allowed to know and share.

The fix

If you instead declare the secret_data in the state:

class StrictState(TypedDict):
    visible_data: str
    secret_data: str

Then running the file again would produce the secret data:

--- SENDER ---
--- RECEIVER ---
Received secret: I am hidden

Leave a Comment