Pydantic AI integration
Add long-term agent memory to Pydantic AI agents
Pydantic AI agents using Zep gain long-term memory backed by a temporal knowledge graph. The zep-pydantic-ai package persists both sides of each conversation turn, injects relevant context into the model prompt using Pydantic AI’s native capabilities, and adds a model-callable graph-search tool.
Core benefits
- Native Pydantic AI capabilities:
zep_capabilities(deps)bundles the currentProcessHistoryhistory-processor hook with aHooks(after_run=...)hook — not a deprecated kwarg - Automatic assistant persistence: The bundled
after_runhook persists the assistant’s reply when the run completes, so no manual persistence call is needed - Single round-trip: Persists the user turn and retrieves context in one
add_messagescall - Correct under tool calls: Dedupes per run (keyed by
RunContext.run_id), so a run that makes tool calls records the turn exactly once - Pin-or-expose graph search: A model-callable tool over
graph.search— every search parameter is model-exposed by default, or pinned/hidden per deployment - Out-of-band provisioning:
ensure_user/ensure_threadcreate resources up front, with a per-user setup hook that fires only on real creation - Graceful degradation: A Zep failure on the turn path is logged but never crashes the agent run
How it works
The integration plugs into Pydantic AI through three components:
ZepDeps— a dataclass used as the agent’sdeps_type. It carries the Zep client, the user/thread identity, and optional context-building configuration. Construct one per conversation and pass it toagent.run(..., deps=deps); the history processor, theafter_runhook, and the search tool all reach it throughRunContext.deps.zep_capabilities(deps)— the recommended way to register memory. It returns a capabilities list bundlingProcessHistory(zep_history_processor)— which persists the latest user message viathread.add_messages(return_context=True)before each model request and prepends Zep’s context block as a system message — with aHooks(after_run=...)hook that persists the assistant’s reply once the run completes. Usecreate_zep_after_run_hook(deps)to compose that hook with your ownHooks(...)instance instead.create_zep_search_tool— a factory returning a model-callablepydantic_ai.Toolovergraph.search. The model decides when to search the knowledge graph and, by default, which search parameters to use.
Because ProcessHistory fires once per model request (not once per run), the history processor dedupes per run, keyed by RunContext.run_id: it persists and retrieves on the first model request of a run and replays the cached context on later requests within that same run, so tool-calling runs never create duplicate episodes.
Installation
Requires Python 3.11+, pydantic-ai>=1.107,<2, and a Zep Cloud API key. Get your API key from app.getzep.com.
Set up your environment variables:
Upgrading from zep-pydantic-ai 0.1.x
Two changes can require code updates: create_zep_search_tool returns a pydantic_ai.Tool rather than a bare function — code that invoked the return value directly should call tool.function(ctx, query=..., **kwargs) — and the default injected context wording follows the canonical DEFAULT_CONTEXT_TEMPLATE; pass context_template=... on ZepDeps to keep custom wording. See the package changelog for the full list of changes.
Usage
Register zep_capabilities(deps) and the search tool when building the agent, then pass the same ZepDeps to each run. Both sides of every turn are persisted automatically. In this bundled form, zep_capabilities(deps) closes over one ZepDeps instance, so construct the Agent inside your per-conversation setup rather than sharing it across users.
Explicit control over persistence
To control exactly when the assistant’s reply reaches Zep, register the history processor directly and call persist_run yourself after the run completes:
persist_run sends only assistant text — tool-call and tool-return scaffolding is skipped — so Zep records one clean assistant message per turn. It is not needed when the agent uses zep_capabilities(deps).
On-demand graph search
Beyond the automatic context injection, create_zep_search_tool() returns a model-callable pydantic_ai.Tool over graph.search; pass it directly in tools=[...]. The model decides when to look up specific facts, entities, or prior episodes, and the tool returns a formatted text summary of the matching results. By default it searches the current user’s graph; pass graph_id=... to target a shared standalone graph.
Every search parameter (scope, reranker, limit, mmr_lambda, center_node_uuid) is exposed to the model in the tool’s JSON schema by default, with documented defaults. Two constructor arguments override this per deployment: pinned_params fixes a parameter to a constant value and hides it from the schema, and hidden_params hides a parameter without pinning it, so Zep’s server-side default applies:
The scope, reranker, and limit constructor arguments are back-compat aliases that pin (and hide) those parameters; prefer pinned_params in new code. search_filters and bfs_origin_node_uuids are constructor-only — their complex shapes are not exposed to the model.
Memory vs tools
The integration combines two retrieval paths on the same agent:
Injection grounds each turn with cross-session context; the search tool lets the model actively dig for specific details.
Provisioning
ensure_user and ensure_thread provision the Zep user and thread out-of-band, before the first turn — useful for onboarding flows that want genuine failures (auth, network, 5xx) to raise loudly rather than degrade silently:
Both helpers are create-then-catch-conflict: they treat an “already exists” conflict as success (returning False), return True on genuine creation, and propagate genuine failures. Use the on_created hook (a UserSetupHook) to configure per-user resources — a custom ontology, custom extraction instructions, or user summary instructions — exactly once, on real creation; see customizing graph structure for the available options. If on_created raises, that exception propagates even though the user was created, so make the hook idempotent.
Calling these helpers is optional: the history processor runs the same logic lazily on the turn path, wrapped so that a genuine failure there is logged and degrades to no-memory rather than breaking the run.
Custom context building
Set context_builder on ZepDeps to replace the default context retrieval with custom logic — for example, searching a different graph, applying filters, or combining multiple sources:
ContextInput is a frozen dataclass bundling zep (the AsyncZep client), user_id, thread_id, user_message, and run_context (the Pydantic AI RunContext for the turn). Returning None skips injection for that turn.
When context_builder is set, message persistence (add_messages without return_context) and the builder run concurrently, with per-side failure isolation:
- If the builder raises, a warning is logged and context injection is skipped for that turn — persistence still completes.
- If persistence raises, a warning is logged and the turn is not marked as persisted (so it retries on the next model request) — a successful builder result is still injected.
Context template
context_template on ZepDeps controls how retrieved context is wrapped before injection. It must contain a literal {context} placeholder, rendered via plain string replacement (template.replace("{context}", context), never str.format), so context text containing {, }, or % is always safe to inject:
The default is DEFAULT_CONTEXT_TEMPLATE, an explicit <ZEP_CONTEXT>...</ZEP_CONTEXT> block with canonical wording shared across Zep’s framework integrations.
Configuration options
ZepDeps
create_zep_search_tool
Constructor arguments (returns a pydantic_ai.Tool[ZepDeps]):
Model-exposed search parameters (when not pinned or hidden), with their defaults:
Best practices
- Construct one
ZepDepsper conversation and reuse a singleAsyncZepclient across runs - Pass real names so Zep can anchor the user’s identity node in the graph
- Use
zep_capabilities(deps)so the assistant’s reply is persisted automatically; callpersist_runonly when you registerProcessHistorydirectly and want explicit control - Provision up front in onboarding flows with
ensure_user/ensure_threadso misconfiguration raises before the agent ever runs - Allow time for indexing — Zep extracts knowledge asynchronously, so facts from a turn are not instantly searchable
Next steps
- Explore customizing graph structure for advanced knowledge organization
- Learn about searching the graph and how to tune search
- See code examples for additional patterns