MemorySync
SDKs · Python

Python Types & Errors

Use public dataclasses, typed dictionaries, and six exception classes to make success and failure paths explicit. Keep retry behavior narrow and operation-aware.

Public result families

FamilyExamplesHow to consume it
Memory dataclassesMemory, QueryResponse, BulkAddResponse, ComposeResponse, RelationUse attributes with snake_case names.
Add unionMemory | AddSkippedResponseUse isinstance before reading created-memory fields.
Control-plane dictionariesLoginResponse, Project, ExportJob, Webhook, WebhookDeliveryUse documented snake_case keys and tolerate optional fields.
Delete resultlist[int]Compare confirmed IDs with requested IDs.

Error classes

ClassTypical conditionDefault action
AuthError401 or 403Fix authentication, permission, or scope.
ValidationError400, 409, 422, or client validationCorrect the request; do not retry unchanged.
NotFoundError404Show an unavailable state without revealing other scopes.
RateLimitError429Use retry_after_seconds and bound attempts.
ServerError5xxRetry safe reads with bounded backoff; reconcile writes.
MemorySyncErrorNetwork, timeout, or other SDK failureTreat mutation outcomes as uncertain.

Catch specific errors first

handle_errors.py
from memorysync import (
AuthError,
MemorySyncError,
NotFoundError,
RateLimitError,
ServerError,
ValidationError,
)
try:
memory = client.get(101)
except NotFoundError:
memory = None
except (AuthError, ValidationError):
raise
except RateLimitError as error:
print("Retry after", error.retry_after_seconds)
raise
except ServerError:
raise
except MemorySyncError:
raise

Retry reads narrowly

retry_read.py
import random
import time
from memorysync import RateLimitError, ServerError
def query_with_retry(client, question: str, attempts: int = 3):
for attempt in range(attempts):
try:
return client.query(question, k=5)
except RateLimitError as error:
if attempt == attempts - 1:
raise
time.sleep(error.retry_after_seconds or 1.0)
except ServerError:
if attempt == attempts - 1:
raise
time.sleep((2 ** attempt) * 0.25 + random.random() * 0.1)
raise RuntimeError("unreachable")

Use error context defensively

AttributeTypeAvailability
status_codeint | NonePresent when an HTTP response supplied a status.
responseAnyParsed response body when one was available.
request_idstr | NonePresent only when the server returned a request identifier.
retry_after_secondsfloatAvailable on RateLimitError.

Log a safe support record

logging.py
import logging
from memorysync import MemorySyncError
log = logging.getLogger(__name__)
try:
response = client.query("What preference applies?", k=5)
except MemorySyncError as error:
log.warning(
"memory query failed",
extra={
"status_code": error.status_code,
"request_id": error.request_id,
},
)
raise

Related contracts

Was this page helpful?