AI agent memory governance — preflight validation before every action. Block hallucinations, stop drift, prove what's safe to act on.
Project description
sgraal
Python SDK for the Sgraal memory governance protocol.
Install
pip install sgraal
Quick Start
from sgraal import SgraalClient
client = SgraalClient(api_key="sg_live_...")
# Or set SGRAAL_API_KEY environment variable
result = client.preflight(
memory_state=[{
"id": "mem_001",
"content": "Customer prefers email communication",
"type": "preference",
"timestamp_age_days": 12,
"source_trust": 0.9,
"source_conflict": 0.1,
"downstream_count": 3,
}],
action_type="irreversible",
domain="fintech",
)
print(result["recommended_action"]) # USE_MEMORY, WARN, ASK_USER, or BLOCK
print(result["omega_mem_final"]) # Risk score 0-100
print(result["assurance_score"]) # Confidence score
Edge Mode (No Network Required)
For edge deployments, medical devices, offline use, or situations where you can't reach api.sgraal.com:
from sgraal.edge import edge_preflight
result = edge_preflight([{
"id": "m1", "content": "User payment info", "type": "tool_state",
"timestamp_age_days": 3, "source_trust": 0.7, "source_conflict": 0.3,
}], domain="fintech", action_type="irreversible")
print(result["decision"]) # USE_MEMORY | WARN | ASK_USER | BLOCK
print(result["omega"]) # Risk score 0-100
Edge mode uses only the 5-signal fast path with zero dependencies — no Redis, no Supabase, no HTTP. Built-in per-type thresholds (identity=13, tool_state=47, etc.). Runs in ~0.2ms on a Raspberry Pi.
Sign Up
from sgraal import SgraalClient
# No API key needed for signup
import requests
resp = requests.post("https://api.sgraal.com/v1/signup", json={"email": "you@company.com"})
data = resp.json()
print(data["api_key"]) # Save this — shown only once
Or use the client:
from sgraal import SgraalClient
client = SgraalClient.__new__(SgraalClient)
client.api_key = ""
client.base_url = "https://api.sgraal.com"
result = client.signup("you@company.com")
print(result["api_key"])
Guard Decorator
Block or warn before executing functions that depend on memory:
from sgraal import guard
@guard(
memory_state=[{
"id": "mem_billing",
"content": "Customer is on Growth plan",
"type": "tool_state",
"timestamp_age_days": 30,
"source_trust": 0.95,
"source_conflict": 0.05,
"downstream_count": 5,
}],
action_type="irreversible",
domain="fintech",
block_on="BLOCK",
)
def charge_customer(customer_id: str, amount: float):
"""Only runs if memory passes preflight check."""
process_payment(customer_id, amount)
Dynamic Memory State
Pass a callable that receives the same arguments as the wrapped function:
@guard(
memory_state=lambda customer_id, amount: fetch_memories(customer_id),
action_type="irreversible",
domain="fintech",
block_on="BLOCK",
)
def charge_customer(customer_id: str, amount: float):
process_payment(customer_id, amount)
Block Levels
block_on="BLOCK"— only block on BLOCK (default)block_on="ASK_USER"— block on BLOCK and ASK_USERblock_on="WARN"— block on BLOCK, ASK_USER, and WARN
When blocked, raises SgraalBlockedError. WARN and ASK_USER log warnings when not blocked.
from sgraal.guard import SgraalBlockedError
try:
charge_customer("cus_123", 99.00)
except SgraalBlockedError as e:
print(f"Blocked: {e.result['block_explanation']}")
LangGraph Integration
from sgraal import SgraalClient
client = SgraalClient()
def preflight_node(state):
"""LangGraph node that checks memory before proceeding."""
result = client.preflight(
memory_state=state["memories"],
action_type=state.get("action_type", "reversible"),
domain=state.get("domain", "general"),
)
if result["recommended_action"] == "BLOCK":
return {**state, "blocked": True, "reason": result["block_explanation"]}
return {**state, "blocked": False, "omega_score": result["omega_mem_final"]}
GeminiGuard
from sgraal import GeminiGuard
guard = GeminiGuard(
sgraal_api_key="sg_live_...",
gemini_api_key="...",
model="gemini-1.5-flash",
)
# Automatically checks memory before calling Gemini
# BLOCK → returns block message without calling Gemini
# WARN → adds warning context to Gemini prompt
# USE_MEMORY → calls Gemini normally
response = guard.check_and_generate(
"What is the user's shipping address?",
memory_data=[{
"id": "mem_addr",
"content": "User address: 123 Main St",
"type": "preference",
"timestamp_age_days": 30,
"source_trust": 0.9,
"source_conflict": 0.1,
"downstream_count": 2,
}],
action_type="irreversible",
domain="customer_support",
)
Requires: pip install google-generativeai
OpenAIGuard
from sgraal import OpenAIGuard
guard = OpenAIGuard(
sgraal_api_key="sg_live_...",
openai_api_key="...",
model="gpt-4",
)
response = guard.check_and_generate(
"Summarize the contract terms",
memory_data=[{
"id": "mem_contract",
"content": "Liability capped at €500K",
"type": "tool_state",
"timestamp_age_days": 90,
"source_trust": 0.7,
"source_conflict": 0.4,
"downstream_count": 5,
}],
action_type="irreversible",
domain="legal",
)
Requires: pip install openai
License
Apache 2.0
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sgraal-0.3.2.tar.gz.
File metadata
- Download URL: sgraal-0.3.2.tar.gz
- Upload date:
- Size: 25.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c5f67ea92a4c5c1be20b84c98f0bdaf8b8797e179b601a0aea3a73debea8d34
|
|
| MD5 |
0f62f22c625680375539fce97ebc2593
|
|
| BLAKE2b-256 |
680bbb58c925e21de88c17e9fc873cab3293fa2458671e6e75b01014db60778e
|
File details
Details for the file sgraal-0.3.2-py3-none-any.whl.
File metadata
- Download URL: sgraal-0.3.2-py3-none-any.whl
- Upload date:
- Size: 30.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
446481b20d468cc2e36d2c25aedf74f1293ac03e4e545d44ea38576359092834
|
|
| MD5 |
aead0dac085add591470180dd0299da1
|
|
| BLAKE2b-256 |
0c4553bdf58e143838b664936efdfbbda505884d9fc4bb1910b6c32659d5d01e
|