Brain Docs

Reference

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

Python SDK

The hand-written Python client for Brain — two clients over one verb vocabulary, and every operation's reference page.

PURPOSE

brain-db-sdk is the hand-written Python client for the Brain memory database. It re-implements Brain's wire protocol independently — the specification, not a shared library, is the contract — and is verified byte-for-byte against Brain's conformance corpus. This page is the map: 62 operations, each with its own reference page.

Two clients, one vocabulary

OptionTypeDefault / Env
BrainHttpClientHTTP / JSON · synchronous

Talks JSON to the Brain HTTP edge. Flat keyword arguments rather than input objects — brain.encode(text=..., session=...).

BrainClientWire / BRN0 over TCP · synchronous

The binary protocol, demultiplexing on a reader thread. Takes request dataclasses, usually built with the builders.

AsyncBrainClientWire / BRN0 over TCP · asyncio

The same verbs with the same signatures, each a coroutine, demultiplexing on a reader task. See Sync vs async.

Pick one tier per program

The synchronous client blocks the event loop when called from inside a coroutine, and the async one needs a running loop. BrainHttpClient is a third, separate thing — the hosted-edge REST client, not a transport choice for the same API.

The client is flat. There are no namespaced resource objects and no separate query verb: the write verb is encode and the read verb is recall.

Install

pip install brain-db-sdk

See Installation for runtime and versioning notes.

Quickstart

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"),
)

brain.encode(text="I prefer dark mode and live in Berlin.")

answer = brain.recall(query="where does the user live?")
print(answer.answer_kind, answer.memories[0].text if answer.memories else None)

recall never fabricates: the hit list is empty when Brain has nothing to say.

Every operation

AreaWhat it coversOperations
MemoryThe three verbs, plus the store-level operations and memory edges.encode, recall, forget, encode_vector, memory_list, memory_inspect, graph_fetch, link, unlink
Spaces and sessionsProvision and erase the caller's space, and the sessions inside it.create_space, list_spaces, delete_space, create_session, list_sessions, delete_session
EntitiesThe typed graph's identity layer — resolution, merging, lifecycle.create_entity, get_entity, resolve_entity, list_entities, update_entity, rename_entity, merge_entities, unmerge_entity, tombstone_entity
StatementsTyped claims, their supersession chains, and three ways to retire one.create_statement, get_statement, list_statements, supersede_statement, tombstone_statement, retract_statement, statement_history
RelationsTyped, directed, time-bounded edges between entities, and traversal.create_relation, get_relation, list_relations, traverse_relations, supersede_relation, tombstone_relation
SchemaDeclare the typed graph, and the extractors bound to it.get_schema, validate_schema, upload_schema, replace_schema, drop_schema, list_schemas, list_extractors
ReasoningPlanning, inference, the procedural block, and query introspection.plan, reason, materialize_procedural, query_explain, query_trace
StreamsThe live change feed, and stream cancellation.subscribe, cancel_stream
TransactionsGroup wire writes into one atomic unit.txn_begin, txn_commit, txn_abort
CredentialsRegister and verify the LLM provider key extraction runs on.put_llm_credential, get_llm_credential, test_llm_credential, verify_llm_credential, delete_llm_credential
IntrospectionWho you are, what the server can do, and whether it is alive.whoami, capabilities, ping

Reading the reference

Each operation has one page covering both tiers. Where the two differ — a field name, an extra filter, a frame-level form — the page says so rather than splitting into two near-identical pages.

PageWhat it covers
InstallationInstall, imports, runtime, versioning
HTTP clientConstruction, auth, conventions
Wire clientConnect, config, act_as, multiplexing, pooling
ErrorsBoth taxonomies, the categories and the code families
RetriesWhat retries automatically, and what does not
PaginationCursors and frames
See also
InstallationStart with encodeHow Brain is built

Was this page helpful?

Pagination

Cursor-based enumeration on the HTTP tier, frame-based streaming on the wire, and the terminator each one uses.

Installation

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

On this page

Two clients, one vocabularyInstallQuickstartEvery operationReading the reference
Suggest an editOpen in Playground