Skip to content

API Reference parrot.storage.backends.base

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.storage.backends.base

View source

Abstract ConversationBackend interface for pluggable storage.

All storage backends — DynamoDB, SQLite, Postgres, MongoDB — implement this ABC. The shared contract test suite in tests/storage/test_backend_contract.py validates that all backends exhibit identical observable behavior.

FEAT-116: dynamodb-fallback-redis — Module 1 (ConversationBackend ABC). See wiki/stores/storage-backends.md for the backend selection matrix.

Class BackfillBatch

Result of one :meth:ConversationBackend.backfill_batch call.

Parameter Type Default Description
processed int
next_cursor Optional[str]
invalid_sessions Tuple[str, ...]

Fields:

Field Type Default Description
processed int
next_cursor Optional[str]
invalid_sessions Tuple[str, ...]

Class ConversationBackend

Abstract storage backend for conversations, threads, turns, and artifacts.

All implementations MUST preserve the semantics of the DynamoDB reference implementation (see backends/dynamodb.py). Verified by the shared contract test suite in tests/storage/test_backend_contract.py.

The ABC operates on plain dict payloads; Pydantic model serialization/deserialization is the responsibility of ChatStorage and ArtifactStore.

Attributes:

Attribute Type Description
is_connected bool Return True when the backend is ready to accept requests.

Method ConversationBackend.initialize

Open connections and create schema/indexes if needed (idempotent).

initialize() -> None

Method ConversationBackend.close

Release all backend connections.

close() -> None

Method ConversationBackend.put_thread

Create or replace a thread metadata item.

put_thread(user_id: str, agent_id: str, session_id: str, metadata: dict) -> None

Parameter Type Default Description
user_id str
agent_id str
session_id str
metadata dict

Method ConversationBackend.update_thread

Update specific attributes on an existing thread metadata item.

update_thread(user_id: str, agent_id: str, session_id: str, **updates) -> None

Parameter Type Default Description
user_id str
agent_id str
session_id str
**updates {}

Method ConversationBackend.query_threads

List thread metadata items for a user+agent pair, newest first.

query_threads(user_id: str, agent_id: str, limit: int = 50) -> List[dict]

Parameter Type Default Description
user_id str
agent_id str
limit int 50

Returns: List[dict]

Method ConversationBackend.put_turn

Idempotent upsert by turn_id, atomic with the deletion guard and the chronological entry.

Ordering fields (timestamp, sequence) of an existing turn are never changed. Returns the committed payload with those canonical ordering fields, so downstream caches do not insert updates using the incoming (possibly changed) ordering.

put_turn(user_id: str, agent_id: str, session_id: str, turn_id: str, data: dict) -> dict

Parameter Type Default Description
user_id str
agent_id str
session_id str
turn_id str
data dict

Returns: dict

Raises:

  • StorageUnavailableError — When the backend is disconnected.
  • StorageWriteError — When the durable write fails.
  • ConversationDeletedError — When the session or the turn is tombstoned.

Method ConversationBackend.query_turns

Query conversation turns for a session.

query_turns(user_id: str, agent_id: str, session_id: str, limit: int = 10, newest_first: bool = True) -> List[dict]

Parameter Type Default Description
user_id str
agent_id str
session_id str
limit int 10
newest_first bool True

Returns: List[dict]

Raises:

  • StorageUnavailableError — When disconnected or the read fails — never returns [] for a failure.

Method ConversationBackend.delete_turn

Delete a single conversation turn.

delete_turn(user_id: str, agent_id: str, session_id: str, turn_id: str) -> bool

Parameter Type Default Description
user_id str
agent_id str
session_id str
turn_id str

Returns: bool — True if the turn existed and was deleted, False otherwise.

Method ConversationBackend.delete_thread_cascade

Delete all items for a session (thread metadata + turns + artifacts).

delete_thread_cascade(user_id: str, agent_id: str, session_id: str) -> int

Parameter Type Default Description
user_id str
agent_id str
session_id str

Returns: int — Number of items deleted.

Method ConversationBackend.query_turn_window

The newest limit turns by (timestamp, sequence, turn_id).

limit is applied DESCENDING at the backend; the window is returned ASCENDING.

An active session with no turns returns [].

query_turn_window(user_id: str, agent_id: str, session_id: str, limit: int) -> List[dict]

Parameter Type Default Description
user_id str
agent_id str
session_id str
limit int

Returns: List[dict]

Raises:

  • StorageUnavailableError — Disconnected or the read failed.
  • ConversationDeletedError — The session is tombstoned.
  • IndexNotReadyError — The chronological index is not ready (no full scan is performed).

Method ConversationBackend.max_sequence

Highest stored sequence for the session, 0 when none.

max_sequence(user_id: str, agent_id: str, session_id: str) -> int

