Skip to content

API Reference parrot.storage.backends.dynamodb

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

parrot.storage.backends.dynamodb

View source

DynamoDB backend implementing ConversationBackend.

Moved from parrot/storage/dynamodb.py in FEAT-116. Behavior is byte-identical to the original; the only additions are:

  • Subclass of ConversationBackend ABC.
  • New delete_turn() method (extracted from chat.py:572-582).
  • Override of build_overflow_prefix() to preserve existing S3 key layout.

FEAT-116: dynamodb-fallback-redis — Module 3 (ConversationDynamoDB refactor).

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 ConversationDynamoDB

Domain wrapper around DynamoDB for conversation storage.

Uses a single aioboto3 session with two table targets — one for conversations (thread metadata + turns) and one for artifacts.

All low-level DynamoDB operations (serialization, pagination, retries) are handled by aioboto3. This class only adds PK/SK construction, TTL setting, and domain-specific query patterns.

Parameter Type Default Description
conversations_table str DynamoDB table name for conversations.
artifacts_table str DynamoDB table name for artifacts.
dynamo_params dict Dict with region_name, aws_access_key_id, aws_secret_access_key, and optional endpoint_url.

Attributes:

Attribute Type Description
is_connected bool Return True if the DynamoDB resource is available.

Method ConversationDynamoDB.initialize

Open aioboto3 resource connections to both tables (idempotent).

initialize() -> None

Method ConversationDynamoDB.close

Close aioboto3 resource connections.

close() -> None

Method ConversationDynamoDB.build_overflow_prefix

Return the S3 key prefix, byte-identical to the original layout.

Preserves the existing artifacts/USER#u#AGENT#a/THREAD#s/aid layout so existing S3 artifacts remain readable.

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

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

Returns: str

Method ConversationDynamoDB.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 ConversationDynamoDB.update_thread

Update specific attributes on a 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 ConversationDynamoDB.query_threads

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

Fix #5: DynamoDB applies Limit before FilterExpression — a naïve Limit=limit query can return fewer items than requested even when more matching items exist. We paginate until we have enough filtered results or exhaust the index.

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

Idempotent transactional upsert of a turn and its chronological companion.

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 (nothing is written).
  • InvalidTurnPayloadError — timestamp/sequence outside the chrono key range.
  • StorageWriteError — the transaction failed (nothing is swallowed).

Method ConversationDynamoDB.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]

Method ConversationDynamoDB.delete_turn

Delete a single conversation turn.

Fix #1: Use ReturnValues='ALL_OLD' so we can detect whether the item actually existed. DynamoDB's delete_item is silent on missing keys without this flag — it would always return True when connected.

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 item existed and was deleted, False otherwise.

Method ConversationDynamoDB.query_turn_window

Newest limit turns by (timestamp, sequence, 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 ConversationDynamoDB.max_sequence

Highest stored sequence (companion items; session-bounded paginated Query), 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 ConversationDynamoDB.put_tombstone

Durably record a session (turn_id=None) or turn deletion. Idempotent; NO ttl attribute.

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

Every turn-scoped tombstone of the session: ONE paginated, consistent Query (not a GetItem per turn).

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 ConversationDynamoDB.deleted_session_ids

The session-tombstoned members of session_ids: consistent BatchGetItem of TOMB#<session> keys, at most 100 keys per request, UnprocessedKeys retried by _batch_get.

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 ConversationDynamoDB.is_deleted

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

With turn_id both tombstone keys are read in ONE consistent BatchGetItem round trip.

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

'ready' | 'pending' | 'invalid' (marker fast path, otherwise derived).

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

Index up to batch_size scanned items after cursor (resumable; offline, never a request path).

invalid_sessions entries are "<user_id>/<agent_id>/<session_id>"; the cursor is the JSON-encoded LastEvaluatedKey of the Scan page.

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

Parameter Type Default Description
cursor Optional[str]
batch_size int

Returns: BackfillBatch

Method ConversationDynamoDB.delete_thread_cascade

Delete all items for a session from the conversations table.

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 ConversationDynamoDB.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 ConversationDynamoDB.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 ConversationDynamoDB.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 ConversationDynamoDB.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 ConversationDynamoDB.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

Alias IndexNotReadyError

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

Alias InvalidTurnPayloadError

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

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