07 Oct LangGraph State, Nodes, and Edges Explained
Every LangGraph app is built from three parts. Master these, and the rest of the tutorial gets easy.
1. State: the shared notebook
State is the data your app carries from step to step. You define its shape up front, usually with a Python TypedDict:
from typing import TypedDict
class State(TypedDict):
question: str
answer: str
Every node can read this state, and each one updates it as the graph runs.
2. Nodes: where work happens
A node is a plain Python function. It receives the current state and returns a dictionary of updates:
def clean_question(state: State):
return {"question": state["question"].strip().lower()}
def make_answer(state: State):
return {"answer": "You asked: " + state["question"]}
Key rule: return only the fields you want to change. Fields you don’t return stay as they were.
3. Edges: the paths between nodes
Edges tell LangGraph which node runs next. Two special markers exist:
- START: where the graph begins
- END: where it finishes
START -> clean_question -> make_answer -> END
Putting it together
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
question: str
answer: str
def clean_question(state: State):
return {"question": state["question"].strip().lower()}
def make_answer(state: State):
return {"answer": "You asked: " + state["question"]}
builder = StateGraph(State)
builder.add_node("clean_question", clean_question)
builder.add_node("make_answer", make_answer)
builder.add_edge(START, "clean_question")
builder.add_edge("clean_question", "make_answer")
builder.add_edge("make_answer", END)
graph = builder.compile()
result = graph.invoke({"question": " What is Python? "})
print(result)
Output:
{'question': 'what is python?', 'answer': 'You asked: what is python?'}
Step-by-step explanation
- invoke() starts with the question ” What is Python? “. The answer field is empty at this point.
- The edge from START sends the state to clean_question, which trims spaces and lowercases the text.
- The updated state moves to make_answer, which reads the cleaned question and writes answer.
- The edge to END finishes the run, and invoke() returns the final state.
Notice that make_answer saw the cleaned question. That is the power of shared state: each node builds on the work of the one before.
Two kinds of edges
- Normal edge: always goes from node A to node B (this chapter).
- Conditional edge: chooses the next node based on state (Chapter 6).
Common beginner mistakes
- Forgetting compile(): you can’t run a builder, only a compiled graph
- Returning the whole state from a node: return only the changes
- Misspelling a node name in add_edge: names must match exactly
- Missing START edge: the graph needs to know where to begin
Summary
- State is the shared data, defined as a TypedDict
- Nodes are functions that read state and return updates
- Edges connect nodes, from START to END
- Nodes only return what changed, and LangGraph merges it into the state
Practice
- Add a third node, add_greeting, that stores “Great question!” in a new feedback field (add the field to State first)
- Change the run order so make_answer runs before clean_question, and observe how the output changes
If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.
For Videos, Join Our YouTube Channel:Â Join Now
Read More:
- Generative AI Tutorial
- AI Ethics
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- ChatGPT Tutorial
- Microsoft Copilot Tutorial
No Comments