Skip to content

API Reference parrot.storage.backends.mongodb

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

parrot.storage.backends.mongodb

View source

MongoDB conversation backend using motor (via asyncdb[mongo]).

Suitable for GCP deployments or teams already running MongoDB / DocumentDB. Uses native MongoDB TTL indexes on expires_at.

Note: Mongo's TTL reaper runs once per minute. Tests must NOT assert instant expiry after writing an expired document — the TTL index is for background cleanup. Use delete_thread_cascade or delete_session_artifacts for deterministic cleanup in tests.

FEAT-116: dynamodb-fallback-redis — Module 6 (MongoDB 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 ConversationMongoBackend

Async MongoDB implementation of ConversationBackend.

Uses motor (the async MongoDB driver) accessed via asyncdb[mongo]. Two collections are used:

  • conversations: thread metadata + turns (discriminated by kind).
  • artifacts: artifact items.

replace_one(..., upsert=True) is used for all writes to match DynamoDB's overwrite-or-create semantics.

Parameter Type Default Description
dsn str MongoDB connection string, e.g. "mongodb://user:pw@host:27017/parrot".
database str 'parrot' Database name (default "parrot").
default_ttl_days int 180 TTL for new documents in days (default 180).

Attributes:

Attribute Type Description
is_connected bool

Method ConversationMongoBackend.initialize

Connect to MongoDB and create indexes (idempotent).

initialize() -> None

Method ConversationMongoBackend.close

Close the MongoDB client.

Note: motor's AsyncIOMotorClient.close() is synchronous — no await needed. This is intentional and correct, but looks surprising inside an async def.

close() -> None

Method ConversationMongoBackend.put_thread

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

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 ConversationMongoBackend.query_threads

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

Idempotent upsert by turn_id fenced by a tombstone check before AND after the write.

Guarded two-phase write (valid on standalone mongod too): check the tombstone, upsert, re-check; a tombstone that landed in between deletes the document just written.

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

Method ConversationMongoBackend.query_turns

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]

Method ConversationMongoBackend.delete_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

Method ConversationMongoBackend.delete_thread_cascade

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

Method ConversationMongoBackend.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 ConversationMongoBackend.max_sequence

Highest stored sequence for the session, 0 when none (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 ConversationMongoBackend.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 ConversationMongoBackend.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 ConversationMongoBackend.deleted_session_ids

The session-tombstoned members of session_ids: one $in query per 1000 ids.

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 ConversationMongoBackend.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 ConversationMongoBackend.get_index_state

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

Derived: ready iff the session has no turn document with ts_utc: None; the state collection 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 ConversationMongoBackend.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>"; the cursor is the str(ObjectId) of the last examined document.

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

Parameter Type Default Description
cursor Optional[str]
batch_size int

Returns: BackfillBatch

Method ConversationMongoBackend.put_artifact

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 ConversationMongoBackend.get_artifact

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 ConversationMongoBackend.query_artifacts

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 ConversationMongoBackend.delete_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 ConversationMongoBackend.delete_session_artifacts

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

Method ConversationMongoBackend.sweep_expired

No-op for Mongo: TTL indexes handle expiry automatically.

sweep_expired() -> int

Returns: int

Alias IndexNotReadyError

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

Alias StorageError

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

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