TruthLayer — AI Hallucination Verification SDK. One-line verification of AI outputs against source documents.
Project description
TruthLayer Python SDK
One-line AI hallucination verification. Zero dependencies.
Install
pip install truthlayer-sdk
With LangChain integration:
pip install truthlayer-sdk[langchain]
Quick Start
from truthlayer import TruthLayer
tl = TruthLayer(
api_key="tl_your_key_here",
api_url="https://qoa10ns4c5.execute-api.us-east-1.amazonaws.com/prod"
)
# Verify an AI response against source documents
result = tl.verify(
ai_response="Refunds are processed within 5-7 business days.",
source_documents=["Our refund policy allows returns within 30 days. Refunds are processed within 5-7 business days."]
)
print(f"Trust Score: {result.trust_score}%")
print(f"Claims: {result.total_claims}")
print(f"Hallucinations: {result.has_hallucinations}")
print(f"Latency: {result.latency_ms}ms")
API Reference
TruthLayer(api_key, api_url, timeout=30)
Initialize the client.
| Parameter | Type | Required | Description |
|---|---|---|---|
api_key |
str | Yes | Your API key (starts with tl_) |
api_url |
str | Yes | Base URL of your TruthLayer deployment |
timeout |
int | No | HTTP timeout in seconds (default: 30) |
tl.verify(ai_response, source_documents=None, document_ids=None)
Verify AI output against source documents.
Returns VerificationResult with:
| Property | Type | Description |
|---|---|---|
claims |
List[Claim] |
Individual claim verdicts |
trust_score |
float |
Overall trust score (0-100) |
has_hallucinations |
bool |
True if any unsupported claims |
verified_count |
int |
Number of verified claims |
unsupported_count |
int |
Number of unsupported claims |
latency_ms |
float |
API response time |
tl.upload_document(content, title="", metadata=None)
Upload a document for later verification by ID.
tl.get_document(document_id) / tl.list_documents() / tl.delete_document(document_id)
Document management endpoints.
tl.health()
Check API health status.
LangChain Integration
from truthlayer.langchain import TruthLayerOutputParser
parser = TruthLayerOutputParser(
api_key="tl_xxx",
api_url="https://your-api/prod",
source_documents=["Your source text here."],
min_trust_score=70.0,
)
# Use in a chain — blocks hallucinations automatically
chain = llm | parser
result = chain.invoke("What is our refund policy?")
print(result.trust_score) # VerifiedOutput with trust score
Error Handling
from truthlayer import TruthLayer, TruthLayerError
tl = TruthLayer(api_key="tl_xxx", api_url="https://your-api/prod")
try:
result = tl.verify("AI response", ["Source text"])
except TruthLayerError as e:
print(f"API error: {e}")
except ValueError as e:
print(f"Invalid input: {e}")
Get an API Key
Visit your TruthLayer dashboard or use the API directly:
import requests
response = requests.post(
"https://qoa10ns4c5.execute-api.us-east-1.amazonaws.com/prod/keys",
json={"owner": "Your Name", "email": "you@company.com", "use_case": "Chatbot"}
)
key = response.json()["api_key"] # Save this — shown once only
License
MIT
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 truthlayer_sdk-0.1.0.tar.gz.
File metadata
- Download URL: truthlayer_sdk-0.1.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51d29de7debba255ff2629f795bac9c18e916c0535aa473560d2d5700e7b0a20
|
|
| MD5 |
3ff35accae0aa407bf3460c452a54467
|
|
| BLAKE2b-256 |
1ab4b15ed8f016fac4fd5beaee24710c699f5bf3edbb86e2641ffa4cb88d6113
|
File details
Details for the file truthlayer_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: truthlayer_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
776f67257be8b8bd54a627d757e25e8fb8599f8d591edbac9dfac8376439eba4
|
|
| MD5 |
6aa8d24ee61eb005d8cbea304122984b
|
|
| BLAKE2b-256 |
566735591547e47121677dec02fce8430832338511d5272153df0e56865bee49
|