State

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

State

Another important aspect of LangGraph is the concept of State. The basic graph example in the previous section didn’t use any state except for the output passed from one node to be used as the input for the next. The problem with this, though, is that it’s difficult to access the state of a node that came much earlier in the graph. In a two-node graph, it’s no problem, but what if you had ten nodes? How would the eighth node know what the second node did?

from typing import TypedDict

class MyState(TypedDict):
  key_1: list[str]
  key_2: int
from typing import TypedDict, Annotated
from operator import add

class MyState(TypedDict):
  key_1: Annotated[list[str], add]
  key_2: int
from langgraph.graph import StateGraph

graph = StateGraph(MyState)
See forum comments
Download course materials from Github
Previous: Graphs Demo Next: State Demo