-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpydanticai_integration.py
More file actions
465 lines (357 loc) · 13.1 KB
/
pydanticai_integration.py
File metadata and controls
465 lines (357 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
"""PydanticAI Integration Examples for Agent-Airlock.
This example demonstrates how to integrate Agent-Airlock with PydanticAI for
type-safe, validated AI agents. Shows:
1. @agent.tool + @Airlock pattern
2. Dependencies with security policies
3. Structured outputs with validation
4. Human-in-the-loop integration
5. LangChain toolsets integration
Requirements:
pip install agent-airlock pydantic-ai
References:
- PydanticAI: https://ai.pydantic.dev/
- Function Tools: https://ai.pydantic.dev/tools/
- Toolsets: https://ai.pydantic.dev/toolsets/
"""
from dataclasses import dataclass
from pydantic import BaseModel, Field
from agent_airlock import (
READ_ONLY_POLICY,
Airlock,
AirlockConfig,
SecurityPolicy,
)
# Check if PydanticAI is available
try:
from pydantic_ai import Agent, RunContext
except ImportError:
print("PydanticAI is required for this example.")
print("Install with: pip install pydantic-ai")
raise SystemExit(1) from None
# =============================================================================
# THE GOLDEN RULE: @Airlock MUST be closest to the function definition
# =============================================================================
#
# For PydanticAI, tools are registered differently:
#
# Option 1: Pre-secure the function, then register
# @Airlock()
# def my_func(): ...
# agent = Agent(tools=[my_func])
#
# Option 2: Use @agent.tool_plain with @Airlock inside
# @agent.tool_plain
# @Airlock()
# def my_func(): ...
#
# Option 3: For @agent.tool (with context), apply @Airlock to the logic
# =============================================================================
# Configuration
config = AirlockConfig(
strict_mode=True,
mask_pii=True,
mask_secrets=True,
max_output_chars=5000,
)
# =============================================================================
# Example 1: Basic secured tools with Agent
# =============================================================================
# Pre-secure the function before passing to Agent
@Airlock(config=config)
def get_stock_price(symbol: str) -> str:
"""Get the current stock price.
Args:
symbol: Stock ticker symbol (e.g., AAPL, GOOGL)
"""
return f"Stock {symbol}: $150.25 (+2.3%)"
@Airlock(config=config)
def get_company_info(symbol: str) -> str:
"""Get company information.
Args:
symbol: Stock ticker symbol
"""
return f"Company {symbol}: Technology sector, Market cap $2.5T"
# Create agent with secured tools
stock_agent = Agent(
"openai:gpt-4o",
system_prompt="You are a financial assistant. Use the tools to answer stock questions.",
tools=[get_stock_price, get_company_info],
)
# =============================================================================
# Example 2: Using @agent.tool_plain with Airlock
# =============================================================================
weather_agent = Agent(
"openai:gpt-4o-mini",
system_prompt="You are a weather assistant.",
)
@weather_agent.tool_plain
@Airlock(config=config)
def get_weather(city: str, units: str = "celsius") -> str:
"""Get current weather for a city.
Args:
city: City name
units: Temperature units (celsius or fahrenheit)
"""
return f"Weather in {city}: 22°{units[0].upper()}, Sunny, Humidity 45%"
@weather_agent.tool_plain
@Airlock(config=config)
def get_forecast(city: str, days: int = 3) -> str:
"""Get weather forecast for a city.
Args:
city: City name
days: Number of days to forecast (1-7)
"""
if days < 1 or days > 7:
return "Error: days must be between 1 and 7"
return f"Forecast for {city}: Sunny for the next {days} days"
# =============================================================================
# Example 3: Dependencies with security context
# =============================================================================
@dataclass
class SecurityContext:
"""Security context with user info and permissions."""
user_id: str
role: str
permissions: list[str]
# Agent that uses dependencies
secure_agent = Agent(
"openai:gpt-4o",
deps_type=SecurityContext,
system_prompt="You are a secure data assistant.",
)
# For tools that need context, secure the inner logic
@secure_agent.tool
async def access_database(
ctx: RunContext[SecurityContext],
table: str,
query: str,
) -> str:
"""Access database with permission checks.
Args:
table: Database table name
query: SQL-like query
"""
# Check permissions from context
if "read" not in ctx.deps.permissions:
return f"Error: User {ctx.deps.user_id} lacks read permission"
# Create an inner secured function
@Airlock(config=config, policy=READ_ONLY_POLICY)
def execute_query(table_name: str, sql: str) -> str: # noqa: ARG001
return f"Query result from {table_name}: 5 rows returned"
return execute_query(table_name=table, sql=query)
@secure_agent.tool
async def update_record(
ctx: RunContext[SecurityContext],
table: str,
record_id: str,
data: str,
) -> str:
"""Update a database record.
Args:
table: Database table name
record_id: Record identifier
data: JSON data to update
"""
if "write" not in ctx.deps.permissions:
return f"Error: User {ctx.deps.user_id} lacks write permission"
if ctx.deps.role not in ["admin", "operator"]:
return f"Error: Role {ctx.deps.role} cannot modify records"
# Secured update function
@Airlock(config=config)
def do_update(tbl: str, rid: str, payload: str) -> str:
return f"Updated {tbl}.{rid} with {payload}"
return do_update(tbl=table, rid=record_id, payload=data)
# =============================================================================
# Example 4: Structured outputs with Pydantic models
# =============================================================================
class OrderStatus(BaseModel):
"""Order status response."""
order_id: str
status: str = Field(pattern=r"^(pending|processing|shipped|delivered)$")
estimated_delivery: str | None = None
tracking_number: str | None = None
class OrderQuery(BaseModel):
"""Order query parameters."""
order_id: str = Field(..., min_length=5, max_length=20)
include_tracking: bool = False
# Agent with structured output
order_agent = Agent(
"openai:gpt-4o",
output_type=OrderStatus,
system_prompt="You look up order statuses.",
)
@order_agent.tool_plain
@Airlock(config=config)
def lookup_order(order_id: str, include_tracking: bool = False) -> str:
"""Look up an order by ID.
Args:
order_id: The order identifier
include_tracking: Whether to include tracking info
"""
result = f"Order {order_id}: Status=shipped"
if include_tracking:
result += ", Tracking=1Z999AA10123456784"
return result
# =============================================================================
# Example 5: Rate-limited and policy-enforced tools
# =============================================================================
API_POLICY = SecurityPolicy(
allowed_tools=["*"],
rate_limits={
"fetch_external_data": "30/minute",
"send_message": "10/minute",
},
)
api_agent = Agent(
"openai:gpt-4o-mini",
system_prompt="You can fetch data and send messages.",
)
@api_agent.tool_plain
@Airlock(config=config, policy=API_POLICY)
def fetch_external_data(url: str, format: str = "json") -> str:
"""Fetch data from an external URL.
Rate limited to 30 calls per minute.
Args:
url: URL to fetch
format: Response format (json, xml, text)
"""
return f"Fetched {format} data from {url}"
@api_agent.tool_plain
@Airlock(config=config, policy=API_POLICY)
def send_message(recipient: str, message: str, priority: str = "normal") -> str: # noqa: ARG001
"""Send a message to a recipient.
Rate limited to 10 per minute.
Args:
recipient: Message recipient (email or user ID)
message: Message content
priority: Message priority (low, normal, high)
"""
return f"Message sent to {recipient} with {priority} priority"
# =============================================================================
# Example 6: Sandboxed code execution
# =============================================================================
code_agent = Agent(
"openai:gpt-4o",
system_prompt="You can execute Python code to solve problems.",
)
@code_agent.tool_plain
@Airlock(config=config, sandbox=True, sandbox_required=True)
def run_python(code: str) -> str:
"""Execute Python code in a secure sandbox.
SECURITY: Runs in isolated E2B Firecracker MicroVM.
Args:
code: Python code to execute
"""
import io
import sys
old_stdout = sys.stdout
sys.stdout = io.StringIO()
try:
exec(code) # noqa: S102 - Safe: sandbox_required=True
return sys.stdout.getvalue() or "Executed successfully"
except Exception as e:
return f"Error: {e}"
finally:
sys.stdout = old_stdout
# =============================================================================
# Example 7: Using PydanticAI Toolsets with Airlock
# =============================================================================
def create_secured_toolset():
"""Create a toolset with secured functions.
Toolsets can be reused across multiple agents.
"""
from pydantic_ai import Toolset
# Define secured functions
@Airlock(config=config)
def calculate(expression: str) -> str:
"""Evaluate a math expression.
Args:
expression: Mathematical expression
"""
try:
result = eval(expression, {"__builtins__": {}}, {}) # noqa: S307
return str(result)
except Exception as e:
return f"Error: {e}"
@Airlock(config=config)
def format_currency(amount: float, currency: str = "USD") -> str:
"""Format a number as currency.
Args:
amount: Numeric amount
currency: Currency code (USD, EUR, GBP)
"""
symbols = {"USD": "$", "EUR": "€", "GBP": "£"}
symbol = symbols.get(currency, currency)
return f"{symbol}{amount:,.2f}"
# Create toolset
return Toolset(tools=[calculate, format_currency])
# =============================================================================
# Demo: Run the examples
# =============================================================================
async def demo_pydanticai():
"""Demonstrate Agent-Airlock with PydanticAI."""
print("\n" + "=" * 60)
print("DEMO: PydanticAI + Agent-Airlock")
print("=" * 60)
# Test 1: Direct function call
print("\n1. Direct secured function call:")
result = get_stock_price(symbol="AAPL")
print(f" Result: {result}")
# Test 2: Type validation
print("\n2. Type validation (wrong type):")
result = get_weather(city=123) # type: ignore
print(f" Result: {result}")
if isinstance(result, dict) and not result.get("success"):
print(f" Fix hints: {result.get('fix_hints', [])}")
# Test 3: Ghost argument rejection
print("\n3. Ghost argument rejection:")
result = get_forecast(city="London", days=5, force=True) # type: ignore
print(f" Result: {result}")
# Test 4: Run with Agent
print("\n4. Running agent with secured tools:")
try:
result = await weather_agent.run("What's the weather in Tokyo?")
print(f" Agent response: {result.data}")
except Exception as e:
print(f" (Agent run requires API key): {e}")
# Test 5: Dependency context
print("\n5. Running with security context:")
try:
ctx = SecurityContext(
user_id="user-123",
role="analyst",
permissions=["read"],
)
result = await secure_agent.run(
"Query the users table for active users",
deps=ctx,
)
print(f" Agent response: {result.data}")
except Exception as e:
print(f" (Agent run requires API key): {e}")
# =============================================================================
# Main entry point
# =============================================================================
if __name__ == "__main__":
import asyncio
print("=" * 60)
print("Agent-Airlock + PydanticAI Integration")
print("=" * 60)
print()
print("Agents Created:")
print(" - stock_agent: Financial data with secured tools")
print(" - weather_agent: Weather with @agent.tool_plain")
print(" - secure_agent: Database access with context")
print(" - order_agent: Structured output with validation")
print(" - api_agent: Rate-limited external calls")
print(" - code_agent: Sandboxed code execution")
print()
print("Key Patterns:")
print(" - Pre-secure functions before passing to Agent")
print(" - Use @agent.tool_plain + @Airlock for simple tools")
print(" - Wrap context-dependent logic with @Airlock")
print()
asyncio.run(demo_pydanticai())
print("\n" + "=" * 60)
print("Examples complete!")
print("=" * 60)