Reference
Riskbound SDK (Python)
The Python SDK. It asks Riskbound before an AI agent acts, then carries out
the answer. It matches the TypeScript SDK in ../sdk in behaviour and on the
wire. It has no runtime dependencies, needs Python 3.10 or later, and is not
published yet.
import os
from riskbound import Riskbound, RiskboundBlockedError
rb = Riskbound(api_key=os.environ["RISKBOUND_API_KEY"], base_url="http://localhost:8200")
refund = rb.guard("refund", agent="support-bot")
try:
result = refund(
{"order_id": "ord_2291", "amount_minor": 48000, "currency": "USD"},
lambda: payments.refund("ord_2291", 48000),
)
# result.outcome is "allow" or "ask_human"; result.value is what the action returned.
except RiskboundBlockedError as err:
# err.reasons says why: a policy id, "spend_cap", "kill_switch", ...
...For asyncio code, use AsyncRiskbound. Actions may be async or plain
functions. HTTP runs in a worker thread, so the event loop is not blocked.
from riskbound import AsyncRiskbound
rb = AsyncRiskbound(api_key=..., base_url=...)
refund = rb.guard("refund", agent="support-bot")
result = await refund({"amount_minor": 48000, "currency": "USD"}, do_refund_async)What happens for each outcome#
| Outcome | What the SDK does |
|---|---|
allow | Runs the action and reports the result |
ask_human | Waits for a person, polling every approval_poll_interval seconds. Runs on approval; raises RiskboundBlockedError on rejection or expiry, RiskboundApprovalTimeoutError after approval_timeout seconds |
dry_run | Runs the dry_run= handler you passed, never the real action. Without a handler, policy never offers a dry run, and if it does anyway the call is blocked |
block | Raises RiskboundBlockedError. The action does not run |
When Riskbound cannot be reached#
By default the guard fails closed: it raises RiskboundUnavailableError and
the action does not run. That covers no connection, a timeout (timeout=,
3 seconds by default) and any 5xx answer. Pass fail_mode="open" to a guard
to run the action unchecked instead; the result says outcome="fail_open"
and the error goes to on_error. Fail-open never applies to a rejected
request (4xx, RiskboundRequestError), and never once a person has been asked.
Reporting the result#
After the action runs, the SDK tells Riskbound whether it worked. A raised
exception is reported as failed, with its message, and raised again
unchanged. By default the guard waits for the report. Pass
report_result="background" to send it after: on a daemon thread for
Riskbound, as a task for AsyncRiskbound (call await rb.drain() before the
program exits). A failed report goes to on_error and never changes the
result.
Other options#
idempotency_key=on a call: send the same key again to get the same answer instead of a new decision.- Requests carry
via: "sdk", which the risk score uses (../docs/adr/0010-risk-score.md). transport=,sleep=andclock=are for tests.
Amounts#
Send amounts as whole numbers in minor units (amount_minor: 48000 for
$480.00). Floats are rejected by the backend.
Develop#
uv venv .venv && uv pip install -e '.[dev]'
.venv/bin/ruff check . && .venv/bin/ruff format --check . && .venv/bin/mypy && .venv/bin/pytestTests run the SDK against a stand-in Riskbound API on a real local HTTP
server (tests/fake_server.py), so the real transport is used.