Skip to main content

RAG verification guardrails — detect hallucinations in LLM responses using hybrid STS + NLI.

Project description

PyPI version Python License: MIT CI

LongTracer

Detect hallucinations in LLM-generated responses. LongTracer verifies every claim against source documents using hybrid STS + NLI, works with any RAG framework, and traces the full verification pipeline.

Quick Start

pip install longtracer
from longtracer import CitationVerifier

verifier = CitationVerifier()
result = verifier.verify_parallel(
    response="The Eiffel Tower is 330 meters tall and located in Berlin.",
    sources=["The Eiffel Tower is a wrought-iron lattice tower in Paris, France. It is 330 metres tall."]
)

print(result.trust_score)         # 0.0 - 1.0
print(result.hallucination_count) # 1 ("Berlin" contradicts "Paris")
print(result.all_supported)       # False

That's it. No vector store dependency, no LLM dependency. Just strings in, verification out.

How It Works

  1. Claim splitting — LLM response is split into individual sentences/claims
  2. STS matching — Fast bi-encoder (all-MiniLM-L6-v2) finds the best-matching source sentence for each claim
  3. NLI verification — Cross-encoder (nli-deberta-v3-xsmall) classifies entailment/contradiction/neutral
  4. Verdict — Trust score computed, hallucinations flagged

Framework Adapters

LangChain (3 lines)

pip install "longtracer[langchain]"
from longtracer import LongTracer, instrument_langchain

LongTracer.init(verbose=True)
instrument_langchain(your_chain)
# Your chain.invoke() now auto-verifies every response

LlamaIndex (3 lines)

pip install "longtracer[llamaindex]"
from longtracer import LongTracer, instrument_llamaindex

LongTracer.init(verbose=True)
instrument_llamaindex(your_query_engine)

Direct API (any framework)

from longtracer.guard.verifier import CitationVerifier

verifier = CitationVerifier()
result = verifier.verify_parallel(
    response="LLM said this...",
    sources=["chunk 1 text", "chunk 2 text"],
    source_metadata=[{"source": "doc.pdf", "page": 1}, {"source": "doc.pdf", "page": 2}]
)

Works with Haystack, custom pipelines, or any code that produces strings.

Multi-Project Tracing

Track multiple RAG applications independently:

from longtracer import LongTracer

LongTracer.init(project_name="chatbot-prod", backend="sqlite")

# Get project-specific tracers
chatbot = LongTracer.get_tracer("chatbot-prod")
search  = LongTracer.get_tracer("search-api")

# Each project's traces are tagged and filterable
chatbot.start_root(inputs={"query": "..."})

Vector Store & LLM Agnostic

The SDK core takes plain str and List[str]. It does not depend on any vector store (Chroma, FAISS, Pinecone, Weaviate, Qdrant, pgvector) or any LLM provider (OpenAI, Anthropic, Ollama, Bedrock). Use whatever you want — LongTracer just verifies the output.

Trace Storage Backends

LongTracer.init(backend="sqlite")   # default — persists to ~/.longtracer/traces.db
LongTracer.init(backend="memory")   # in-memory, lost on restart
LongTracer.init(backend="mongo")    # production, distributed
Backend Install Where traces live
SQLite built-in (default) ~/.longtracer/traces.db
Memory built-in RAM only, lost on restart
MongoDB pip install "longtracer[mongo]" MongoDB database
PostgreSQL pip install "longtracer[postgres]" PostgreSQL database
Redis pip install "longtracer[redis]" Redis key-value store

Viewing Traces

CLI

longtracer view                        # list recent traces
longtracer view --last                 # view most recent
longtracer view --id <trace_id>        # view specific trace
longtracer view --project chatbot-prod # filter by project
longtracer view --export <trace_id>    # export to JSON
longtracer view --html <trace_id>      # export to HTML report

Console (verbose mode)

[longtracer] span=retrieval    chunks=5
[longtracer] span=llm_call     answer_len=179
[longtracer] span=eval_claims  total=3 supported=2
[longtracer] span=grounding    score=0.67 verdict=FAIL

HTML Report

from longtracer.guard.trace_report import export_trace_html
export_trace_html(tracer, filepath="report.html")

Generates a self-contained HTML file with trust score, per-claim results, timing breakdown — viewable in any browser, no external dependencies.

JSON Export

from longtracer.guard.trace_report import export_trace_json
export_trace_json(tracer, filepath="trace.json")

Optional Dependencies

Extra Install What it adds
langchain pip install "longtracer[langchain]" LangChain callback adapter
llamaindex pip install "longtracer[llamaindex]" LlamaIndex event adapter
mongo pip install "longtracer[mongo]" MongoDB trace backend
postgres pip install "longtracer[postgres]" PostgreSQL trace backend
redis pip install "longtracer[redis]" Redis trace backend
chroma pip install "longtracer[chroma]" ChromaDB + HuggingFace embeddings
all pip install "longtracer[all]" Everything

Environment Variables

Variable Default Description
LONGTRACER_ENABLED false Auto-enable with LongTracer.auto()
LONGTRACER_VERBOSE false Print per-span summaries
LONGTRACER_LOG_LEVEL INFO Python logging level
TRACE_CACHE_BACKEND sqlite Trace storage: sqlite, memory, mongo, postgres, redis
MONGODB_URI MongoDB connection URI
POSTGRES_HOST PostgreSQL host
REDIS_HOST Redis host
TRACE_PROJECT longtracer Default project name

Demo Application

The examples/ directory contains a complete RAG demo using ChromaDB + Ollama. It is NOT part of the published PyPI package. See examples/README.md for setup instructions.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

longtracer-0.1.0.tar.gz (42.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

longtracer-0.1.0-py3-none-any.whl (56.9 kB view details)

Uploaded Python 3

File details

Details for the file longtracer-0.1.0.tar.gz.

File metadata

  • Download URL: longtracer-0.1.0.tar.gz
  • Upload date:
  • Size: 42.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for longtracer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7f170f5f09771a71f4207b14aa34e808936d053284762903e71d95449d598ac7
MD5 9e5e66e0e16b8d3d7ef92a8b2dfffc75
BLAKE2b-256 93dcb8b8a253a688b25bcd383cc4c486f54910834d00b7de5d7522fbcc7cccba

See more details on using hashes here.

File details

Details for the file longtracer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: longtracer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 56.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for longtracer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cea49ab97fdc2b4e14c376e8d3f1fd59dfb6a5fd0c5729d1cfc319994f4730d9
MD5 fccf370e8ac19fa35bfce4b6d08e2364
BLAKE2b-256 4631faddf12ed738fd091f6aa5a98f33293179b3386d425814acd685a7d7b880

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page