TruthVouch SDK — drop-in LLM governance for Python
Project description
TruthVouch Python SDK
Drop-in LLM governance for Python. Route OpenAI, Anthropic, and Google AI calls through the TruthVouch Governance Gateway for automatic policy enforcement, PII detection, and hallucination monitoring.
Installation
pip install truthvouch
Optional extras:
pip install "truthvouch[telemetry]" # OpenTelemetry + Prometheus
pip install "truthvouch[adapters]" # LangGraph + CrewAI integrations
pip install "truthvouch[all]" # Everything
Quick Start — Gateway Proxy
from truthvouch import TruthVouch
async with TruthVouch(gateway_url="https://gateway.truthvouch.com", api_key="tv_live_...") as client:
# OpenAI drop-in
resp = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What is quantum computing?"}],
)
print(resp.choices[0].message.content)
print(resp.governance.verdict) # "allowed" | "blocked" | "flagged"
print(resp.governance.pii_detected) # False
Provider Facades
# Anthropic
resp = await client.anthropic.messages.create(
model="claude-3-5-sonnet-20241022",
messages=[{"role": "user", "content": "Hello"}],
max_tokens=512,
)
print(resp.text)
# Google AI
resp = await client.google.generate_content(
model="gemini-1.5-pro",
contents=[{"role": "user", "parts": [{"text": "Hello"}]}],
)
print(resp.text)
Streaming
async for chunk in await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True,
):
print(chunk.content, end="", flush=True)
if chunk.done:
print(f"\nVerdict: {chunk.governance_report.verdict}")
Batch Scanning
job = await client.batch.submit(source_url="s3://my-bucket/prompts.jsonl", format="jsonl")
status = await client.batch.get_status(job.id)
print(status.status) # "completed"
Governance Guards
For fine-grained input/output evaluation with local and remote guards:
from truthvouch.governance import GovernanceClient
client = GovernanceClient(api_key="vt_live_...")
result = client.evaluate_input("My SSN is 123-45-6789", model="gpt-4o")
if result.blocked:
print("Blocked:", result.block_reasons)
# Async usage
from truthvouch.governance import AsyncGovernanceClient
async with AsyncGovernanceClient(api_key="vt_live_...") as client:
result = await client.evaluate_input("Hello!", model="gpt-4o")
Built-in Guards
| Guard | Type | Description |
|---|---|---|
pii_regex |
Local | Regex PII detection (SSN, CC, email, phone, passport, address) |
banned_phrases |
Local | Case-insensitive banned phrase matching |
injection |
Local | 2-layer prompt injection heuristic detector |
cost |
Local | Token estimation + budget enforcement |
truth |
Remote | Truth nugget verification |
pii_remote |
Remote | Full Presidio PII scan |
Framework Adapters
LangGraph / LangChain:
from truthvouch.governance.adapters.langgraph import TruthVouchCallbackHandler
handler = TruthVouchCallbackHandler(client, model="gpt-4o")
result = chain.invoke(inputs, config={"callbacks": [handler]})
CrewAI:
from truthvouch.governance.adapters.crewai import TruthVouchMiddleware
middleware = TruthVouchMiddleware(client)
llm = middleware.wrap_llm(ChatOpenAI(model="gpt-4o"))
agent = Agent(llm=llm, role="Analyst", goal="...", backstory="...")
Error Handling
from truthvouch.exceptions import (
PolicyBlockedError,
AuthenticationError,
QuotaExceededError,
GatewayUnreachableError,
)
try:
resp = await client.chat.completions.create(model="gpt-4o", messages=[...])
except PolicyBlockedError as e:
print(f"Blocked: {e.governance_report}")
except AuthenticationError:
print("Invalid API key")
except QuotaExceededError as e:
print(f"Rate limited, retry after {e.retry_after_seconds}s")
except GatewayUnreachableError:
print("Gateway down — call went direct to LLM (fail-open mode)")
Configuration
from truthvouch import TruthVouch
client = TruthVouch(
gateway_url="https://gateway.truthvouch.com",
api_key="tv_live_...",
fail_mode="open", # "open" (bypass) or "closed" (raise error)
timeout=30.0, # HTTP timeout in seconds
max_retries=3, # Retry attempts with exponential backoff
circuit_breaker_threshold=5, # Failures before circuit opens
circuit_breaker_recovery_seconds=60, # Seconds before recovery probe
verify_ssl=True, # TLS certificate verification
)
License
Apache-2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file truthvouch-1.0.0.tar.gz.
File metadata
- Download URL: truthvouch-1.0.0.tar.gz
- Upload date:
- Size: 74.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1f52afb4725e1ab1409fbcde313254023e8b093e6d5379e5cca2107967d74e5
|
|
| MD5 |
a4c56fad055e71922f296f55d2e62a7f
|
|
| BLAKE2b-256 |
106e8c5f6e864bd5db788662092675d56e5410bc8ec87d10e86e97f6ce5445ec
|
File details
Details for the file truthvouch-1.0.0-py3-none-any.whl.
File metadata
- Download URL: truthvouch-1.0.0-py3-none-any.whl
- Upload date:
- Size: 70.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b58ed15de198a81af2144ea8f9bfc6977851a657df465a6963e871bd10df6016
|
|
| MD5 |
51b9b47fabe5a05a3a9043d081808eab
|
|
| BLAKE2b-256 |
e8f6312f1c861f362e3649190eb5f452bd553b0605462268502a71834a4be888
|