Skip to content

API Reference parrot.storage.backends.sqlite

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

parrot.storage.backends.sqlite

View source

SQLite conversation backend — zero-dependency local storage.

Uses aiosqlite (a transitive dependency of asyncdb[sqlite]) for async SQLite access. Suitable for:

  • Data-analyst laptops without Docker or AWS credentials.
  • CI environments without external services.

Limitations (see wiki/stores/storage-backends.md):

  • Single-writer: SQLite serializes writes. Not suitable for multi-process deployments. For multi-worker local setups, use Postgres via Docker.
  • No built-in background TTL sweeper in v1. Expired rows are filtered on read paths; call sweep_expired() explicitly when desired.
  • File path only (no :memory: URIs) in v1; the factory always provides a real path.

FEAT-116: dynamodb-fallback-redis — Module 4 (SQLite backend).

Alias BackfillBatch

Re-exported from parrot.storage.backends.base.BackfillBatch.

Alias ConversationBackend

Re-exported from parrot.storage.backends.base.ConversationBackend.

Alias ConversationDeletedError

Re-exported from parrot.storage.errors.ConversationDeletedError.

Class ConversationSQLiteBackend

Async SQLite implementation of ConversationBackend.

Stores threads, turns, and artifacts in two local SQLite tables. Payload dicts are JSON-encoded so the schema stays simple.

Turn IDs MUST be zero-padded for lexicographic ordering to match numeric ordering (e.g. "001", "002"). This mirrors the DynamoDB reference implementation.

Parameter Type Default Description
path str Filesystem path to the SQLite database file.
default_ttl_days int 180 TTL for new rows in days (default 180).

Attributes:

Attribute Type Description
is_connected bool Return True when the connection is open.

Method ConversationSQLiteBackend.initialize

Open the database connection and create tables (idempotent).

initialize() -> None

Method ConversationSQLiteBackend.close

Close the database connection.

close() -> None

Method ConversationSQLiteBackend.put_thread

Create or replace a thread metadata row.

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 ConversationSQLiteBackend.update_thread

Update specific attributes on a thread metadata row.

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 ConversationSQLiteBackend.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 ConversationSQLiteBackend.put_turn

Idempotent upsert by turn_id, atomic with the tombstone guard.

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 — the backend is not connected.
  • ConversationDeletedError — the session or this turn is tombstoned.
  • StorageWriteError — the write failed (nothing is swallowed).

Method ConversationSQLiteBackend.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 — disconnected or the read failed.

Method ConversationSQLiteBackend.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 ConversationSQLiteBackend.delete_thread_cascade

Delete all conversation items + artifacts for a session.

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 rows deleted across both tables.

Method ConversationSQLiteBackend.put_artifact

Store an artifact row.

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 ConversationSQLiteBackend.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]

Method ConversationSQLiteBackend.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 ConversationSQLiteBackend.delete_artifact

Delete a single artifact row.

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 ConversationSQLiteBackend.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 ConversationSQLiteBackend.query_turn_window

Newest limit turns by (ts_utc, seq, turn_id), limit applied descending, returned ascending.

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]

Method ConversationSQLiteBackend.max_sequence

Highest stored sequence for the session, 0 when none (includes expired rows: durable maximum).

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 ConversationSQLiteBackend.put_tombstone

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

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 ConversationSQLiteBackend.deleted_turn_ids

Every turn-scoped tombstone of the session in one query.

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]

Method ConversationSQLiteBackend.deleted_session_ids

The session-tombstoned members of session_ids: one query per 500 ids (below SQLite's variable limit).

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]

Method ConversationSQLiteBackend.is_deleted

Whether the session (or session OR this turn) is tombstoned; raises when it cannot be determined.

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

Method ConversationSQLiteBackend.get_index_state

'ready' | 'pending' | 'invalid'.

Derived: a session is ready iff it has no live turn row without ts_utc; the conversation_index_state table only distinguishes invalid from pending.

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 ConversationSQLiteBackend.backfill_batch

Index up to batch_size legacy turns after cursor (resumable, safe next to new writes).

invalid_sessions entries are formatted "<user_id>/<agent_id>/<session_id>".

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

Parameter Type Default Description
cursor Optional[str]
batch_size int

Returns: BackfillBatch

Method ConversationSQLiteBackend.sweep_expired

Delete all rows whose expires_at is in the past.

This helper is NOT called automatically in v1. Invoke it explicitly when you want to reclaim space (e.g., on a schedule or at startup).

sweep_expired() -> int

Returns: int — Number of rows deleted across both tables.

Alias IndexNotReadyError

Re-exported from parrot.storage.errors.IndexNotReadyError.

Alias StorageUnavailableError

Re-exported from parrot.storage.errors.StorageUnavailableError.

Alias StorageWriteError

Re-exported from parrot.storage.errors.StorageWriteError.

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