Python SDK
First-party Python SDK for ContractGate β sync & async HTTP client, plus a pure-Python local validator for unit tests and pre-commit hooks.
Python 3.9+httpx async/syncMIT Licensev0.1.0RFC-005 Β· Accepted
ΒΆInstallation
Requires Python 3.9+. Runtime dependencies: httpx, PyYAML.
pip install contractgatebashPython 3.9+httpx β₯0.25,<1.0PyYAMLMIT License
ΒΆQuick Start β HTTP Client (Sync)
Use Client to send events to the ContractGate gateway and inspect per-event results.
from contractgate import Client
cg = Client(base_url="https://gw.example.com", api_key="cg_live_...")
result = cg.ingest(
contract_id="11111111-1111-1111-1111-111111111111",
events=[
{"user_id": "alice_01", "event_type": "click", "timestamp": 1712000000},
],
)
print(result.passed, "/", result.total, "events passed")
for r in result.results:
if not r.passed:
for v in r.violations:
print(v.field, v.kind, v.message)pythonΒΆQuick Start β HTTP Client (Async)
AsyncClient is the async equivalent. It wraps httpx.AsyncClient and supports async with for lifecycle management.
import asyncio
from contractgate import AsyncClient
async def main():
async with AsyncClient(base_url="...", api_key="...") as cg:
result = await cg.ingest(contract_id="...", events=[...])
asyncio.run(main())pythonβΉ
Client and AsyncClient share the same transport layer and response models β they can never drift on header or auth shape.ΒΆQuick Start β Local Validator
A pure-Python port of the Rust validator β no network required. Useful in unit tests and pre-commit hooks.
from contractgate import Contract
contract = Contract.from_yaml(open("user_events.yaml").read())
compiled = contract.compile()
vr = compiled.validate({
"user_id": "alice_01",
"event_type": "click",
"timestamp": 1712000000,
})
assert vr.passed, vr.violationspythonβ
The local validator does not run RFC-004 PII transforms (
mask, hash, drop, redact). It is read-only β pass/fail and violations only. The gateway's transformed_eventfield is the authoritative "what got stored" view.ΒΆCaveats
Local validator does not run PII transforms
The per-contract PII salt is server-side only and never returned in API responses (RFC-004). The local validator is for assert event_passes_contract(β¦)in user tests β not for replicating the gateway's storage path. Read
transformed_event from each per-event result for the post-transform payload.Audit honesty
Every per-event result carries the
contract_version that actually matched the event (relevant under multi_stable_resolution: fallback). Surface it as-is β do not substitute the requested version.Retries are off by default
Layer
httpx.HTTPTransport(retries=) or tenacityif you need them. Avoid client-side retry on ingest to prevent double-write β use the gateway's quarantine replay endpoint instead.httpx pin
httpx is pinned >=0.25,<1.0; the upper bound will widen once httpx 1.x ships and is tested.Ready to integrate?
Get your API key and contract UUID from the Account page, then pip install contractgate.