Skip to main content

Python SDK for the qbrix multi-armed bandit platform

Project description

Qbrix

Python SDK for the qbrix platform.

License Coverage Python 3.10+ Pydantic v2 Transports


Typed sync and async clients for Qbrix — pool/experiment/gate management and the agent select/feedback loop.

Installation

The SDK speaks to the qbrix proxy over either HTTP/JSON or gRPC — pick a transport (or install both):

pip install qbrix[http]   # HTTP/JSON transport (httpx)
pip install qbrix[grpc]   # gRPC transport (grpcio)
pip install qbrix[all]    # both

pip install qbrix with no extra installs only the models and config layer — constructing a client then raises a clear ImportError naming the extra to add.

Quick Start

Set your credentials as environment variables and call resources directly — no client instantiation needed:

export QBRIX_API_KEY="optiq_xxx"
export QBRIX_BASE_URL="https://cloud.qbrix.io"
import qbrix

# 1. Create a pool of arms (variants)
pool = qbrix.pool.create(
    name="homepage-buttons",
    arms=[{"name": "blue"}, {"name": "green"}, {"name": "red"}],
)

# 2. Create an experiment with a bandit policy
exp = qbrix.experiment.create(
    name="button-color-test",
    pool_id=pool.id,
    policy="BetaTSPolicy",
)

# 3. Select an arm for a user
result = qbrix.agent.select(
    experiment_id=exp.id,
    context={"id": "user-123", "metadata": {"country": "US"}},
)
print(result.arm.name)       # "green"
print(result.is_default)     # False (bandit selected)

# 4. Send feedback (reward) after observing the outcome
qbrix.agent.feedback(request_id=result.request_id, reward=1.0)

The system learns from every reward and adjusts future selections automatically.

Transports

The same Qbrix client, resources, and Pydantic models work over either wire format — only the transport differs:

from qbrix import Qbrix

# HTTP (default)
client = Qbrix(transport="http", base_url="https://cloud.qbrix.io")

# gRPC
client = Qbrix(transport="grpc", base_url="grpcs://cloud.qbrix.io:443")

When transport is omitted it's resolved in this order: the transport= kwarg → the QBRIX_TRANSPORT env var → the base_url scheme (grpc:// / grpcs:// → gRPC) → HTTP.

The gRPC transport covers pool, experiment, gate, and agent operations. The auth, policy, and runtime resources are HTTP-only (the proxy doesn't expose them over gRPC) — calling them on a gRPC client raises NotImplementedError. Install qbrix[all] and use transport="http" if you need them.

Explicit Client

For full control over configuration or lifecycle (e.g. closing the transport connection, using a context manager), instantiate the client directly:

from qbrix import Qbrix

with Qbrix(api_key="optiq_xxx", base_url="https://cloud.qbrix.io") as client:
    pool = client.pool.create(
        name="homepage-buttons",
        arms=[{"name": "blue"}, {"name": "green"}, {"name": "red"}],
    )
    result = client.agent.select(experiment_id="exp-uuid", context={"id": "user-123"})
    client.agent.feedback(request_id=result.request_id, reward=1.0)

Async

from qbrix import AsyncQbrix

async with AsyncQbrix(api_key="optiq_xxx") as client:
    result = await client.agent.select(
        experiment_id="exp-uuid",
        context={"id": "user-456"},
    )
    await client.agent.feedback(request_id=result.request_id, reward=1.0)

Configuration

Constructor kwargs take priority over environment variables, which take priority over defaults.

export QBRIX_API_KEY="optiq_xxx"
export QBRIX_BASE_URL="https://cloud.qbrix.io"
from qbrix import Qbrix

client = Qbrix()  # picks up env vars automatically
Env Var Default Description
QBRIX_API_KEY None API key (optiq_xxx)
QBRIX_BASE_URL http://localhost:8080 Proxy service URL
QBRIX_TRANSPORT (auto) http or grpc — overrides URL-scheme detection
QBRIX_TIMEOUT 30.0 Request timeout / gRPC deadline (seconds)
QBRIX_MAX_RETRIES 2 Retry count on transient failures (429/5xx, gRPC UNAVAILABLE)

gRPC-only knobs: QBRIX_GRPC_KEEPALIVE_TIME_MS (30000), QBRIX_GRPC_KEEPALIVE_TIMEOUT_MS (10000), QBRIX_GRPC_USE_TLS (false).

Feature Gates

Attach a feature gate to control rollout before the bandit kicks in:

import qbrix

qbrix.gate.create(
    experiment_id=exp.id,
    enabled=True,
    rollout_percentage=80.0,
    default_arm_id=pool.arms[0].id,
    rules=[
        {"key": "plan", "operator": "==", "value": "enterprise", "arm_id": pool.arms[1].id},
    ],
)

# Gate-matched selections return is_default=True
result = qbrix.agent.select(
    experiment_id=exp.id,
    context={"id": "user-789", "metadata": {"plan": "enterprise"}},
)
print(result.is_default)  # True

Error Handling

import qbrix
from qbrix import NotFoundError, RateLimitedError

try:
    exp = qbrix.experiment.get("nonexistent-id")
except NotFoundError as e:
    print(f"Not found: {e.detail}")
except RateLimitedError as e:
    print(f"Retry after {e.retry_after}s")

Supported Policies

Policy Type Best For
BetaTSPolicy Stochastic Binary rewards (clicks, conversions)
GaussianTSPolicy Stochastic Continuous rewards
UCB1TunedPolicy Stochastic Theoretical regret guarantees
KLUCBPolicy Stochastic Binary rewards with tight bounds
MOSSPolicy Stochastic Fixed horizon problems
LinUCBPolicy Contextual Linear reward models with features
LinTSPolicy Contextual Linear models with uncertainty
EXP3Policy Adversarial Non-stationary environments
FPLPolicy Adversarial Follow the perturbed leader

License

MIT

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

qbrix-0.2.0.tar.gz (185.8 kB view details)

Uploaded Source

Built Distribution

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

qbrix-0.2.0-py3-none-any.whl (50.0 kB view details)

Uploaded Python 3

File details

Details for the file qbrix-0.2.0.tar.gz.

File metadata

  • Download URL: qbrix-0.2.0.tar.gz
  • Upload date:
  • Size: 185.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for qbrix-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1b337901775dd57c9612cb6831d85751e54b6b0d876e395f49582a6a965e6a52
MD5 540b2b74f4ac877ef23326877acbcc0e
BLAKE2b-256 465bae1ee004c5554fe30e3739dfba4786579d5034cb6493373b5b1507d1d1e0

See more details on using hashes here.

File details

Details for the file qbrix-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: qbrix-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 50.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for qbrix-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 670f311dc03ac4c685b6c1580ff4ee93818da6a24eb6e2e885deafef25cf65c7
MD5 0928166099ebc2ddc2d0538b94d228ae
BLAKE2b-256 9534f5d9674ad256caee1d447fb88e413c6e61cacf6d8b5acc48397358c7cd71

See more details on using hashes here.

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