Skip to main content

Spectron agent memory for Pydantic AI: toolset, auto-recall history processor, and persistence helpers.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spectron_pydantic_ai-0.1.0.tar.gz (120.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spectron_pydantic_ai-0.1.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file spectron_pydantic_ai-0.1.0.tar.gz.

File metadata

  • Download URL: spectron_pydantic_ai-0.1.0.tar.gz
  • Upload date:
  • Size: 120.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for spectron_pydantic_ai-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3db3c8098cc3ceded10770457392c70e3bf6ee8e8bbdda4f0e124add9e27a22c
MD5 0333ffeed7f9a3ce0e4d9a3671ce7893
BLAKE2b-256 3c1c39527baf702928999fa3fd3668693b1786b565fd1f9fa3d1b5555b0c6b00

See more details on using hashes here.

Provenance

The following attestation bundles were made for spectron_pydantic_ai-0.1.0.tar.gz:

Publisher: release.yml on surrealdb/spectron-pydantic-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spectron_pydantic_ai-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for spectron_pydantic_ai-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 99ef36449c7fe37531da0c30956287235ff2fd676c003d0a8623aafd1d4537fb
MD5 c266133203b2af62caabf4ccd7ea3d6c
BLAKE2b-256 817442551f5e1c661ca5e169724e8b689daf026c7b7212727ada9a624344de6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for spectron_pydantic_ai-0.1.0-py3-none-any.whl:

Publisher: release.yml on surrealdb/spectron-pydantic-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page