Runnable code samples for aigis v1.3.1.
pip install aigis
# or for all examples:
pip install 'aigis[all]'| File | Description | Extra deps |
|---|---|---|
basic_usage.py |
Core Guard class — input/output scanning, policies, risk scoring |
none |
fastapi_integration.py |
FastAPI middleware + manual check | aigis[fastapi], uvicorn |
langchain_integration.py |
LangChain callback for input + output scanning | aigis[langchain], langchain-openai |
langgraph_integration.py |
LangGraph agent with tool authorization | aigis[langchain], langgraph |
openai_proxy.py |
Drop-in SecureOpenAI wrapper (sync + async) |
aigis[openai] |
custom_policy.py |
YAML policy files, inline overrides, custom rules | aigis[yaml] |
# Basic — no dependencies, no API key needed
python examples/basic_usage.py
# FastAPI server
pip install 'aigis[fastapi]' uvicorn
uvicorn examples.fastapi_integration:app --reload
# then: curl -X POST http://localhost:8000/chat -H "Content-Type: application/json" \
# -d '{"messages": [{"role": "user", "content": "Hello!"}]}'
# LangChain (live LLM calls optional)
pip install 'aigis[langchain]' langchain-openai
OPENAI_API_KEY=sk-... python examples/langchain_integration.py
# LangGraph agent with tool authorization (v1.3.1)
pip install 'aigis[langchain]' langgraph
python examples/langgraph_integration.py
# OpenAI proxy (live API calls optional — guard fires offline)
pip install 'aigis[openai]'
OPENAI_API_KEY=sk-... python examples/openai_proxy.py
# Custom policy
pip install 'aigis[yaml]'
python examples/custom_policy.py- Prompt injection detection (165+ patterns, 25+ threat categories)
- PII detection (credit card, SSN, API keys)
- SQL injection detection
- Policy comparison (
permissivevsdefaultvsstrict) - Output scanning
- Accessing the full
CheckResult(score, reasons, remediation)
AIGuardianMiddlewaresetup- Automatic scan of all POST request bodies
- Accessing
request.state.guardian_resultinside route handlers - Manual
guard.check_messages()as an alternative to middleware - Custom error handler for blocked requests
AIGuardianCallbackwithblock_on_input=True/block_on_output=True- Handling
GuardianBlockedError - LCEL chain integration
- Custom
on_blockedhandler (silent logging instead of raising)
Guard.authorize_tool()— Capability-Based Access Control (CaMeL-inspired)CapabilityStorewith scoped grants and automatic expiryTaintLabel(TRUSTED / UNTRUSTED / SANITIZED) enforcementAtomicPipeline— Scan → Execute → Vaporize as indivisible operationSafetyVerifierwithProofCertificatefor audit trails
SecureOpenAIas a drop-in foropenai.OpenAIscan_response=Truefor output scanningAsyncSecureOpenAIfor async code- One-line migration from
openai.OpenAI
- Built-in policy comparison
- Inline
auto_block_threshold/auto_allow_thresholdoverride - YAML policy file with custom rules
- Combining built-in patterns with custom regex rules
from aigis import Guard
from aigis.capabilities import CapabilityStore, TaintLabel
store = CapabilityStore()
guard = Guard(capabilities=store)
# Grant a scoped capability
store.grant("file:read", scope="*.py", ttl_seconds=3600)
# Authorize a tool call — blocks UNTRUSTED data from control-flow tools
result = guard.authorize_tool(
tool_name="shell:exec",
taint=TaintLabel.UNTRUSTED,
)
print(result.allowed) # False — UNTRUSTED data cannot execute shell commandsfrom aigis.aep import AtomicPipeline
pipeline = AtomicPipeline(guard=guard)
# Scan → Execute → Vaporize (indivisible)
result = pipeline.run(
command="python script.py",
timeout=30,
vaporize=True, # destroy artifacts after execution
)
print(result.stdout)
print(result.sandbox_used) # True
print(result.artifacts_clean) # Truefrom aigis.safety import SafetyVerifier, DEFAULT_SAFETY_SPEC
verifier = SafetyVerifier(spec=DEFAULT_SAFETY_SPEC)
certificate = verifier.verify(
action="file:write",
target="output.txt",
content="safe content",
)
print(certificate.passed) # True
print(certificate.certificate_id) # UUID4
print(certificate.timestamp) # UTC timestamp