Brain Docs

Reference

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

HTTP client

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

PURPOSE

BrainHttpClient talks JSON to the Brain HTTP edge — brain-edge self-hosted, or the Arc cloud gateway. It is built on urllib.request with no third-party dependencies. This page documents construction and the conventions that apply to every verb on it.

Construction

Bearer API keystable
BrainHttpClient(
    api_key: str,
    base_url: str = "http://127.0.0.1:8080",
    timeout: float = 30.0,
    retry: HttpRetryPolicy | None = None,
)
ParameterTypeRequired
api_keystrrequired

Sent as Authorization: Bearer <api_key>. Empty raises BrainHttpError with status == 0 and code == "config" before any request is made, and so does a key carrying an ASCII control character — see below.

base_urlstroptional

The edge. Defaults to the self-host address. A scheme that is not http or https is rejected at construction, not at call time.

timeoutfloatoptional

Per-request timeout in seconds — not milliseconds. Defaults to 30.0. A timeout surfaces as a BrainHttpError with status == 0.

retryHttpRetryPolicy | Noneoptional

Defaults to HttpRetryPolicy() — 3 attempts, 0.1 s base, 2.0 s cap. See Retries.

import os
from brain_db_sdk import BrainHttpClient

brain = BrainHttpClient(
    api_key=os.environ["BRAIN_API_KEY"],
    base_url=os.environ.get("BRAIN_BASE_URL", "http://127.0.0.1:8080"),
    timeout=15.0,
)

There is no close()

The HTTP client holds no connection. Construct one per process and keep it; there is nothing to release.

What the constructor rejects

Three checks, all raising BrainHttpError(0, "config", ...) before any request is attempted. The same three rules, with the same messages, apply in the TypeScript client's constructor (which spells the field apiKey) and in Rust's BrainHttpClient::try_new — so a key is accepted or refused the same way in every SDK. Rust's plain new is the one exception: it returns Self and so cannot report a problem at all.

CheckMessage
api_key is non-emptyapi_key is required
api_key carries no ASCII control characterapi_key must not contain control characters
base_url has an http or https schemebase_url must be http:// or https://, got …

The control-character rule covers every C0 character and DEL, except HTAB — RFC 7230 permits a tab in a field value and all three transports send it. The range stops below 0x80, so a key carrying a non-ASCII letter still works. The ordinary way one gets in is a key read from an env file with a trailing newline; left unchecked, urllib sent a NUL straight into the Authorization header, where a proxy may truncate the value. The message names no part of the key, because it is a credential.

The scheme check is a real SSRF guard here, not just a typo check: urlopen honours every scheme urllib supports, so a file:// base URL would turn an SDK call into a local file read.

Conventions

Flat keyword arguments

Unlike the TypeScript and Rust clients, which take input objects, the Python HTTP client takes keyword arguments and builds the body itself:

brain.encode(text="...", session=42, occurred_at=1_741_000_000_000_000_000)
brain.recall(query="...", max_results=5, scope="space")

Arguments left at None are dropped from the body rather than sent as JSON null, so an omitted option means "let the edge decide" instead of "explicitly nothing".

Naming

You write Python's snake_case, which is what the edge speaks, so there is no conversion layer to misread. Where a keyword differs from the wire field name — occurred_at for occurred_at_unix_nanos — the operation's page says so.

64-bit numbers

Python's int is arbitrary precision, so session ids, LSNs and nanosecond timestamps are plain int with nothing to lose. The TypeScript SDK needs bigint for the same values; Python does not.

What the HTTP tier cannot do

OptionTypeDefault / Env
Transactionswire only

No txn_begin. Each HTTP verb is its own unit. See txnBegin.

act_aswire only

The space comes from the credential. Addressing another space per request needs the wire client — see act_as.

Frame-level streamingwire only

No *_frames variants. The HTTP tier concatenates; only subscribe streams, over SSE.

ping and cancel_streamwire only

There is no connection to keep alive and no stream id to cancel.

See also
Wire clientErrorsRetries

Was this page helpful?

Installation

Install brain-db-sdk, its zero dependencies, the Python floor, and how to import each of the three tiers.

Wire client

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

On this page

ConstructionWhat the constructor rejectsConventionsFlat keyword argumentsNaming64-bit numbersWhat the HTTP tier cannot do
Suggest an editOpen in Playground