Parameter Type Default Description
user_id str
agent_id str
session_id str

Returns: int

Method ConversationBackend.put_tombstone

Durably record a session (turn_id=None) or turn deletion. Idempotent.

Not subject to transcript TTL or :meth:delete_thread_cascade.

put_tombstone(user_id: str, agent_id: str, session_id: str, turn_id: Optional[str] = None) -> None

Parameter Type Default Description
user_id str
agent_id str
session_id str
turn_id Optional[str] None

Method ConversationBackend.deleted_turn_ids

The ids of every TURN tombstoned in the session, in ONE bounded read (session tombstone excluded).

deleted_turn_ids(user_id: str, agent_id: str, session_id: str) -> Set[str]

Parameter Type Default Description
user_id str
agent_id str
session_id str

Returns: Set[str]

Raises:

  • StorageUnavailableError — If it cannot be determined — never returns an empty set on failure.

Method ConversationBackend.deleted_session_ids

The members of session_ids that carry a SESSION tombstone, in one bounded read.

Turn tombstones are ignored. An empty session_ids returns set() without touching the store; duplicates in session_ids are harmless and only ids taken from it are returned.

deleted_session_ids(user_id: str, agent_id: str, session_ids: Sequence[str]) -> Set[str]

Parameter Type Default Description
user_id str
agent_id str
session_ids Sequence[str]

Returns: Set[str]

Raises:

  • StorageUnavailableError — If it cannot be determined — never returns an empty set on failure (also when the backend is disconnected).

Method ConversationBackend.is_deleted

Whether the identity is tombstoned.

Without turn_id: whether the SESSION is tombstoned. With turn_id: whether the session OR that turn is tombstoned — a session tombstone deletes every turn of the session, so one call answers "may this turn exist?".

is_deleted(user_id: str, agent_id: str, session_id: str, turn_id: Optional[str] = None) -> bool

Parameter Type Default Description
user_id str
agent_id str
session_id str
turn_id Optional[str] None

Returns: bool

Raises:

  • StorageUnavailableError — If it cannot be determined — never returns False on failure.

Method ConversationBackend.get_index_state

'ready' | 'pending' | 'invalid'. Sessions created after FEAT-886 are 'ready'.

get_index_state(user_id: str, agent_id: str, session_id: str) -> str

Parameter Type Default Description
user_id str
agent_id str
session_id str

Returns: str

Method ConversationBackend.backfill_batch

Index up to batch_size legacy turns after cursor; resumable and safe alongside new writes.

Marks a session 'ready' once all its turns are indexed, 'invalid' when a turn has a missing or unparseable timestamp.

backfill_batch(cursor: Optional[str], batch_size: int) -> BackfillBatch

Parameter Type Default Description
cursor Optional[str]
batch_size int

Returns: BackfillBatch

Method ConversationBackend.put_artifact

Store an artifact item.

put_artifact(user_id: str, agent_id: str, session_id: str, artifact_id: str, data: dict) -> None

Parameter Type Default Description
user_id str
agent_id str
session_id str
artifact_id str
data dict

Method ConversationBackend.get_artifact

Get a single artifact by its key.

get_artifact(user_id: str, agent_id: str, session_id: str, artifact_id: str) -> Optional[dict]

Parameter Type Default Description
user_id str
agent_id str
session_id str
artifact_id str

Returns: Optional[dict] — Artifact dict or None if not found.

Method ConversationBackend.query_artifacts

List all artifacts for a session.

query_artifacts(user_id: str, agent_id: str, session_id: str) -> List[dict]

Parameter Type Default Description
user_id str
agent_id str
session_id str

Returns: List[dict]

Method ConversationBackend.delete_artifact

Delete a single artifact.

delete_artifact(user_id: str, agent_id: str, session_id: str, artifact_id: str) -> None

Parameter Type Default Description
user_id str
agent_id str
session_id str
artifact_id str

Method ConversationBackend.delete_session_artifacts

Delete all artifacts for a session.

delete_session_artifacts(user_id: str, agent_id: str, session_id: str) -> int

Parameter Type Default Description
user_id str
agent_id str
session_id str

Returns: int — Number of artifacts deleted.

Method ConversationBackend.build_overflow_prefix

Return a stable key prefix for overflow storage.

Default implementation yields a DynamoDB-compatible shape so existing S3 layouts do not change. Backends MAY override if they want a different overflow layout (e.g., filesystem-friendly paths).

build_overflow_prefix(user_id: str, agent_id: str, session_id: str, artifact_id: str) -> str

Parameter Type Default Description
user_id str User identifier.
agent_id str Agent/bot identifier.
session_id str Conversation session identifier.
artifact_id str Artifact identifier.

Returns: str — Key prefix string, e.g. "artifacts/USER#u#AGENT#a/THREAD#s/aid".

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally