Loading
Loading
The async mirror of Memory. Same kwargs, same method shapes; data-plane methods are awaitable, for_user() stays sync.
Identical to Memory except transport must be an httpx.AsyncBaseTransport. Passing an httpx.AsyncClient raises ConfigValidationError.
AsyncMemory(
*,
api_key: str | None = None,
base_url: str | None = None,
path: str | os.PathLike[str] | None = None,
validate_user_id: Callable[[str], None] | None = None,
timeout: float | httpx.Timeout = 10.0,
max_retries: int = 2,
suppress_async_warning: bool | None = None, # accepted, no-op
transport: httpx.AsyncBaseTransport | None = None,
)from contextlib import asynccontextmanager
from fastapi import FastAPI, Depends
from atlaso import AsyncMemory
memory: AsyncMemory | None = None
@asynccontextmanager
async def lifespan(app: FastAPI):
global memory
memory = AsyncMemory()
yield
await memory.aclose()
app = FastAPI(lifespan=lifespan)
@app.post("/remember")
async def remember(text: str, user_id: str = Depends(current_user)):
user = memory.for_user(user_id) # sync; no I/O
result = await user.add(text)
return {"id": result.id}
@app.get("/recall")
async def recall(q: str, user_id: str = Depends(current_user)):
user = memory.for_user(user_id)
hits = await user.recall(q)
return {
"verdict": hits.explain(),
"is_confident": hits.is_confident,
"has_disagreement": hits.has_disagreement,
"items": [
{
"content": h.content,
"is_confident": h.is_confident,
"has_disagreement": h.has_disagreement,
"agreement_score": h.agreement_score,
}
for h in hits
],
}Every data-plane method on Memory exists on AsyncMemory with the same signature and the same defaults — the only difference is each is awaitable: await user.add(...), await user.recall(...), etc.
for_user(user_id) stays synchronous on AsyncMemory — it returns an AsyncUserHandle with no I/O. Only the method calls on the handle are awaitable. update()is also sync (it's the typo-blocker — see below). Lifecycle uses aclose() / async with.
aclose() instead of close().__aenter__ / __aexit__ instead of sync context manager.suppress_async_warning accepted for parity but is a no-op (calling async code from async code does not trip the warning).AsyncMemory from sync code raises immediately, rather than warning.Was this page helpful?