DSPy Integration
Integrate Sentinel's THSP safety validation into DSPy pipelines.
Installation
pip install dspy sentinelseed
Overview
DSPy is Stanford's framework for programming language models. This integration adds safety validation to DSPy modules, ensuring outputs pass through THSP gates.Quick Start
import dspy
from sentinelseed.integrations.dspy import SentinelGuard
dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))
base_module = dspy.ChainOfThought("question -> answer")
safe_module = SentinelGuard(
base_module,
api_key="sk-...",
mode="block"
)
result = safe_module(question="What is machine learning?")
print(result.answer)
print(result.safety_passed) # True
Components
SentinelGuard
Wraps any DSPy module and validates output.
from sentinelseed.integrations.dspy import SentinelGuard
guard = SentinelGuard(
module,
api_key="...",
provider="openai", # or "anthropic"
mode="block", # block, flag, heuristic
)
SentinelChainOfThought
Validates both reasoning AND output.
from sentinelseed.integrations.dspy import SentinelChainOfThought
cot = SentinelChainOfThought(
"problem -> solution",
api_key="...",
validate_reasoning=True,
validate_output=True,
)
result = cot(problem="...")
print(result.safety_fields_validated) # ["reasoning", "solution"]
Tools for ReAct
from sentinelseed.integrations.dspy import create_sentinel_tool
safety_tool = create_sentinel_tool(api_key="...")
agent = dspy.ReAct("task -> result", tools=[safety_tool])
Output Metadata
result.safety_passed # bool: Did content pass?
result.safety_gates # dict: Individual gate results
result.safety_reasoning # str: Explanation
result.safety_method # str: "semantic" or "heuristic"
Validation Modes
| Mode | Description |
|---|---|
| Semantic | LLM-based, ~90% accuracy |
| Heuristic | Pattern-based, ~50% accuracy, no API needed |
Agent Modules
SentinelToolValidator
Validates tool calls before execution.
from sentinelseed.integrations.dspy import SentinelToolValidator
validator = SentinelToolValidator(api_key="...")
@validator.wrap
def search_web(query: str) -> str:
return web_search(query)
SentinelAgentGuard
Validates each step of agent execution.
from sentinelseed.integrations.dspy import SentinelAgentGuard
safe_agent = SentinelAgentGuard(
agent,
api_key="...",
validate_input=True,
validate_steps=True,
validate_output=True,
)