RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /How-to
  5. /How to Define State Schema in LangGraph
HOW-TO · RAG

How to Define State Schema in LangGraph

intermediate·15 min·By Eruo Fredoline
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES

LangGraph installed, Python 3.10+

What this does

The state schema defines what data flows through a LangGraph workflow. Each node reads from and writes to this shared state, with reducers controlling how updates are applied.

Steps

  • Define a basic state schema with TypedDict. Every key is a state field.
from typing import TypedDict

class SearchState(TypedDict):
    query: str
    results: list[str]
    error: str | None
  • Add reducer annotations for append semantics. Use Annotated to specify how updates merge.
from typing import Annotated, TypedDict
from operator import add

class ChatState(TypedDict):
    messages: Annotated[list, add]  # appends new messages
    context: str
    step: int
  • Use add_messages reducer for LangChain message objects. This handles message deduplication by ID.
from typing import TypedDict, Annotated, Sequence
from langgraph.graph.message import add_messages
from langchain_core.messages import BaseMessage

class AgentState(TypedDict):
    messages: Annotated[Sequence[BaseMessage], add_messages]
    next_agent: str
  • Define a default factory for complex types. Prevents key errors on first access.
from typing import TypedDict
from dataclasses import dataclass, field

@dataclass
class WorkflowContext:
    documents: list[str] = field(default_factory=list)
    score: float = 0.0

class AdvancedState(TypedDict):
    query: str
    context: WorkflowContext
  • Inspect state at runtime. Use NodeInterrupt or logging to see state shape.
def inspect_node(state: AgentState) -> dict:
    print(f"State keys: {list(state.keys())}")
    print(f"Messages count: {len(state['messages'])}")
    return {}

Verification

python -c "
from typing import TypedDict, Annotated
from operator import add
class S(TypedDict):
    items: Annotated[list, add]
s: S = {'items': ['a']}
s['items'] = s['items'] + ['b']
print(s['items'])
# Expected: ['a', 'b']
"

Common failures

  • Missing Annotated reducer. Without a reducer, updating a field replaces it instead of merging. This causes data loss in cyclic graphs.
  • Mutability side effects. If a node modifies a list in place, the reducer may not trigger. Always return new values.
  • KeyError on first access. Nodes try to read a key that hasn't been initialized. Provide defaults or use state.get("key", default).
  • Version mismatch - The installed package or runtime differs from the command shown; check the version first and rerun the smallest verification command.
  • Local environment drift - Another service, virtual environment, model, or path is being used; print the active binary path and configuration before changing the guide steps.

Related guides

  • How to Set Up Basic LangGraph Agent from Scratch
  • How to Implement Memory in LangGraph State
← All how-to guidesCourses →