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 cortexdb[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."""
results = await ctx.deps.recall(query, top_k=5)
return "\n".join([m.content for m in results.memories])
@agent.tool
async def store_memory(ctx, content: str, episode_type: str = "observation") -> str:
"""Store information in long-term memory."""
await ctx.deps.remember(content, episode_type=episode_type)
return "Stored in memory."
# 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 |
| namespace | None | Memory namespace |
| top_k | 10 | Results per recall |