LangGraph newbie here.
I am following the doc here: https://docs.langchain.com/langgraph-platform/custom-lifespan
How do i access for example app.state.db_session
when using with langgraph dev
(i.e. local server) if I need to use the db_session
within a graph node?
In langgraph dev
, you can’t access app.state.db_session
directly from node config
since LangGraph nodes run in isolation from the FastAPI app instance. You need to either:
Option 1: Inject into graph input
# Pass db_session when invoking the graph
graph.invoke({"messages": [...], "db_session": app.state.db_session})
def my_node(state, config):
db_session = state["db_session"]
return state
Option 2: Use a service locator/global utility
# Create a global service
class DatabaseService:
db_session = None
# Set in lifespan, access in nodes
def my_node(state, config):
db_session = DatabaseService.db_session
return state
Lifespan objects must be explicitly passed or stored globally since nodes can’t access the FastAPI app state directly.