Skip to main content

pydantic-ai-memorysync

Long-term memory for Pydantic AI, backed by MemorySync — one capability in the Agent(...) constructor gives every run recalled context and automatic capture of what the user tells your agent.

  • MemorySyncCapability — injects what MemorySync knows about the user before every run and, after each successful run, sends the user's prompt to fact extraction, through the framework's own capability channels.
  • Five agent tools — add, search, list, update, delete; they never raise.
  • create_memory_search_tool — memory search returning typed list[MemoryResult] Pydantic models.
  • Async helpers — get_memory_context, search_memories, save_turn.
pip install pydantic-ai-memorysync

Set MEMORYSYNC_API_KEY in the environment (create a key at app.memorysync.io), or pass api_key explicitly. Python 3.10+, pydantic-ai 2.x.

The capability

from pydantic_ai import Agent
from pydantic_ai_memorysync import MemorySyncCapability

agent = Agent(
    "openai:gpt-5",
    instructions="You are a helpful assistant.",
    capabilities=[MemorySyncCapability(user_id="customer-7")],
)

result = await agent.run("What should I cook tonight?")
# The model saw "Relevant memories about this user..." — and the durable
# facts in this prompt are remembered for every future run.

Recall rides the instructions channel. The capability contributes a dynamic instruction (the framework's get_instructions channel), so recalled memory lands in ModelRequest.instructions — visible to the model, but never a conversation part. Replaying result.all_messages() into the next run can never stack stale memory blocks into the transcript, the classic failure mode of message-mutation integrations. One recall per run, replayed identically across however many model requests a tool-calling run makes.

Capture is success-only, by construction. The user's prompt is sent in after_run, which Pydantic AI fires only when the run succeeds — a failed run leaves nothing behind (no remembered question with no answer), with zero custom guard code to get wrong. MemorySync extracts the durable facts the prompt contains and stores only those; filler ("ok", "thanks") yields none, and the prompt text itself is not stored. The model's reply is not sent — assistant replies are not stored as memories — and neither are tool calls, retry prompts or thinking parts. Streaming runs send the prompt once the stream completes.

Identity comes from your deps. In order: user_id_resolver(ctx) when provided, the static user_id, then a user_id attribute on ctx.deps:

from dataclasses import dataclass

@dataclass
class MyDeps:
    user_id: str

agent = Agent(
    "openai:gpt-5",
    deps_type=MyDeps,
    capabilities=[MemorySyncCapability()],  # reads ctx.deps.user_id per run
)
await agent.run("hi", deps=MyDeps(user_id="customer-7"))

Without an identity the run proceeds memoryless and the miss is reported through on_error — guessing a shared namespace would mix users' memories, the one unforgivable failure for a memory layer.

Failure discipline: recall failures degrade to the agent's base instructions; a failed send of the user's prompt leaves the run result untouched. Both report through on_error (default: a logging warning); neither ever breaks a run the model completed. persist=False gives read-only memory.

Conversations group themselves. Each prompt is sent with the run's conversation_id as its session_id (kept on the facts extracted from it), which Pydantic AI carries across message_history continuations — one conversation stays one conversation across processes and deploys. Pass session_id="..." to pin the grouping yourself. The same prompt sent again for the same conversation is recognised server-side and not extracted twice.

Agent tools

from pydantic_ai import Agent
from pydantic_ai_memorysync import create_memorysync_tools

agent = Agent(
    "openai:gpt-5",
    instructions="Use the memory tools to remember durable facts.",
    tools=create_memorysync_tools(user_id="customer-7"),
)

# Untrusted agents: search + list only.
create_memorysync_tools(user_id="customer-7", read_only=True)

add_memory, search_memory, list_memories, update_memory, delete_memory — the same five operations, same response strings as the MemorySync LangChain, AI SDK, CrewAI, Mastra, OpenAI Agents, LlamaIndex and Google ADK tool sets. Plain async callables; failures return short readable strings, never exceptions — in Pydantic AI a tool exception fails the whole run, and a memory lookup is never worth that.

Typed search results

from pydantic_ai_memorysync import create_memory_search_tool, MemoryResult

agent = Agent(
    "openai:gpt-5",
    tools=[create_memory_search_tool(user_id="customer-7")],
)
# The tool returns list[MemoryResult]: validated id / text / score fields.

Helpers

from pydantic_ai_memorysync import get_memory_context, save_turn, search_memories

context = await get_memory_context("what should I cook?", user_id="customer-7")
hits = await search_memories("dietary preferences", user_id="customer-7")
await save_turn(user_id="customer-7", user="I'm vegetarian", assistant="Noted!")

save_turn sends the user message to fact extraction; assistant is accepted so an exchange can be passed as-is, but it is not sent — assistant replies are not stored as memories. All surfaces share the same idempotency seeds, so the same message sent for the same session by any of them is recognised server-side and not extracted twice. save_turn raises on failure — an explicit persist call is owed the truth.

Version support

Package Requires Runtime
pydantic-ai-memorysync 1.1.0 pydantic-ai (or -slim) >=2,<3 Python 3.10+

CI drives REAL agents — instruction-channel injection, replay safety, success-only capture, streaming, typed tool results — against the latest pydantic-ai 2.x release on every push.

Documentation

Metadata

Release files for pydantic-ai-memorysync 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-ai-memorysync 1.1.0
File Size Uploaded Provenance
pydantic_ai_memorysync-1.1.0.tar.gz 15.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-ai-memorysync 1.1.0
File Interpreter ABI Platform
pydantic_ai_memorysync-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 34.3 kB

Release files / pydantic_ai_memorysync-1.1.0.tar.gz

Download URL pydantic_ai_memorysync-1.1.0.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0daddfcbf3030fc33233bdddf6a110ee40dfff32c31ef3dd03cae948bb1e8b03
BLAKE2b-256 checksum
How to use checksums
cf8dc772075ecba4fb5bcece3b9042c66bcb427f29e3cdd3e7f0d3a330196050
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.5

Release files / pydantic_ai_memorysync-1.1.0-py3-none-any.whl

Download URL pydantic_ai_memorysync-1.1.0-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e0fb3258a982b5aed8594b18b6e112eb3dcb741535d82c0a04ca5d6b689645f1
BLAKE2b-256 checksum
How to use checksums
a93c2a6617878ba043f82aaa6ab322c042589b163f2970e370997c6b1b760c7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.5

Security for pydantic-ai-memorysync 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page