🌐
LangChain
langchain-ai.github.io › langgraphjs › reference › classes › langgraph.StateGraph.html
StateGraph | LangGraph.js API Reference
StateGraph · Defined in libs/langgraph-core/dist/graph/state.d.ts:116 · constructor · branches channels compiled edges entry · Point? Node nodes waiting · Edges · all · Edges · _ add · Schema add · Conditional · Edges add · Edge add · Node add · Sequence compile validate warn ·
🌐
Langchain
reference.langchain.com › python › langgraph › graph › state › StateGraph
StateGraph | langgraph | LangChain Reference
You must first call .compile() to create an executable graph that supports methods like invoke(), stream(), astream(), and ainvoke(). See the CompiledStateGraph documentation for more details.
🌐
Langchain
docs.langchain.com › oss › python › langgraph › graph-api
Graph API overview - Docs by LangChain
Specify a cache when compiling a graph (or specifying an entrypoint) Specify a cache policy for nodes. Each cache policy supports: key_func used to generate a cache key based on the input to a node, which defaults to a hash of the input with pickle. ttl, the time to live for the cache in seconds. If not specified, the cache will never expire. For example: import time from typing_extensions import TypedDict from langgraph.graph import StateGraph from langgraph.cache.memory import InMemoryCache from langgraph.types import CachePolicy class State(TypedDict): x: int result: int builder = StateGrap
🌐
LangChain
langchain-ai.github.io › langgraphjs › reference › classes › langgraph.CompiledStateGraph.html
CompiledStateGraph | LangGraph.js API Reference
langgraph · CompiledStateGraph · Final result from building and compiling a StateGraph. Should not be instantiated directly, only using the StateGraph .compile() instance method. S · U · N extends string = typeof START · I extends SDZod = StateDefinition ·
🌐
Medium
medium.com › ai-agents › langgraph-for-beginners-part-4-stategraph-794004555369
LangGraph for Beginners, Part 4: StateGraph. | by Santosh Rout | AI Agents | Medium
October 26, 2024 - We can’t define a schema using Graph instance. In order to define a state schema, we wil have to use StateGraph instance. In this tutorial, we will learn ... We will use Colab notebook for this. # Install LangGraph and langchain packages !pip install --quiet -U langgraph
🌐
LangChain
blog.langchain.com › langgraph
LangGraph
January 17, 2024 - from langgraph.graph import StateGraph from typing import TypedDict, List, Annotated import Operator class State(TypedDict): input: str all_actions: Annotated[List[str], operator.add] graph = StateGraph(State)
🌐
Medium
medium.com › @diwakarkumar_18755 › understanding-langgraphs-stategraph-a-simple-guide-020f70fc0038
Understanding LangGraph’s StateGraph: A Simple Guide | by Diwakar Kumar | Medium
March 24, 2025 - The following example demonstrates a simple StateGraph where: The graph starts by adding two numbers. The sum is then multiplied by 2. The final result is displayed. from langgraph.graph import StateGraph, START, END # Define the state using BaseModel class MathState(BaseModel): num1: float num2: float sum_result: float = 0 final_result: float = 0 # Define node functions async def add_numbers(state: MathState) -> MathState: state.sum_result = state.num1 + state.num2 return state async def multiply_result(state: MathState) -> MathState: state.final_result = state.sum_result * 2 return state # I
🌐
GitHub
github.com › langchain-ai › langgraph
GitHub - langchain-ai/langgraph: Build resilient language agents as graphs. Available in TypeScript! · GitHub
4 days ago - Build resilient language agents as graphs. Available in TypeScript! - langchain-ai/langgraph
Starred by 31.2K users
Forked by 5.3K users
Languages   Python
Find elsewhere
🌐
Baihezi
baihezi.com › mirrors › langgraph › reference › graphs › index.html
Graphs - LangGraph
msgs1 = [HumanMessage(content="Hello", id="1")] msgs2 = [AIMessage(content="Hi there!", id="2")] add_messages(msgs1, msgs2) # [HumanMessage(content="Hello", id="1"), AIMessage(content="Hi there!", id="2")] msgs1 = [HumanMessage(content="Hello", id="1")] msgs2 = [HumanMessage(content="Hello again", id="1")] add_messages(msgs1, msgs2) # [HumanMessage(content="Hello again", id="1")] from typing import Annotated from typing_extensions import TypedDict from langgraph.graph import StateGraph class State(TypedDict): messages: Annotated[list, add_messages] builder = StateGraph(State) builder.add_node("chatbot", lambda state: {"messages": [("assistant", "Hello")]}) builder.set_entry_point("chatbot") builder.set_finish_point("chatbot") graph = builder.compile() graph.invoke({}) # {'messages': [AIMessage(content='Hello', id='f657fb65-b6af-4790-a5b5-1d266a2ed26e')]}
🌐
Medium
medium.com › @gitmaxd › understanding-state-in-langgraph-a-comprehensive-guide-191462220997
Understanding State in LangGraph: A Beginners Guide 🚀 | by Rick Garcia | Medium
August 17, 2024 - from langgraph.graph import StateGraph, END def create_simple_graph(): workflow = StateGraph(BasicState) def increment_node(state: BasicState): return {"count": state["count"] + 1} workflow.add_node("increment", increment_node) workflow.set_entry_point("increment") workflow.add_edge("increment", END) return workflow.compile()
🌐
Kitemetric
kitemetric.com › blogs › visualizing-langgraph-workflows-with-get-graph
LangGraph Visualization: Mastering StateGraph | Kite Metric
from IPython.display import Image from typing_extensions import TypedDict from langgraph.graph import StateGraph, START, END class State(TypedDict): "State to manage all chats" class MyNode: "Custom Node" # Initialize the graph builder with a state builder = StateGraph(State) # Add a custom node builder.add_node("myNode", MyNode) # Define the edges for transitions builder.add_edge(START, "myNode") builder.add_edge("myNode", END) # Compile the graph chat_graph = builder.compile()
🌐
Langchain
reference.langchain.com › javascript › classes › _langchain_langgraph.index.CompiledStateGraph.html
CompiledStateGraph | langchain.js
@langchain/langgraph · index · ... only using the StateGraph .compile() instance method. S · The full state type representing the complete shape of your graph's state after all reducers have been applied....
🌐
PyPI
pypi.org › project › langgraph › 0.0.23
langgraph · PyPI
from langgraph.graph import StateGraph, END # Define a new graph workflow = StateGraph(AgentState) # Define the two nodes we will cycle between workflow.add_node("agent", call_model) workflow.add_node("action", call_tool) # Set the entrypoint as `agent` # This means that this node is the first one called workflow.set_entry_point("agent") # We now add a conditional edge workflow.add_conditional_edges( # First, we define the start node.
      » pip install langgraph
    
