LangGraph Integration
Safety nodes and tools for LangGraph state machines.
Installation
pip install sentinelseed langgraph
Components
| Component | Description |
|---|---|
SentinelSafetyNode | Node that validates state/messages |
SentinelGuardNode | Wrapper that validates before/after node execution |
SentinelAgentExecutor | Wrapper for compiled graphs |
conditional_safety_edge | Conditional edge based on safety state |
sentinel_gate_tool | Tool for agents to self-check actions |
Quick Start
Option 1: Safety Node
from langgraph.graph import StateGraph, MessagesState, START, END
from sentinelseed.integrations.langgraph import (
SentinelSafetyNode,
conditional_safety_edge,
)
safety_node = SentinelSafetyNode(
on_violation="block", # log, block, flag
check_input=True,
)
graph = StateGraph(MessagesState)
graph.add_node("safety_check", safety_node)
graph.add_node("agent", agent_node)
graph.add_node("blocked", blocked_response_node)
graph.add_edge(START, "safety_check")
graph.add_conditional_edges(
"safety_check",
conditional_safety_edge,
{"continue": "agent", "blocked": "blocked"}
)
Option 2: Guard Node
from sentinelseed.integrations.langgraph import SentinelGuardNode
def tool_node(state):
return state
safe_tool_node = SentinelGuardNode(
tool_node,
on_violation="block",
)
graph.add_node("safe_tools", safe_tool_node)
Option 3: Agent Executor
from sentinelseed.integrations.langgraph import SentinelAgentExecutor
app = graph.compile()
executor = SentinelAgentExecutor(
app,
on_violation="block",
max_output_messages=5,
)
result = executor.invoke({"messages": [{"role": "user", "content": "Hello"}]})
State Integration
Safety nodes add these fields to state:
{
"sentinel_safe": bool,
"sentinel_blocked": bool,
"sentinel_violations": list,
"sentinel_risk_level": str, # low, medium, high
}
Configuration
SentinelSafetyNode(
seed_level="standard",
on_violation="log",
check_input=True,
check_output=True,
message_key="messages",
max_text_size=50*1024,
fail_closed=False,
)
THSP Protocol
| Gate | Blocks When |
|---|---|
| TRUTH | Misinformation, fake claims |
| HARM | Violence, dangerous advice |
| SCOPE | Jailbreaks, authority claims |
| PURPOSE | Purposeless destruction |