Brain Docs

Reference

Python SDKInstallationHTTP clientWire clientSync vs async
ErrorsRetriesPagination
Brain/Docs
✦ ASKBrain on GitHubDashboard
Python SDKSync vs async

Sync vs async

Three tiers, two of them transports for the same protocol — how AsyncBrainClient differs from BrainClient, and why you pick one per program.

PURPOSE

The Python SDK ships the wire protocol twice: BrainClient demultiplexes on a reader thread, AsyncBrainClient on a reader task. Same verbs, same signatures, same request types. This page covers the differences that actually matter, and the one mistake that mixing them causes.

Three tiers, not two transports

OptionTypeDefault / Env
BrainClientwire · synchronous

Blocking calls over the BRN0 protocol. A background reader thread demultiplexes responses by stream_id, so one client serves requests from several threads at once.

AsyncBrainClientwire · asyncio

The same verbs, each a coroutine. A reader task does the demultiplexing.

BrainHttpClientHTTP · synchronous

A separate thing — the hosted-edge REST client, not a transport choice for the same API. See HTTP client.

Pick one wire tier per program

The synchronous client blocks the event loop when called from inside a coroutine — the calls succeed, and every other task on that loop stalls for the duration. The async one needs a running loop and raises without one. Neither failure is loud, which is why the choice belongs at the top of a program rather than per call site.

What changes

BrainClient
AsyncBrainClient
Connect
BrainClient.connect(host, port, auth)
await AsyncBrainClient.connect(host, port, auth)
Full config
connect_with(host, port, config)
await connect_with(host, port, config)
Verbs
client.encode(request)
await client.encode(request)
Close
client.close()
await client.aclose()
Subscriptions
for event in sub:
async for event in sub:
Pool
Pool
AsyncPool
Retry helper
brain_db_sdk.retry.with_retry
brain_db_sdk.aio.with_retry

The close methods have different names

AsyncBrainClient closes with aclose(), not close() — the asyncio convention, and a deliberate difference so a copy-pasted client.close() fails loudly instead of silently doing nothing.

Everything else is shared: the same ClientConfig, the same Auth, the same request dataclasses, the same builders, the same error hierarchy, the same ConnectionInfo.

Side by side

# Synchronous
from brain_db_sdk import BrainClient, Auth, EncodeBuilder, RecallBuilder

client = BrainClient.connect("127.0.0.1", 9090, Auth.token(key))
try:
    client.encode(EncodeBuilder("Priya moved to Lisbon.").session(42).build())
    answer = client.recall(RecallBuilder("where does Priya work?").limit(5).build())
finally:
    client.close()
# asyncio
from brain_db_sdk import AsyncBrainClient, Auth, EncodeBuilder, RecallBuilder

client = await AsyncBrainClient.connect("127.0.0.1", 9090, Auth.token(key))
try:
    await client.encode(EncodeBuilder("Priya moved to Lisbon.").session(42).build())
    answer = await client.recall(RecallBuilder("where does Priya work?").limit(5).build())
finally:
    await client.aclose()

The builders are not async and never were — they construct a request locally. Only the verbs are coroutines.

Concurrency

Both tiers multiplex: several requests can be in flight on one connection, and you do not manage streams. The difference is how you express the concurrency.

# Synchronous — threads
from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor() as pool:
    answer, entities = pool.map(
        lambda f: f(), [lambda: client.recall(req), lambda: client.list_entities(list_req)]
    )
# asyncio — gather
answer, entities = await asyncio.gather(
    client.recall(req),
    client.list_entities(list_req),
)

Subscriptions

AsyncSubscription is an async iterator; the synchronous Subscription is an ordinary one and also a context manager.

# asyncio
sub = await client.subscribe(request)
async for event in sub:
    last_lsn = event.lsn
    handle(event)
await sub.unsubscribe()

See subscribe for the filter, resumption by LSN, and the difference between unsubscribe() and close().

Pools

AsyncPool mirrors Pool: connect, connect_with, get, get_healthy, size and close, each awaited where the synchronous version blocks. Prefer get_healthy for anything long-lived — see Wire client.

See also
Wire clientRetriessubscribe

Was this page helpful?

Wire client

BrainClient over the BRN0 protocol — connect, ClientConfig, the two auth methods, act_as, multiplexing, the pool, and the id helpers.

encode

Store a memory from text — the write verb, its wait modes, and the deduplication result.

On this page

Three tiers, not two transportsWhat changesSide by sideConcurrencySubscriptionsPools
Suggest an editOpen in Playground