Skip to main content

Bayesian LLM Guard

PyPI version

Epistemic Uncertainty Estimation & Guardrails for Agentic LLMs and RAG Systems

Bayesian LLM Guard is an enterprise-grade middleware designed to detect and prevent Large Language Model (LLM) hallucinations. By leveraging Vectorized Monte Carlo Dropout, it calculates the epistemic variance of neural network representations, allowing you to intercept uncertain or fabricated responses in real-time before they reach the user.

Features

  • High-Throughput GPU Batching: Utilizes tensor batching for zero-latency uncertainty quantification.
  • Deterministic Guardrails: Decorator-based interception (@uq_guard) for clean integration into RAG pipelines.
  • Autonomous Self-Correction: Seamlessly integrates with Agentic loops for query refinement upon hallucination detection.
  • Model Agnostic: Works with any underlying PyTorch-based neural architecture.

Installation

Stable release from PyPI:

pip install bayesian-llm-guard

Quick Start

1. Basic Uncertainty Quantification

import torch
import torch.nn as nn
from bayesian_llm_guard import UQEngine

# Initialize with your custom PyTorch model
model = nn.Sequential(nn.Linear(768, 256), nn.Dropout(0.5))
engine = UQEngine(model=model, mc_samples=30)

# Input features (e.g., embeddings from an LLM)
features = torch.randn(1, 768)

# Estimate epistemic variance
variance = engine.estimate_variance(features)
print(f"Model Uncertainty (Variance): {variance}")

2. Using the Guardrail Decorator

Integrate directly into your RAG or LLM generation pipeline to automatically block uncertain responses.

from bayesian_llm_guard import UQGuardConfig, uq_guard, UQGuardException

config = UQGuardConfig(threshold=0.15)

@uq_guard(uq_engine=engine, config=config)
def generate_response(prompt: str):
    # Your RAG/LLM logic here
    # The function must return a dictionary containing 'tensor_features'
    return {
        "response": "The capital of France is Paris.",
        "tensor_features": torch.randn(1, 768)
    }

try:
    result = generate_response("What is the capital of France?")
    print(result["response"])
except UQGuardException as e:
    print(f"Blocked due to high uncertainty: {e}")

Architecture

Developed and maintained by the XAIDeep Research Team. The core mathematical engine relies on stochastic forward passes with active dropout layers during inference, capturing the model's internal confidence distribution without requiring external APIs or secondary evaluation models.

License

Apache License 2.0

Release files for bayesian-llm-guard 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bayesian-llm-guard 1.0.1
File Size Uploaded
bayesian_llm_guard-1.0.1.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bayesian-llm-guard 1.0.1
File Interpreter ABI Platform
bayesian_llm_guard-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / bayesian_llm_guard-1.0.1.tar.gz

Download URL bayesian_llm_guard-1.0.1.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2897f27b7dd6ab0c7fd690d01a5c1fc9148440e1383a00b3fd66b0fd4b508c0a
BLAKE2b-256 checksum
How to use checksums
b9836223035b7564213ac8e336dab4b5f90a8a01cab7b1096d853c408d276d85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.

Transparency log

Release files / bayesian_llm_guard-1.0.1-py3-none-any.whl

Download URL bayesian_llm_guard-1.0.1-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
999356fd8ef10f14a4e98fe27d0fb3a56a5528b996bca4d6df1f9a4f8223ef36
BLAKE2b-256 checksum
How to use checksums
7d81f83dbb1510b8acfbf07b9db71cb7b7259bd828fce295b768ac65ee2a7bed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page