liveBook · Manning

StateGraph

As explained in Build Python Web Apps with Streamlit

StateGraph is a LangGraph graph-builder API for defining nodes, state, and transitions in a stateful computation graph.

To turn an LLM call into a chatbot workflow, use `StateGraph` to build a graph whose nodes read and update shared state. Start by defining the state: a `TypedDict` can specify a `messages` list, though it is not the only supported way to represent state. section 10.2.3

A one-node graph follows a short sequence:

  • Create a builder with `StateGraph(MyGraphState)`. section 10.2.3
  • Add a Python function as the assistant node. It receives the current state, calls the LLM with `state["messages"]`, and returns the state keys it wants to update. section 10.2.3
  • Connect `START` to the assistant and the assistant to `END`; compile the builder before invoking the graph with an initial state. section 10.2.3
Listing 10.1 An example of a graph
from langgraph.graph import START, END, StateGraph
from langchain_core.messages import AnyMessage, HumanMessage
from langchain_openai import ChatOpenAI
from typing import TypedDict

llm = ChatOpenAI(model_name="gpt-5.1", openai_api_key="sk-proj-...")

class MyGraphState(TypedDict):
  messages: list[AnyMessage]

builder = StateGraph(MyGraphState)

def assistant_node(state):
  messages = state["messages"]
  ai_response_message = llm.invoke(messages)
  return {"messages": messages + [ai_response_message]}

builder.add_node("assistant", assistant_node)
builder.add_edge(START, "assistant")
builder.add_edge("assistant", END)
graph = builder.compile()

input_message = input("Talk to the bot: ")
initial_state = {"messages": [HumanMessage(content=input_message)]}
final_state = graph.invoke(initial_state)

print("Bot:\n" + final_state["messages"][-1].content)

The complete listing puts the state definition, node, edges, compilation, and a single chatbot exchange together. section 10.2.3

A node returns only the keys it wants to modify, even when a one-key state makes its return value look like the whole state. The chatbot version also uses LangGraph’s built-in `MessagesState` when wrapping the graph for a Streamlit interface. section 10.2.3

How Build Python Web Apps with Streamlit covers it

How deep it goes
Works through state definition, node updates, edges, compilation, and invocation; stops at a single assistant node.
Examples
Uses a command-line chatbot graph and a Streamlit single-prompt, single-response chatbot.
What it assumes
Assumes basic Python dictionaries and functions while explaining TypedDict as a way to define state fields.
What you can do after
Build it from scratch: compile and invoke a one-node graph that returns an LLM response.
How it connects
Connects TypedDict and MessagesState to graph state, and Python functions and LLM calls to nodes.

Also explained in

Compare how 2 books explain StateGraph →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.