Skip to main content

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 tools
  • govern() decorator — Wrap any PydanticAI tool with policy enforcement
  • GovernanceToolset — Apply governance to all tools via PydanticAI's WrapperToolset
  • TrustScorer — 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:

  1. Semantic intent classification — Weighted signal classifier with 9 threat categories
  2. Policy composition — Hierarchical "most-restrictive-wins" merging
  3. Multi-agent awareness — Swarm-level anomaly detection
  4. Deterministic — Zero LLM dependency, sub-millisecond enforcement

License

Apache-2.0

Metadata

Release files for pydantic_ai_agentmesh 3.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic_ai_agentmesh 3.6.0
File Size Uploaded Provenance Actions
pydantic_ai_agentmesh-3.6.0.tar.gz 13.6 kB Without provenance Details for pydantic_ai_agentmesh-3.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pydantic_ai_agentmesh 3.6.0
File Interpreter ABI Platform Actions
pydantic_ai_agentmesh-3.6.0-py3-none-any.whl Python 3 none any Details for pydantic_ai_agentmesh-3.6.0-py3-none-any.whl

Total release size: 28.1 kB

Release files / pydantic_ai_agentmesh-3.6.0.tar.gz

Download URL pydantic_ai_agentmesh-3.6.0.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
164cd37b4320566f1f0b1ed28fa94764a5755f9080f876e317891b6fc2323783
BLAKE2b-256 checksum
How to use checksums
d4831779b903d616232fe509b288b48a1005be69196c5ea85c47b650ede59f1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via RestSharp/106.13.0.0

Release files / pydantic_ai_agentmesh-3.6.0-py3-none-any.whl

Download URL pydantic_ai_agentmesh-3.6.0-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90025b06401bcbf35f319ea18fee6a171e98d82c619b03e03e3640ba9b759ce3
BLAKE2b-256 checksum
How to use checksums
efdd689c84c3029b6f38d56ae56fffc2d936cf03835ebdaea905f18384418430
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via RestSharp/106.13.0.0

Security for pydantic_ai_agentmesh 3.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page