Skip to content

Pydantic AI

Gate every Pydantic AI tool call through LatchGate’s enforcement pipeline.

Terminal window
pip install latchgate-pydantic-ai
from pydantic_ai import Agent
from latchgate_pydantic_ai import LatchGateToolset
async def main():
toolset = await LatchGateToolset.create(gate_url="http://localhost:3000")
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)

LatchGateToolset.create() discovers all registered LatchGate actions via the REST API and provides each one as a Pydantic AI tool with the action’s JSON Schema passed directly to parameters_json_schema. When the LLM calls a tool, the toolset routes it through LatchGateClient.execute() — the full enforcement pipeline runs transparently.

toolset = await LatchGateToolset.create(
gate_url="http://localhost:3000",
include={"http_fetch", "github_read"},
exclude={"send_message"},
)
from latchgate import LatchGateClient
client = LatchGateClient(socket="/run/latchgate/gate.sock", agent_id="my-pydantic-agent")
toolset = await LatchGateToolset.create(client=client)

Use on_audit for programmatic access to receipt metadata after each successful execution:

def my_audit_handler(record):
# record.action_id, record.receipt_id, record.trace_id, record.verification
audit_log.store(record)
toolset = await LatchGateToolset.create(
gate_url="http://localhost:3000",
on_audit=my_audit_handler,
)

You can set LATCHGATE_URL instead of passing gate_url explicitly:

# With LATCHGATE_URL=http://localhost:3000 set:
toolset = await LatchGateToolset.create()

Control the HTTP timeout for the discovery phase (default 15 seconds):

toolset = await LatchGateToolset.create(
gate_url="http://localhost:3000",
discovery_timeout=10.0,
)

Full API reference: latchgate-integrations/pydantic/README.md.