Governance middleware for PydanticAI — semantic policy enforcement, trust scoring, and audit trails for agent tool execution
Project description
pydantic-ai-governance
Governance middleware for PydanticAI — semantic policy enforcement, trust scoring, and audit trails for agent tool execution.
Part of the AgentMesh ecosystem.
What This Does
Unlike input/output guardrails that validate LLM I/O, this package enforces what tools are allowed to do based on semantic policy — blocking dangerous operations before they execute.
| Layer | Scope | Example |
|---|---|---|
| Guardrails (#1197) | LLM input/output | "Don't discuss competitor products" |
| Hooks/Traits (#2885/#4303) | Agent lifecycle | Transform PII in messages |
| Governance (this) | Tool execution | "Block rm -rf, limit to 10 tool calls, require trust score > 0.7" |
Features
GovernancePolicy— Pydantic model defining execution limits, blocked patterns, allowed toolsgovern()decorator — Wrap any PydanticAI tool with policy enforcementGovernanceToolset— Apply governance to all tools via PydanticAI'sWrapperToolsetTrustScorer— Multi-dimensional trust tracking (reliability, capability, security, compliance)- Semantic intent classification — Categorize tool calls by threat type, not just keyword matching
- YAML policy files — Version-controlled policies alongside code
- Audit trail — Every policy decision logged with context
Quick Start
from pydantic_ai import Agent
from pydantic_ai_governance import GovernancePolicy, govern, PatternType
policy = GovernancePolicy(
max_tokens_per_request=4096,
max_tool_calls_per_request=10,
blocked_patterns=[
("rm -rf", PatternType.SUBSTRING),
(r".*password.*=.*", PatternType.REGEX),
],
allowed_tools=["search", "read_file"],
)
agent = Agent("openai:gpt-4o")
@agent.tool
@govern(policy)
async def search(ctx, query: str) -> str:
"""Search the web."""
return f"Results for {query}"
GovernanceToolset (apply to all tools)
from pydantic_ai_governance import GovernanceToolset
toolset = GovernanceToolset(policy=policy, tools=[search, read_file])
agent = Agent("openai:gpt-4o", toolsets=[toolset])
Trust Scoring
from pydantic_ai_governance import TrustScorer
scorer = TrustScorer()
scorer.record_success("agent-1", dimensions=["reliability", "security"])
scorer.record_failure("agent-1", dimensions=["compliance"])
score = scorer.get_score("agent-1")
print(f"Trust: {score.overall:.2f}") # 0.0-1.0
YAML Policies
# governance-policy.yaml
max_tokens_per_request: 4096
max_tool_calls_per_request: 10
blocked_patterns:
- pattern: "rm -rf"
type: substring
- pattern: ".*password.*=.*"
type: regex
allowed_tools:
- search
- read_file
confidence_threshold: 0.8
policy = GovernancePolicy.from_yaml("governance-policy.yaml")
How It Differs from Guardrails
See pydantic/pydantic-ai#4335 for the full discussion. Key differences:
- Semantic intent classification — Weighted signal classifier with 9 threat categories
- Policy composition — Hierarchical "most-restrictive-wins" merging
- Multi-agent awareness — Swarm-level anomaly detection
- Deterministic — Zero LLM dependency, sub-millisecond enforcement
License
Apache-2.0
Project details
Release history Release notifications | RSS feed
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 pydantic_ai_agentmesh-3.5.0.tar.gz.
File metadata
- Download URL: pydantic_ai_agentmesh-3.5.0.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: RestSharp/106.13.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9689617e93f04400610f73d06feb9e11508f8d4974105f4afa5ba429b0505bb5
|
|
| MD5 |
50433b718bdcd197e92db57c41ab0df0
|
|
| BLAKE2b-256 |
59e3d8e7070d50dc0ca723148a2b864ad3d8ede3a4ca34faaa07f01401020db9
|
File details
Details for the file pydantic_ai_agentmesh-3.5.0-py3-none-any.whl.
File metadata
- Download URL: pydantic_ai_agentmesh-3.5.0-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: RestSharp/106.13.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61430853ff11a28eb5f6b9b3be892e5034dec9a6911acdfca5b9a2d26922644c
|
|
| MD5 |
da65182ed77a2987b9998c43bac62146
|
|
| BLAKE2b-256 |
19cd02de425a026b3c3ff16e353a50b3d07ef7c428b89f458e16f694412d2e11
|