Python Integration Guide
Govern AI content in Python — either on-device with tork-governance, or server-side via the REST API. Includes FastAPI, Flask, and Django integrations.
Prerequisites
- Python 3.10 or higher
- No API key is needed for on-device governance. A Tork API key is required only for the REST examples (get one here)
- pip or poetry for package management
Two different packages — pick deliberately
tork-governance (PyPI) is the on-device package. Its import root is tork_governance and its entry class is Tork. tork-governance makes the governance decision on your machine. PII detection, redaction and the returned verdict are computed on-device. With no API key it makes zero network calls: prompts, completions and detected PII values never leave the machine, and nothing appears in your dashboard.
The REST API (and @torknetwork/sdk on npm) sends content to Tork, which makes the decision server-side and writes a receipt to your dashboard. Different package, different trust model. There is no published Python SDK for this path — use requests or httpx against /api/v1/govern, as shown below.
TorkClient and AsyncTorkClient are not part of tork-governance and never have been. Importing them from it raises an ImportError.
Local governance is free and unlimited — decisions made on your own machine are never metered. Evidence is what is metered: attestations, receipts and anchoring.
Installation
Install the on-device package, or just an HTTP client for the REST API.
Never commit your API key. Add .env to .gitignore and use python-dotenv to load it.
On-device SDK (tork-governance)
Decisions are made in-process. govern() is synchronous and does no I/O.
Server-governed REST API
Send content to Tork and get a dashboard receipt back. Requires an API key.
Error Handling & Retry Logic
Handle API errors gracefully with exponential backoff.
HTTP Status Codes
| 200 | Success | Process response |
| 400 | Bad request | Fix request body, don't retry |
| 401 | Unauthorized | Check API key, don't retry |
| 429 | Rate limited | Wait and retry (check Retry-After) |
| 500+ | Server error | Retry with exponential backoff |
Framework Integrations
Ready-to-use examples for popular Python web frameworks
Batch Processing
Evaluate multiple items efficiently with controlled concurrency.
Reporting to your dashboard
Metadata-only attestation from the on-device SDK.
Supply an API key and the SDK additionally reports a metadata-only attestation of each decision — the action taken, PII type labels and counts, a risk classification, policy labels and a salted fingerprint. It never sends input text, output text or PII values. The decision itself is still made on-device and is never delayed or changed by reporting. Those attestations appear in your dashboard and are included in the daily on-chain anchor — each one recorded as a client attestation (capture_mode=edge, attested_by=client), a claim Tork recorded but did not execute and cannot independently verify. Requires tork-governance 0.24.0+ (PyPI) or 0.11.0+ (npm).
A decision reported by an on-device SDK is recorded as a client attestation (capture_mode=edge, attested_by=client): a claim Tork recorded but did not execute and cannot independently verify. A decision made by @torknetwork/sdk is recorded as capture_mode=cloud, attested_by=tork — Tork made that call itself. Both are equally immutable once anchored; they differ in what is immutable. A client attestation freezes your claim. A server-governed receipt freezes Tork's own decision.
Best Practices
Know which package you installed
tork-governance exports Tork and decides on-device. TorkClient belongs to @torknetwork/sdk and decides server-side. Mixing them is the single most common integration failure.
Pass the redacted output downstream
Use result.output when calling your model or logging. Passing the original text forward defeats the redaction you just performed.
Don't await govern() on-device
It is synchronous in both Python and JavaScript. There is no network call to wait for, and no client to close.
Use attribute access, not subscripts
GovernanceResult is a dataclass: result.action works, result['action'] raises TypeError.
Decide how YOUR call fails
Wrap your own governance call and choose whether an unexpected error in your code path allows or blocks the request. This is your error-handling policy, not a statement about Tork's availability.
Use middleware for web frameworks
Middleware provides consistent protection across all endpoints without code duplication.
Typed dataclasses
tork-governance returns dataclasses (GovernanceResult, PIIResult, Receipt) with inline annotations, so editors resolve result.action and result.pii.count. The distributed package does not yet ship a py.typed marker, so strict mypy/pyright runs will treat it as untyped unless you opt in explicitly.
Next Steps
See the API reference for the full /api/v1/govern contract, or the quickstart for the shortest path from install to a first decision.