Skip to main content

LangChain / LangGraph integration for comply54 — African AI agent compliance enforcement

Project description

langchain-comply54

LangChain and LangGraph integration for comply54 — runtime compliance enforcement for AI agents operating under African data protection and financial-sector regulations.

Overview

langchain-comply54 wraps the comply54 policy engine as a first-class LangGraph node and LangChain tool. It intercepts agent tool calls before execution, evaluates them against African regulatory frameworks (NDPA 2023, CBN, NFIU, Ghana DPA, Kenya DPA, and 15+ more), and either blocks the call or lets it through — all in-process, with no OPA binary or external service required.

Agent → Comply54Guard → [deny] → ToolMessage error back to agent
                      → [allow] → Tools execute normally

Installation

pip install langchain-comply54

Quick Start — LangGraph Guard Node

The recommended pattern is Comply54Guard as a pre-execution node in a LangGraph ReAct graph:

from langchain_comply54 import Comply54Guard, comply54_route
from comply54.sectors import NigeriaFintechCompliance
from langgraph.graph import StateGraph, MessagesState, END
from langgraph.prebuilt import ToolNode

# 1. Build your tools and agent as normal
tools = [transfer_funds_tool, lookup_account_tool]
tool_node = ToolNode(tools)
agent_node = build_agent(tools)  # your LLM + tools

# 2. Create the guard
guard = Comply54Guard(
    NigeriaFintechCompliance(),
    context={"kyc_tier": 3, "customer_verified": True},
)

# 3. Wire it into the graph between agent and tools
graph = StateGraph(MessagesState)
graph.add_node("agent", agent_node)
graph.add_node("comply54_guard", guard)
graph.add_node("tools", tool_node)

graph.set_entry_point("agent")
graph.add_conditional_edges("agent", should_continue,
    {"comply54_guard": "comply54_guard", END: END})
graph.add_conditional_edges("comply54_guard", comply54_route,
    {"tools": "tools", "agent": "agent"})
graph.add_edge("tools", "agent")

app = graph.compile()

When the agent attempts a tool call that violates compliance policy, Comply54Guard injects a ToolMessage with a structured denial that the agent can explain to the user:

{
  "blocked": true,
  "decision": "deny",
  "reason": "CBN NIP Framework: single transfer of ₦15,000,000 exceeds the ₦10,000,000 per-transaction cap",
  "regulation": "CBN Transaction Limits & Controls",
  "audit_id": "a3f2c1d4-...",
  "jurisdictions": ["NG"]
}

Compliance Decision Outcomes

Every evaluation returns one of four outcomes:

Decision Meaning Guard behaviour
allow All checks passed Tool executes normally
audit Action logged for regulatory trail Tool executes; state gets compliance_result
escalate Elevated-risk action — human review recommended Tool executes by default; blocked if block_on_escalate=True
deny Hard regulatory violation Tool blocked; agent receives structured ToolMessage error

Available Sector Packs

Import Jurisdictions Key Regulations
NigeriaFintechCompliance NG NDPA 2023, CBN NIP (₦10M cap), NFIU AML/CFT, BVN/NIN
NigeriaHealthCompliance NG NDPA 2023, NHA, NHIA — patient data & health records
NigeriaInsuranceCompliance NG NDPA 2023, NIIRA 2025 (replaces Insurance Act 2003)
KenyaFintechCompliance KE Kenya Data Protection Act 2019, CBK guidelines
PanAfricanCompliance NG, KE, GH, RW, EG, MU, ZA, TZ, UG 9-jurisdiction pack
from comply54.sectors import (
    NigeriaFintechCompliance,
    NigeriaHealthCompliance,
    NigeriaInsuranceCompliance,
    KenyaFintechCompliance,
    PanAfricanCompliance,
)

Patterns

Block on escalate

guard = Comply54Guard(
    NigeriaFintechCompliance(),
    block_on_escalate=True,  # treat escalate as deny
)

Per-request context from agent state

Merge dynamic context (e.g. session KYC tier, consent flags) at evaluation time:

# Set compliance_context in your agent state before the guard runs
state["compliance_context"] = {
    "kyc_tier": user.kyc_tier,
    "customer_verified": user.is_verified,
    "consent_documented": user.has_data_transfer_consent,
}

LangChain StructuredTool (self-check pattern)

For agents that should self-check before acting rather than being intercepted automatically:

from langchain_comply54 import comply54_tool
from comply54.sectors import NigeriaFintechCompliance

# The agent can call this tool to ask "is this action allowed?"
compliance_check = comply54_tool(NigeriaFintechCompliance())
tools = [transfer_funds_tool, compliance_check]

Legacy state-dict node

For LangGraph graphs that pass structured state rather than messages:

from langchain_comply54 import compliance_node
from comply54.sectors import NigeriaFintechCompliance

node = compliance_node(NigeriaFintechCompliance())
# state must contain: action, params, output, context
# node writes back: compliance_result, compliance_blocked, compliance_message

Compliance Certificate

Every evaluation produces an auditor-exportable compliance certificate:

result = guard._compliance.check(action="transfer_funds", params={"amount": 5_000_000})
cert = result.to_certificate(
    sector_pack="NigeriaFintechCompliance",
    jurisdictions=["NG"],
    regulations=["NDPA 2023", "CBN FPR"],
)
print(cert.to_json())

Requirements

  • Python 3.9+
  • comply54[langgraph] >= 0.3.0 (installed automatically)
  • No OPA binary, no Docker, no external services

Links

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

langchain_comply54-0.3.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_comply54-0.3.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file langchain_comply54-0.3.0.tar.gz.

File metadata

  • Download URL: langchain_comply54-0.3.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for langchain_comply54-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8b1a23994d59100a7d26cbefac530b6cd2217c278cbe2b58e8b2ddc5a2c3a089
MD5 4a08300e083c47f5382c5a22a74e6c80
BLAKE2b-256 90b21fa045ca1ac0a595b379e142a366f2b6dc8ebd81c8cca53319ac61686c53

See more details on using hashes here.

File details

Details for the file langchain_comply54-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_comply54-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1d73ec003b02d7b0d833cecea5cdee8ef5b791004d8c45a02e4b436007a553e8
MD5 7dd2e50fd6252dc1dfb3bd7f7c337b0d
BLAKE2b-256 6b1c74340247429e5c8516f31aa414c78a81246df31f3b77edc954b52609318a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page