Brain Docs

Reference

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

Wire client

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

PURPOSE

BrainClient speaks Brain's native binary protocol: a 32-byte BRN0 frame header, a CBOR payload, and an optional trailing little-endian f32 vector section, over TCP. One connection multiplexes many concurrent requests on a background reader thread. This page covers connecting, configuring, and the two things the HTTP tier cannot do.

Connecting

Handshake credentialstable
BrainClient.connect(host: str, port: int, auth: Auth) -> BrainClient
BrainClient.connect_with(host: str, port: int, config: ClientConfig) -> BrainClient

connect takes just a credential and uses the default transport settings; connect_with takes a full ClientConfig. The handshake — HELLO, version negotiation, AUTH — runs inside both.

ClientConfig

ParameterTypeRequired
authAuthrequired

The credential. Mandatory, and the only field without a default: the server resolves the connection's identity from it, so a config cannot be built without one.

client_idstroptional

Identifies the client to the server. Defaults to "brain-db-sdk-python".

supported_versionslist[int]optional

Wire versions this client accepts. Defaults to [1] — the only version Brain speaks today.

capabilitiesHelloCapabilitiesoptional

Defaults to streaming on, zstd compression off, server push off.

connect_timeoutfloat | Noneoptional

Deadline for the connect and handshake, in seconds. Defaults to 10.0.

request_timeoutfloat | Noneoptional

Deadline for an ordinary request. Defaults to 30.0.

derived_write_timeoutfloat | Noneoptional

Deadline for an encode under the DERIVED wait mode only. Defaults to 120.0 — it exists because the 30 s request timeout would fire before the derivation pipeline finished.

Auth

ParameterTypeRequired
Auth.token(token: bytes)Authrequired

A shared bearer token, as bytes — encode it yourself.

Auth.mtls(claim: MtlsClaim)Authoptional

Mutual TLS: the client certificate's fingerprint plus the subject it claims. The server checks the assertion against the presented cert.

import os
from brain_db_sdk import BrainClient, Auth, ClientConfig

client = BrainClient.connect_with(
    "127.0.0.1",
    9090,
    ClientConfig(
        auth=Auth.token(os.environ["BRAIN_API_KEY"].encode()),
        request_timeout=15.0,
    ),
)
try:
    ...
finally:
    client.close()   # sends BYE, then closes

Connection facts

client.connection is a frozen ConnectionInfo carrying what the handshake established, with no round trip: space_id, server_id, chosen_version, connection_id, bound_shard_id, permissions, namespace and server_features. client.namespace, client.space_id and client.is_closed are shorthands.

bound_shard_id matters: the connection is pinned to one shard, and a request for data on another is WrongShard, not a transparent redirect.

Multiplexing

One connection carries many concurrent requests, demultiplexed by stream_id on a background reader thread — so a single client serves requests from several threads at once. You do not manage streams.

Streaming verbs — anything with a *_frames form, plus subscribe — each occupy one stream for their duration. cancel_stream ends one early.

act_as

Every tenant-scoped wire request carries an optional act_as selector — namespace, space, and an optional grant. It is what lets one connection serve many spaces: the credential fixes the namespace, and act_as selects the space per request. The builders expose .act_as(namespace, space); hand-built requests set the field directly.

act_as is a grant, not a claim

A principal without the act-as grant — or one naming a namespace outside its allowlist — raises ActAsDenied, a ServerError subclass you can catch by type. The two optional grant bits, schema upload and admin, are requested per call and must also be held.

This is the mechanism behind per-tenant isolation at scale: one pooled connection, a storage-key prefix per end user, and no credential per user. See Namespaces.

Pool

ParameterTypeRequired
Pool.connect(host, port, size, auth)Poolrequired

Opens size connections, each running its own handshake.

Pool.connect_with(host, port, size, config)Pooloptional

The same, with a full config.

pool.get()BrainClientrequired

The next connection, round-robin. No liveness check.

pool.get_healthy()BrainClientrequired

The next connection, reconnected first if its socket died. Concurrent callers landing on the same dead slot share one reconnect.

pool.size()intoptional
How many members.
pool.close()Noneoptional
BYE and close every member.

Pool is a context manager, so with Pool.connect(...) as pool: closes it for you.

Prefer get_healthy for anything long-lived

A pooled member outlives any single request, so a server restart leaves every borrower holding the same dead socket until the process is bounced. get() will hand it to you.

Id helpers

ParameterTypeRequired
new_id()bytesrequired

A fresh random 16-byte id, for request_id and txn_id on hand-built requests. The builders call it for you.

derive_space_id(namespace, space)bytesoptional

The space id the server derives at ingress: UUIDv5(UUIDv5(ROOT, namespace), space). Folding the namespace in is what makes equal space strings under different namespaces diverge. The seed is frozen and pinned by a golden test on both sides.

Do not let the derived space id leave Brain

Clients only ever send the space string. derive_space_id exists for the rare local case — naming the effective space by its resolved id in a subscription filter. It is Brain's internal identifier, not your control plane's; using it as an external key couples two id spaces that are deliberately separate.

See also
Sync vs asyncTransactionsNamespaces

Was this page helpful?

HTTP client

BrainHttpClient — construction, Bearer auth, flat keyword arguments, and what the HTTP tier cannot do.

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.

On this page

ConnectingClientConfigAuthConnection factsMultiplexingact_asPoolId helpers
Suggest an editOpen in Playground