01Real-world example

A graph is a set of nodes joined by edges. Every node reads the shared state and returns only what it changed.


class State(TypedDict) declares the keys every node can read: tester and greeting.greet() returns only {"greeting": ...}. LangGraph merges that into the state; it never replaces the whole dict.add_edge(START, "greet") and add_edge("greet", END) make the only path. compile() validates it and returns a runnable app.app.get_graph().draw_mermaid() prints the graph you built, so you can check the wiring before you add logic.cd chapter_18_LangGraph/src/chapters
python 001_Hello_Graph.py
Hello Pramod, your first graph ran.
Work in chapter_18_LangGraph/src/chapters. Change one thing, run it, and compare with the expected result.
Run the script and paste the Mermaid text it prints into mermaid.live.
__start__ to greet to __end__.Add role: str to State, pass {"tester": "Pramod", "role": "SDET"} to invoke, and use it in the greeting.
State, the invoke input and the f-string in greet change.Hello Pramod (SDET), your first graph ran. (or however you format it).Make greet return every key, not just greeting. Does the result change?
The exact file from the course repo, plus the output it printed when this page was written.
"""001 - The smallest possible graph: one node between START and END.
State is the backpack. A node is a plain function: it receives the backpack
and returns only the keys it wants to change. LangGraph merges them in.
"""
from typing import TypedDict
from langgraph.graph import END, START, StateGraph
class State(TypedDict):
tester: str
greeting: str
def greet(state: State) -> dict:
return {"greeting": f"Hello {state['tester']}, your first graph ran."}
builder = StateGraph(State)
builder.add_node("greet", greet)
builder.add_edge(START, "greet")
builder.add_edge("greet", END)
app = builder.compile()
if __name__ == "__main__":
result = app.invoke({"tester": "Pramod"})
print(result["greeting"])
print("\nFinal state:", result)
print("\nThe graph as Mermaid (paste into mermaid.live):")
print(app.get_graph().draw_mermaid())
Hello Pramod, your first graph ran.