Add persistent memory to PydanticAI agents.
PydanticAI Integration
CortexDB integrates with PydanticAI to provide type-safe, persistent memory for structured AI agents.
Installation
pip install cortexdbai[pydanticai]
Setup
from pydantic_ai import Agent
from cortexdb.integrations.pydanticai import CortexMemoryDep
# Define memory as a dependency
memory = CortexMemoryDep(
api_key="your-cortex-api-key",
tenant_id="my-app",
)
agent = Agent(
"openai:gpt-4o",
deps_type=CortexMemoryDep,
system_prompt="You are a helpful assistant with long-term memory.",
)
@agent.tool
async def recall_memory(ctx, query: str) -> str:
"""Recall relevant information from memory."""
result = await ctx.deps.recall(query)
return result.context
@agent.tool
async def store_memory(ctx, content: str) -> str:
"""Store information in long-term memory."""
result = await ctx.deps.remember(content)
return f"Stored (event_id={result.event_id})."
# Use the agent
result = await agent.run(
"What do you know about our deployment process?",
deps=memory,
)
Configuration
| Parameter | Default | Description |
|---|---|---|
| api_key | $CORTEX_API_KEY | CortexDB API key |
| tenant_id | Required | Tenant identifier |
Under the Hood
The integration wrapper maps to CortexDB's REST API:
# ctx.deps.remember("deployment uses blue-green strategy")
curl -X POST https://api.cortexdb.ai/v1/remember \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"content": "deployment uses blue-green strategy",
"tenant_id": "my-app"
}'
# Returns: { "event_id": "019d6359-d3cc-7671-9e4c-9151011fa016" }
# ctx.deps.recall("deployment process")
curl -X POST https://api.cortexdb.ai/v1/recall \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"query": "deployment process",
"tenant_id": "my-app"
}'
# Returns: { "context": "...", "confidence": 0.95, "latency_ms": 8 }