Skip to main content

SDK for building Holokai neurons in Python — connect to a BigBrain gateway over HTTP+SSE and execute capability-typed tasks.

Project description

holokai-neuron-sdk

SDK for building Holokai neurons in Python — connect to a BigBrain gateway over HTTP+SSE and execute capability-typed tasks.

This is the Python sibling of @holokai/neuron-sdk (TypeScript) and speaks the same wire protocol byte-for-byte (packages/neuron-protocol).

Install

pip install holokai-neuron-sdk

Requires Python 3.10+.

Quick start

import asyncio
import logging

from holokai_neuron_sdk import Capability, HandlerContext, Neuron


async def echo(input, ctx: HandlerContext):
    await ctx.progress(percent=50, message="halfway")
    return {"echo": input}


async def main():
    neuron = Neuron(
        gateway_url="https://api.holokai.dev",
        neuron_id="my-neuron-1",
        auth=lambda: "<bearer token>",  # sync or async; called per request
        logger=logging.getLogger("neuron"),
    )

    neuron.register_handler(
        Capability(type="example/echo", scope="any", concurrency=4),
        echo,
        input_schema={"type": "object"},
        output_schema={
            "type": "object",
            "properties": {"echo": {}},
            "required": ["echo"],
        },
    )

    await neuron.start()
    try:
        await asyncio.Event().wait()  # run until cancelled
    finally:
        await neuron.stop(drain=True, timeout=30)


asyncio.run(main())

Concepts

Concept What it is
Neuron A process that registers itself with a BigBrain gateway and offers one or more capabilities.
Capability A namespaced task type (owner/package[/name][.variant]) with a scope ("any" or {onBehalfOf: userId}) and a per-capability concurrency.
Lease A single in-flight task delivery. Carries the input, schemas, and a deadline. The neuron acks (success), nacks (failure), or progresses.
Cancel The gateway can revoke a lease mid-flight. The handler's cancellation asyncio.Event is set, the task is cancelled, and a cancelled nack is sent.
Heartbeat The SDK posts a heartbeat every 10s with the current in-flight lease list. The gateway uses it to detect dead neurons and to renew lease TTLs for long-running tasks.

Wire protocol

  • Transport: SSE (server → neuron) + HTTPS POST (neuron → server).
  • Endpoints:
    • GET /neuron/events?neuronId=… — SSE stream
    • POST /neuron/{register, update-capability, heartbeat, ack, nack, progress}
  • Authentication: Authorization: Bearer <jwt>. The auth callback is invoked on every POST and SSE open, and is given one chance to refresh on 401 before the SDK escalates to reconnect-with-backoff.
  • Schemas: input/output JSON Schema arrives on the lease frame; the SDK validates with jsonschema and emits a schema-mismatch nack on failure.
  • Versioning: PROTOCOL_VERSION = "1.0.0" (free-form on the wire; the gateway negotiates compatibility).

Handler contract

async def handler(input, ctx: HandlerContext):
    # ctx.task         — the full task payload
    # ctx.lease_id     — the lease the gateway holds for this delivery
    # ctx.cancelled    — asyncio.Event set on cancel / shutdown
    # ctx.log          — logger scoped to this lease
    # ctx.progress(...) — non-blocking progress update
    # ctx.fail(reason, code=...) — terminal nack (do not catch)
    return {...}
  • Sync handlers are supported; the runner awaits awaitable return values and otherwise treats the return as the result.
  • Cancellation: handlers wrapping I/O should cooperate with ctx.cancelled (e.g. asyncio.wait with [ctx.cancelled.wait(), …]).
  • Idempotency is the handler author's responsibility — the framework does not checkpoint mid-task.

Failure classification

Outcome Wire kind
Handler returns ack
Handler raises asyncio.CancelledError (or cancel event set) nack: cancelled
ctx.fail(...) nack: terminal
Handler raises any other exception nack: retryable
Input/output fails JSON Schema nack: schema-mismatch

Retry policy lives on the workflow (server-side), never on the SDK.

Architecture

┌────────────┐   POST register / heartbeat / ack / nack / progress
│   Neuron   │──────────────────────────────────────────────────────▶│
│  (this    │                                                       │ BigBrain
│   SDK)    │◀──────────────────────────── SSE: lease / cancel ────│ gateway
└────────────┘
  • HttpTransport owns one SSE stream and a shared httpx.AsyncClient.
  • Neuron wires the transport to a heartbeat loop and a per-capability semaphore-bounded dispatcher.
  • run_lease(...) validates input, invokes the handler, validates output, and posts the right ack/nack frame.

Examples

Example Capability What it does
examples/http_fetch/ examples/http.fetch Fetches an arbitrary HTTP(S) URL via httpx and returns status/headers/body.
examples/web_search/ examples/web.search Keyless web search via DuckDuckGo HTML scrape. No API key, but inherently brittle — see the example README for caveats.

Each ships with input/output JSON Schemas, cooperative cancellation (ctx.cancelled), and a CLI runner. Run them the same way:

pip install -e .
BIGBRAIN_GATEWAY_URL=https://api.holokai.dev \
BIGBRAIN_NEURON_ID=my-python-neuron-1 \
BIGBRAIN_TOKEN=eyJ... \
python -m examples.http_fetch    # or examples.web_search

Custom transports

HttpTransport is the default but the public Transport shape is documented on TransportCallbacks and Neuron(transport=…) accepts any object that matches the protocol — handy for tests or in-process embeddings.

Tests

# Unit + deterministic e2e (default — no network).
pytest

# Network-gated e2e: hits real DuckDuckGo through the real SDK to catch
# scraper drift. Skipped unless explicitly enabled.
E2E_NETWORK=1 pytest -m e2e_network

The e2e tests stand up a tiny aiohttp server on loopback that serves both the BigBrain /neuron/* surface and a stub for DuckDuckGo, then run the real Neuron SDK against it. Every byte flows over real HTTP — SSE in, POSTs out — exercising the full register → lease → handler → ack loop. See tests/e2e/.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

holokai_neuron_sdk-0.1.1.tar.gz (33.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

holokai_neuron_sdk-0.1.1-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file holokai_neuron_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: holokai_neuron_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for holokai_neuron_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fec33b3539c70b71e8a1fccf5157cb6c69fee4ddd2c04517a699173fb462dc39
MD5 8cdc78a7fef6c207c5b5142ee13454da
BLAKE2b-256 d8e143bddc6b5ded21446f0f38a4e136f64dacb6bab13c48cf194f08d31f00ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for holokai_neuron_sdk-0.1.1.tar.gz:

Publisher: publish-neuron-sdk-python.yml on holok-ai/bigbrain

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file holokai_neuron_sdk-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for holokai_neuron_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 87fe564e84e41692033e60169a6d16faad96fa71c63abe835359bf8624a112f9
MD5 4be4e97486f6fd835aac95a0c79f0dc4
BLAKE2b-256 0a000b2254d83f781a1a888c1e7637c7d3829af03853702e72629ccc9b43cdaa

See more details on using hashes here.

Provenance

The following attestation bundles were made for holokai_neuron_sdk-0.1.1-py3-none-any.whl:

Publisher: publish-neuron-sdk-python.yml on holok-ai/bigbrain

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page