Pydantic AI
Instrument a Pydantic AI agent with VeriProof governance tracing. Captures model calls, tool invocations, and session-level metadata, with full EU AI Act compliance attributes available.
PythonPydantic AIPython ≥ 3.10
Prerequisites
pip install veriproof-sdk veriproof-sdk-instrumentation-pydantic-ai pydantic-aiEnvironment
# .env
VERIPROOF_API_KEY=vp_cust_your-tenant.<key-material>
VERIPROOF_APPLICATION_ID=my-ai-app
ANTHROPIC_API_KEY=sk-ant-...Complete example
import asyncio
import os
from veriproof_sdk import (
configure_veriproof,
VeriproofClientOptions,
DecisionType,
RiskLevel,
SessionIntent,
AgentRole,
)
from veriproof_sdk_instrumentation_pydantic_ai import VeriproofPydanticAISession
from pydantic_ai import Agent
from pydantic_ai.models.anthropic import AnthropicModel
# 1. Configure VeriProof once at application startup
configure_veriproof(
VeriproofClientOptions(
api_key=os.environ["VERIPROOF_API_KEY"],
application_id=os.environ["VERIPROOF_APPLICATION_ID"],
),
service_name=os.environ["VERIPROOF_APPLICATION_ID"],
set_global=True,
)
# 2. Create the Pydantic AI agent once (reuse across requests)
agent = Agent(
AnthropicModel("claude-3-5-haiku-20241022"),
system_prompt=(
"You are a credit risk analyst. "
"Assess the creditworthiness of the applicant and recommend APPROVE or DECLINE."
),
)
async def assess_loan_application(applicant_id: str, credit_score: int) -> dict:
# 3. Configure governance attributes for this session
decision = DecisionType.APPROVAL if credit_score >= 680 else DecisionType.DENIAL
risk = RiskLevel.LOW if credit_score >= 720 else RiskLevel.MEDIUM
session = VeriproofPydanticAISession(
session_intent=SessionIntent.ADVISORY,
decision_type=decision,
agent_role=AgentRole.PRIMARY,
)
session.set_risk_level(risk.value)
# 4. Run the agent — OTel spans are captured automatically
result = await session.run(
agent,
f"Applicant {applicant_id} has a credit score of {credit_score}. "
"Debt-to-income ratio is 0.32. Assess their loan application.",
)
recommendation = "APPROVE" if credit_score >= 680 else "DECLINE"
return {
"session_id": session.session_id,
"recommendation": recommendation,
"analysis": result.data,
}
if __name__ == "__main__":
result = asyncio.run(assess_loan_application("A-1042", credit_score=712))
print(f"Session ID : {result['session_id']}")
print(f"Decision : {result['recommendation']}")
print(f"Analysis : {result['analysis']}")What you’ll see in VeriProof
| Span | Key attributes |
|---|---|
agent_run pydantic-ai | veriproof.session.intent=advisory, veriproof.session.risk_level=low or medium |
pydantic_ai.agent AnthropicModel | gen_ai.system=pydantic-ai, gen_ai.request.model, gen_ai.usage.input_tokens |
Model spans (including token counts and model metadata) are captured automatically by the instrumentation adapter. Prompt and completion text is not captured by default — set enable_content_capture=True in VeriproofClientOptions to opt in.
Tool-using agents
Pydantic AI tool calls are also captured automatically as child spans:
from pydantic_ai import Agent, RunContext, tool
agent = Agent(
AnthropicModel("claude-3-5-sonnet-20241022"),
system_prompt="You are a financial data assistant.",
)
@agent.tool
async def get_credit_report(ctx: RunContext, applicant_id: str) -> dict:
# This tool call appears as a child span under the agent span
return {"score": 712, "bureau": "Equifax", "inquiries": 2}Each @agent.tool call creates a pydantic_ai.tool <tool_name> span automatically linked to the session.
Next steps
- LangGraph Chatbot example — multi-node graph tracing
- Multi-Turn Workflow example — sequential agent orchestration
- Python SDK Core reference — full
VeriproofSessionAPI
Last updated on