LangGraph Integration Guide
Add formal runtime invariants and preconditions to LangGraph StateGraph nodes. Intercept state updates before edge transitions occur.
Using the LangGraph Contract Wrapper
Wrap your graph node execution functions with `ContractNode` to automatically validate state invariants and catch drift before continuing along the graph.
langgraph_example.py
from langgraph.graph import StateGraph, END from agentassert_abc.integrations.langgraph import ContractNode from agentassert_abc import Contract # 1. Load contract contract = Contract.from_yaml("contracts/financial-advisor.yaml") # 2. Define node function def advisor_node(state): # Standard LangGraph logic return {"response": "Your portfolio summary is ready."} # 3. Create StateGraph with ContractNode wrapper workflow = StateGraph(dict) workflow.add_node("advisor", ContractNode(advisor_node, contract=contract)) workflow.set_entry_point("advisor") workflow.add_edge("advisor", END) app = workflow.compile() output = app.invoke({"input": "Show my balance"})