Published   Feb 04, 2024
Version   0.0.23
🌐
Prem AI
blog.premai.io › langgraph-deep-dive-state-machines-tools-and-human-in-the-loop
LangGraph Deep Dive: State Machines, Tools, and Human-in-the-Loop
March 16, 2026 - from langgraph.graph import StateGraph, START, END from langgraph.graph.message import add_messages from typing import Annotated, TypedDict class AgentState(TypedDict): messages: Annotated[list, add_messages] # Build graph graph = StateGraph(AgentState) graph.add_node("agent", agent) graph.add_node("tools", tool_node) # Edges graph.add_edge(START, "agent") graph.add_conditional_edges( "agent", should_use_tool, {"tools": "tools", "end": END} ) graph.add_edge("tools", "agent") # After tool, back to agent app = graph.compile()
🌐
Reddit
reddit.com › r/langchain › question related to graphs
r/LangChain on Reddit: Question related to Graphs
October 2, 2024 -

Hi Guys,

I'm dabbling around in Langgraph and running into an issue at this point.

I am trying to make a first node in my graph that should decide if someone is just small talking or actually asking a RAG specific question. It should make that decision based on the question and memory. I've try to implement this and it works if do this only on the question, but id like to do it also based on memory.

Here is my implementation:

from typing import TypedDict
from langgraph.graph import StateGraph
from langgraph.graph import Graph

class AgentState(TypedDict):
messages: list[str]
workflow = StateGraph(AgentState)

def agent(question, memory):
res = llm.invoke(f"""You are given an interaction with a user so far and the final question. Use these to decide if the user is interested in small talk or that it want to know something specifically pension related.
If it's related to small talk, return "Small Talk"
If it's related to pensions, return "Pension"
Only return either of these values and nothing else

Here is the question:
{question}

Here is the full conversation:
{memory}
""")
return res.content

from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
workflow = Graph()
workflow.add_node("agent", agent)
workflow.add_edge(START, "agent")
workflow.add_edge("agent", END)
graph = workflow.compile()
graph.invoke('How are you?', 'Nothing')

this returns:
AttributeError: 'str' object has no attribute 'items'

is there an issue with what i defined in my class?

Any help would be sweet! Thanks in advance

Top answer
1 of 1
3
Hi there! The issue in your graph is that it is accepting two arguments, when it should just be accepting a single state argument. You will need to modify your state in some way, perhaps by adding a question key like so: from typing import TypedDict from langgraph.graph import StateGraph, START, END from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-4o-mini") class AgentState(TypedDict): question: str messages: list[str] workflow = StateGraph(AgentState) def agent(state): res = llm.invoke(f"""You are given an interaction with a user so far and the final question. Use these to decide if the user is interested in small talk or that it want to know something specifically pension related. If it's related to small talk, return "Small Talk" If it's related to pensions, return "Pension" Only return either of these values and nothing else Here is the question: {state['question']} Here is the full conversation: {state['messages']} """) return {"messages": state['messages']+[res.content]} workflow = StateGraph(AgentState) workflow.add_node("agent", agent) workflow.add_edge(START, "agent") workflow.add_edge("agent", END) graph = workflow.compile() graph.invoke({"question":"How are you?","messages":["How are you?"]}) If you want to explore more about how memory can persist, check out these guides: long term memory and persistence . In addition, be sure to read the conceptual guides, linked here to get a better understanding of how LangGraph is designed with memory in mind.