Skip to main content
Add persistent, per-user memory to a Pydantic AI agent in two lines. The integration leans on Pydantic AI’s dependency-injection model: scope and SDK travel through deps, and the search/store tools are registered onto the agent automatically. A run wrapper reports each turn to Synap and, once Synap has compacted the conversation, gives the model Synap’s compaction in place of the earlier turns.
Requires Python 3.11+.

Overview

This guide shows how to add Synap to a Pydantic AI application to build agents that:
  • Recall user-specific facts, preferences, and past conversations
  • Persist new information surfaced during a conversation
  • Stay type-safe and testable end-to-end, with every dependency a dataclass
  • Report each run to Synap and use Synap’s compaction of the conversation

Setup

Install the package alongside Pydantic AI:
Installing the package also installs maximem-synap (0.5.3 or later) and maximem-synap-integrations-common (0.2.3 or later).
Requires Pydantic AI 0.1 or later. Read the agent’s answer from result.output; result.data was removed in Pydantic AI 0.7, and result.output works on every supported version.
The pip package is maximem-synap-pydantic-ai, but the import drops the maximem- prefix and uses underscores: from synap_pydantic_ai import ....
Configure your API key. Generate one from the Synap Dashboard.
Initialize the SDK once at application startup:
See SDK Initialization for the full lifecycle and configuration options.

Basic integration

Declare an agent with deps_type=SynapDeps, call register_synap_tools(agent) once, then pass SynapDeps per request:
register_synap_tools does four things:
  1. Registers search_memory, a tool the agent can call to retrieve memories
  2. Registers store_memory, a tool the agent can call to persist new memories
  3. Registers a system prompt, re-evaluated on every run, that puts what Synap returns for the user in front of the model under Relevant user context:. It is a fetch with no question, so it is a best-effort sample and not an answer to a specific question. Use search_memory for specific recall. It carries memories only, not the conversation. If Synap fails it adds nothing.
  4. Installs the history processor that swaps in Synap’s compaction (see below)
It does not tell the agent to call the tools: put that in your own system prompt. The agent never sees user_id or customer_id directly. It pulls them from RunContext[SynapDeps] inside the tool implementations, so the model cannot spoof scope.

Core concepts

SynapDeps

SynapDeps is the dependency container Pydantic AI injects into every tool call. It carries the SDK and the scoping triple:
You construct a fresh SynapDeps per request, which means the same agent instance can serve any number of users without bleeding scope between them.

register_synap_tools

register_synap_tools(agent) attaches the two memory tools to your agent. After registration, every run that passes SynapDeps exposes:
  • search_memory(query: str): returns the formatted memory context for the question, scoped to deps.user_id (and deps.customer_id if set)
  • store_memory(content: str): persists a new memory under the same scope
Tool descriptions are written so the model calls search_memory for recall questions and store_memory when the user shares a new fact. The model supplies only query or content; the scope comes from deps.

synap_run

register_synap_tools gives the agent memory tools. It does not tell Synap what the conversation is. synap_run does: use it in place of agent.run and it reports the whole turn.
Everything else you pass (message_history, output_type, model_settings and the rest) goes straight to agent.run, and you get back what agent.run returned. From synchronous code, use synap_run_sync with the same arguments. You do not call listen() yourself. The first report opens the gRPC stream. The stream gets up to 10 seconds to open. If it cannot open, the user and assistant turns are written over REST instead, never both, and the stream is tried once more a few seconds later, then no more than once a minute. Reasoning, tool calls and tool results exist only on the stream, so they are dropped while it is down. The REST write and the compaction lookup both need conversation_id to be a UUID. A stream you opened yourself is used as is. To keep synap_run on REST, set SYNAP_AUTO_LISTEN=false. See gRPC vs REST. A failed report is logged, never raised into your run. The tool, tool-result and reasoning events come from a SynapEventStreamHandler, which synap_run installs as the run’s event_stream_handler. Pass your own event_stream_handler= and it is left alone. A tool result is usually your own customer’s data. Synap uses it only as an anticipation hint and never stores it as a long-term memory, but it does leave your process, and the integration has no switch to turn it off. If that matters, pass your own handler to synap_run. On a Pydantic AI version whose Agent.run takes no event_stream_handler, synap_run logs one warning and reports only the two turns.

