Chapter 1 · Run a graph without a model
Separate orchestration from model inference first. This chapter normalizes input and produces a receipt without API keys or external services. Once this loop is clear, a model can replace one node without changing the orchestration concepts.
Three pieces of a graph
State is shared data; a node reads it and returns an update; an edge chooses what runs next. START and END are boundary markers, not business functions you implement.
From definition to execution
StateGraph(State) is a builder. compile() produces an executable graph. Pass initial state to invoke(). This example uses the default return format, a state dictionary. Each node returns only its changes; unrelated fields remain intact.
Example
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
text: str
receipt: str
def normalize(state: State) -> dict:
return {"text": state["text"].strip().lower()}
def acknowledge(state: State) -> dict:
return {"receipt": f"Received: {state['text']}"}
builder = StateGraph(State)
builder.add_node("normalize", normalize)
builder.add_node("acknowledge", acknowledge)
builder.add_edge(START, "normalize")
builder.add_edge("normalize", "acknowledge")
builder.add_edge("acknowledge", END)
graph = builder.compile()
result = graph.invoke({"text": " HELLO ", "receipt": ""})
assert result == {"text": "hello", "receipt": "Received: hello"}
print(result)Run and verify
Install langgraph as described in the guide, save as ch01.py, and run python ch01.py. The assertion must pass with hello and Received: hello. This checks graph execution, not LLM capabilities.
Pitfalls
Do not mutate input and return None. Type annotations help developers, but TypedDict does not validate external requests at runtime. Add validation at the service boundary.
🧪 随堂测验
点击你认为正确的选项。答错时会展示正确答案与原因解析。
1. Which call produces the executable graph?
2. Does this chapter need a model key?
3. What should a node return?
4. Does TypedDict automatically validate HTTP input?
Hands-on exercises
- Add an empty-input check; empty text must produce an explicit error.
- Return a character count and assert it uses normalized text.
- Sketch the execution order, swap the nodes, and explain the changed result.
Official references
Verified on: 2026-09-17.