High severity intermediate · Fix: 2-5 min

KeyError

langgraph.exceptions.StateGraphKeyError

What this error means
LangGraph's StateGraph raised a KeyError because a required state key was missing from the graph's state dictionary.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    graph.run()
  File "/usr/local/lib/python3.9/site-packages/langgraph/core.py", line 128, in run
    state_value = self.state[state_key]
KeyError: 'user_input'
QUICK FIX
Ensure all required state keys are initialized in the state dictionary before running the StateGraph to avoid KeyError.

Why it happens

LangGraph's StateGraph requires all referenced state keys to be present in its internal state dictionary before execution. If a key is missing, typically because it was never initialized or set, the graph raises a KeyError indicating the absent key. This usually happens when the state setup is incomplete or a required input was not provided.

Detection

Add assertions or logging before graph execution to verify all required state keys exist in the state dictionary, catching missing keys early and preventing runtime crashes.

Causes & fixes

1

The required state key was never initialized or set before running the StateGraph.

✓ Fix

Initialize all required state keys with default or input values before calling graph.run(), ensuring the state dictionary contains every key the graph expects.

2

A typo or mismatch in the state key name between the graph definition and the provided state dictionary.

✓ Fix

Verify that all state keys used in the graph exactly match the keys in the state dictionary, including case sensitivity and spelling.

3

Conditional logic in the graph expects a key only under certain conditions, but the key is missing when those conditions occur.

✓ Fix

Add conditional checks or default values for optional keys, or refactor the graph logic to ensure keys are always set before access.

Code: broken vs fixed

Broken - triggers the error
python
from langgraph import StateGraph

graph = StateGraph()
# Missing initialization of 'user_input' key
result = graph.run()  # This line raises KeyError: 'user_input'
Fixed - works correctly
python
import os
from langgraph import StateGraph

# Initialize environment or inputs
os.environ['USER_INPUT'] = 'Hello'

graph = StateGraph()
graph.state['user_input'] = os.environ['USER_INPUT']  # Fixed: set required state key
result = graph.run()
print(result)  # Runs without KeyError
Added explicit initialization of the required 'user_input' state key before running the graph, preventing the KeyError.

Workaround

Wrap the graph.run() call in try/except KeyError, catch the missing key error, log the missing key name, and set a default value before retrying execution.

Prevention

Design your application to always initialize and validate all required state keys before graph execution, and use schema validation or type hints to enforce state completeness.

Python 3.9+ · langgraph >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.