Synap’s compaction replaces earlier turns

Pydantic AI sends the model the message_history you pass plus the new prompt. Its one seam over that list is a history processor, which runs before a model request and returns the messages the model will see. register_synap_tools, register_synap_st_system_prompt and synap_run each install one on the agent, so it is on by default. Once Synap has compacted the conversation, the processor replaces every turn before the current one with Synap’s compaction, wrapped in <synap_conversation_history> tags, followed by the turns recorded after it. The current turn is untouched. It needs deps.conversation_id; with none, with no compaction yet, or on any error, the messages are left as they came. Pydantic AI’s own processors stay installed: they see a short history and do not fire, and they are still there when Synap has nothing. Three things follow from how Pydantic AI treats the result:
  • The processor changes the messages on the first model request of a run only. A run that calls tools makes several requests, and the history cannot change between them.
  • Turns reach Synap only if something records them. register_synap_tools installs the processor but does not record turns, so use synap_run, or record the turns yourself.
  • result.all_messages() carries the compaction block. If you feed it back as message_history, the next run replaces it again rather than stacking it.
On Pydantic AI 1.x the processor goes on the agent’s history_processors list; on 2.x it goes on as a ProcessHistory capability. The register functions and synap_run handle both. If an agent has neither, a warning is logged once and Synap’s compaction is not applied. To install the processor on an agent yourself, call install_synap_history(agent), or pass synap_history_processor() where your version takes history processors:
On 1.x, pass history_processors=[synap_history_processor()] instead.

register_synap_st_system_prompt

register_synap_st_system_prompt(agent, system="...") registers a system prompt that adds Synap’s short-term context for deps.conversation_id ahead of your own text, wrapped in <synap_short_term_context> tags. It adds nothing when the deps carry no conversation_id, or when the run is given a message_history: the framework already sends those turns, and the history processor swaps in Synap’s compaction. If Synap has none or fails (the default on_error="fallback"), only your system text is used; on_error="raise" raises SynapIntegrationError. It also installs the history processor.

Complete example: per-user assistant

The following pattern is what most production deployments end up with: a single Agent defined at module load, fresh SynapDeps per inbound request, and a thin handler around it.
Three things to notice in this pattern:
  1. The agent is defined once. Pydantic AI’s dependency injection makes it safe to share: each run() gets its own deps.
  2. Scope is request-local. Different users get different SynapDeps; the model only ever sees a scope-stripped tool surface.
  3. The system prompt is the policy. Want recall to be optional? Loosen the prompt. Want stores to be aggressive? Tighten it. The tools obey whatever the prompt tells the model.

Advanced patterns

Multi-tenant scoping

SynapDeps carries the same scoping triple every Synap integration uses: user_id (required), customer_id (B2B instances only), optional conversation_id. customer_id is required on B2B Synap instances and not accepted on B2C (single-tenant) ones, where a call carrying it is rejected with HTTP 400. See Memory Scopes.

Testing with mock deps

Because SynapDeps is a plain dataclass, you can substitute a fake SDK in tests:

Failure semantics

The integration follows the Synap-wide contract:
  • search_memory surfaces failures: logs an error and raises SynapIntegrationError, so the agent never reads a failed search as “nothing remembered”.
  • store_memory surfaces failures: raises SynapIntegrationError so the caller knows persistence failed.
  • The memory-context system prompt degrades: if Synap is unreachable it adds nothing and logs an error.
  • Reporting and history never break a run: a failed report or compaction lookup is logged and dropped, and the run carries on with Pydantic AI’s own messages.
This is by design: a failed tool call is visible to the agent and to you, while the background enrichment never stops an answer.

Going further


Next steps

OpenAI Agents

Function tools for the OpenAI Agents SDK.

CrewAI

Storage backend for CrewAI crews.

Context Fetch

The retrieval API that powers search_memory: modes, scopes, and response shapes.

Memory Scopes

How user_id, customer_id, and conversation_id interact across reads.