Skip to main content

spectron-pydantic-ai

Spectron agent memory for Pydantic AI.

Spectron is SurrealDB's memory and knowledge layer for AI agents. This package connects it to Pydantic AI through that framework's own extension points, so an agent can remember facts across runs, recall them when they are relevant, and keep a durable record of its conversations.

It gives you three surfaces, which you can use on their own or together:

  • Memory tools (SpectronToolset): expose recall, context, remember, and more as tools the agent calls when it decides to.
  • Auto-recall (spectron_history_processor): inject relevant memory before each model request, with no tool call required.
  • Persistence (store_run, store_messages): write a run's messages back to Spectron so conversations survive across sessions.

Status

Spectron is in early preview. Its Python client ships in the base SurrealDB SDK (surrealdb, v3 alpha or newer), which installs automatically as a dependency of this package. Until you have access to a Spectron instance, you can still install this package, wire it into an agent, and run the test suite: every Spectron call goes through a small client protocol that is easy to fake.

Install

pip install spectron-pydantic-ai

To run against a live Spectron instance and a model provider (the Spectron client is bundled in surrealdb, installed automatically):

pip install "spectron-pydantic-ai" "pydantic-ai-slim[openai]"

Quickstart

import asyncio
from pydantic_ai import Agent
from spectron_pydantic_ai import SpectronMemory, SpectronToolset

async def main():
    memory = SpectronMemory.connect(
        context="your-context",
        endpoint="https://your-spectron-instance",
        api_key="your-api-key",
        on_behalf_of="ada",
    )
    agent = Agent("openai:gpt-4o", toolsets=[SpectronToolset(memory)])
    result = await agent.run("Remember that I prefer window seats.")
    print(result.output)

asyncio.run(main())

Auto-recall

Inject relevant memory before every run without giving the agent a tool. The processor reads the latest user message, recalls related memories, and prepends them as context.

from pydantic_ai import Agent
from pydantic_ai.capabilities import ProcessHistory
from spectron_pydantic_ai import spectron_history_processor

processor = spectron_history_processor(memory)
agent = Agent("openai:gpt-4o", capabilities=[ProcessHistory(processor)])

Use mode="context" to load the current working set instead of searching by the latest message.

Note on versions: Pydantic AI registers history processors through the capabilities argument with ProcessHistory, as shown above. Older releases used a history_processors=[...] argument instead. The processor function returned by spectron_history_processor works with both; only the way you attach it to the agent differs. Check the version installed in your project.

Persistence

Store a run's messages so the next session can recall them:

from spectron_pydantic_ai import store_run

result = await agent.run("I am planning a trip to Tokyo.")
await store_run(memory, result)

Scoping and multi-tenancy

SpectronMemory carries a scope (session_id, scope, on_behalf_of) that is added to every operation — scope is applied as scopes on writes and lens on reads. One connection can serve many users and sessions by creating narrowed views:

base = SpectronMemory(client)
alice = base.scoped(on_behalf_of="alice", session_id="s1")
bob = base.scoped(on_behalf_of="bob", session_id="s2")

API

Name Purpose
SpectronMemory Scoped wrapper over the Spectron client. connect(...), scoped(...), and the memory verbs (remember, remember_many, recall, query_context, reflect, forget, inspect, upload).
SpectronToolset Pydantic AI toolset exposing memory operations as tools.
spectron_history_processor Build a history processor for auto-recall.
store_run, store_messages Persist messages back to Spectron.
SpectronClient Protocol describing the client this package needs.
SpectronError, SpectronImportError Exceptions raised by the package.

The toolset exposes recall, context, and remember by default. Pass tools=ALL_TOOLS (or a subset) to also expose reflect and forget:

from spectron_pydantic_ai import ALL_TOOLS, SpectronToolset

toolset = SpectronToolset(memory, tools=ALL_TOOLS)

Examples

See the examples directory for runnable scripts covering the toolset, auto-recall, and a persistent multi-turn chat.

Development

This project uses uv.

uv sync
uv run ruff check .
uv run ruff format --check .
uv run pyright
uv run pytest

License

Apache License 2.0. See LICENSE.

Metadata

Release files for spectron-pydantic-ai 0.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 spectron-pydantic-ai 0.1.0
File Size Uploaded
spectron_pydantic_ai-0.1.0.tar.gz 120.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spectron-pydantic-ai 0.1.0
File Interpreter ABI Platform
spectron_pydantic_ai-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 139.4 kB

Release files / spectron_pydantic_ai-0.1.0.tar.gz

Download URL spectron_pydantic_ai-0.1.0.tar.gz
Size 120.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3db3c8098cc3ceded10770457392c70e3bf6ee8e8bbdda4f0e124add9e27a22c
BLAKE2b-256 checksum
How to use checksums
3c1c39527baf702928999fa3fd3668693b1786b565fd1f9fa3d1b5555b0c6b00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 16, 2026.

Transparency log

Release files / spectron_pydantic_ai-0.1.0-py3-none-any.whl

Download URL spectron_pydantic_ai-0.1.0-py3-none-any.whl
Size 18.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
99ef36449c7fe37531da0c30956287235ff2fd676c003d0a8623aafd1d4537fb
BLAKE2b-256 checksum
How to use checksums
817442551f5e1c661ca5e169724e8b689daf026c7b7212727ada9a624344de6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 16, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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