Skip to main content

Pydantic AI integration for LatchGate — execution security kernel for AI agents

Project description

latchgate-pydantic-ai

Pydantic AI integration for LatchGate — execution security kernel for AI agents.

Every tool call goes through LatchGate's enforcement pipeline: auth => policy => WASM sandbox => verification => signed receipt. The LLM never holds credentials and never contacts external systems directly.

Installation

pip install latchgate-pydantic-ai

Requires a running LatchGate instance:

curl -fsSL https://raw.githubusercontent.com/latchgate-ai/latchgate/main/install.sh | bash && latchgate up

Quick start

from pydantic_ai import Agent
from latchgate_pydantic_ai import LatchGateToolset

async def main():
    async with await LatchGateToolset.create(gate_url="http://localhost:3000") as toolset:
        agent = Agent(
            "openai:gpt-4o",
            instructions="You have access to LatchGate-protected tools with full audit trail.",
            toolsets=[toolset],
        )

        result = await agent.run("Fetch https://httpbin.org/get")
        print(result.output)

API

LatchGateToolset

Native Pydantic AI AbstractToolset implementation. Discovers actions and provides them to the agent via toolsets=[...].

toolset = await LatchGateToolset.create(
    gate_url="http://localhost:3000",    # Required (or set LATCHGATE_URL)
    agent_id="my-agent",                 # Default: "pydantic-ai"
    include={"http_fetch", "database"},  # Optional
    exclude={"send_message"},            # Optional
    on_audit=my_audit_callback,          # Optional: receipt callback
)

agent = Agent("openai:gpt-4o", toolsets=[toolset])

# Properties:
toolset.action_ids   # list[str]
toolset.client       # LatchGateClient

Use as an async context manager for automatic cleanup:

async with await LatchGateToolset.create(gate_url="...") as toolset:
    agent = Agent("openai:gpt-4o", toolsets=[toolset])

Or construct from pre-fetched descriptors:

toolset = LatchGateToolset(client=client, descriptors=descriptors)

discover_actions(gate_url, **kwargs)

Low-level discovery:

from latchgate_pydantic_ai import discover_actions

descriptors = await discover_actions("http://localhost:3000", include={"http_fetch"})

Error handling

LatchGate errors are returned as structured strings (not raised):

LatchGate error Tool returns
Policy denied "ERROR: Action '...' denied: {reason}..."
Approval required "ERROR: ... requires human approval..." (approval_id emitted via log, not to the model)
Budget exhausted "ERROR: Budget exhausted..."
Unknown action "ERROR: Unknown LatchGate action '...'."
Transport / infra "ERROR: LatchGate error..."

Output format

Tool output is a JSON string containing only the action result. Enforcement metadata (receipt ID, trace ID, verification) is never returned to the model — it is emitted at INFO log level and via the optional on_audit callback.

{"status": 200, "body": "{...}"}

Why AbstractToolset?

Pydantic AI's AbstractToolset is the native interface for external tool collections. It provides get_tools() for schema discovery and call_tool() for execution — matching LatchGate's discovery + execute pattern perfectly. No schema-to-Pydantic model conversion needed; JSON Schemas pass through directly via ToolDefinition.parameters_json_schema.

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

latchgate_pydantic_ai-0.1.1.tar.gz (229.3 kB view details)

Uploaded Source

Built Distribution

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

latchgate_pydantic_ai-0.1.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file latchgate_pydantic_ai-0.1.1.tar.gz.

File metadata

  • Download URL: latchgate_pydantic_ai-0.1.1.tar.gz
  • Upload date:
  • Size: 229.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for latchgate_pydantic_ai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dc995551b0aba83a5cbbd04daf59f46942d6041c9a95b05cb33c30a6c79edea7
MD5 8899cd111f3a21676ff7aed381140f37
BLAKE2b-256 713bfcaa28666c32d66c55838d70a3c91c2be617d81d9ed96c22ff1c0645d215

See more details on using hashes here.

Provenance

The following attestation bundles were made for latchgate_pydantic_ai-0.1.1.tar.gz:

Publisher: release.yml on latchgate-ai/latchgate-integrations

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file latchgate_pydantic_ai-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for latchgate_pydantic_ai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b8cb9daae827b3f44a127084ffc14013c2817e4ed9b2875cc7b9a32e555f230e
MD5 f5d31c4116a75c6c4f84b45d6318e93c
BLAKE2b-256 fbdbf196a6e1be80ecca0a5bce4698f87dc7938ef4e290ed60ab94fdc469044f

See more details on using hashes here.

Provenance

The following attestation bundles were made for latchgate_pydantic_ai-0.1.1-py3-none-any.whl:

Publisher: release.yml on latchgate-ai/latchgate-integrations

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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