Verification over trust.
How PatraSaar eliminates legal hallucinations by combining in-process hybrid retrieval, structured grammar constraints, and a zero-LLM deterministic AST verifier.
The End-to-End Pipeline Philosophy
PHASE 1
In-Process Hybrid Search
Sparse BM25 + 384-d dense embeddings in a contiguous Float32 binary file fused via RRF. Exact sections pinned with score 9999.
PHASE 2
Constrained Generation
The LLM is treated as an untrusted writer locked into a 14k context budget and forced to output in an AST citation grammar.
PHASE 3
Deterministic Verification
A non-LLM parser verifies existence, prompt provenance, and verbatim substring match against the Gazette before the user sees it.
Step-by-Step Architecture
The 8 stages of statutory verification.
Click on any stage below to inspect the engineering implementation, mathematical formulation, and sample TypeScript code.
Statutory PDF Parsing & Normalization
Transforming messy gazette typography into clean semantic sections
Official Indian Bare Act PDFs from indiacode.nic.in are heavily corrupted with page headers, line wraps, footnote references, and amendment brackets. PatraSaar extracts the text with unpdf, strips running headers, unwraps amendment markers (e.g. 1[imprisonment for life] → imprisonment for life), and regex-splits each section into an atomic unit preserving its section number, marginal title, and text.
Engineering Rationale
Unlike blind chunking (e.g. 500-token chunks), section-aware extraction preserves statutory boundaries so legal citations always point to real provisions.
Indexed Acts
15 Bare Acts
Total Sections
3,630 Sections
Corpus Size
14.45 MB JSON
// scripts/build-corpus.ts (Excerpt)
const headingRegex = /^(\d+[A-Z]*)\.\s+([^.\n]+)\.\s*([\s\S]*)/m;
const unwrappedText = rawText
.replace(/\d+\[([^\]]+)\]/g, '$1') // unwrap amendments
.replace(/\r?\n\s*\d+\s*\r?\n/g, ' ') // strip page numbers
.replace(/\s+/g, ' '); // normalize whitespaceAlgorithmic Rigor
The mathematical foundations of PatraSaar.
Every retrieval score and verification status is derived from reproducible, transparent formulas rather than opaque black-box heuristics.
1. Okapi BM25 with Title-Phrase Boosting
Score(D, Q) = \sum_{i=1}^n IDF(q_i) \cdot \frac{f(q_i, D) \cdot (k_1 + 1)}{f(q_i, D) + k_1 \cdot (1 - b + b \cdot \frac{|D|}{\text{avgdl}})}In statutory law, exact words matter. BM25 scores sections using term saturation (k1 = 1.2) and document length normalization (b = 0.75). PatraSaar adds an aggressive Title-Phrase multiplier: if a multi-word phrase from the query appears contiguously in the marginal title of a section, its BM25 score is multiplied by 1.5x.
2. Contiguous Float32 Dense Vector Space
\text{Sim}(\vec{q}, \vec{d}) = \sum_{k=1}^{384} q_k \cdot d_kRather than relying on expensive and privacy-compromising external vector cloud databases, all 3,630 sections are pre-computed using Xenova/all-MiniLM-L6-v2 and stored in a single 5.32 MB binary Float32Array. Because each 384-dimensional vector is L2-normalized during corpus compilation, cosine similarity reduces to an ultra-fast raw dot product scanned in < 2ms on CPU.
3. Reciprocal Rank Fusion (RRF)
RRF(d) = \sum_{m \in M} \frac{1}{k + r_m(d)} \quad (k = 60)BM25 scores and vector dot-products exist on completely different numerical scales. Reciprocal Rank Fusion overcomes score calibration issues by considering only the ordinal rank of each document. With k=60, a section that ranks highly in both lexical and dense lists is exponentially elevated above candidate noise.
4. Normalized AST String Verifier
\text{Clean}(S) = S.\text{lower}().\text{stripPunct}().\text{collapseWs}()The citation verifier is a pure, non-LLM algorithmic validator. Quotes and bare acts are passed through a rigorous unicode normalization pipeline: smart quotes (“, ”) and dashes (—, –) are unified, ligatures are decomposed, punctuation is stripped, and whitespace is collapsed before executing a zero-tolerance substring check.
Architectural Contrast
Why deterministic AST verification beats “LLM-as-a-Judge”.
Many modern AI frameworks use a second LLM to evaluate the first LLM. In statutory law, this is mathematically flawed.
The “LLM-as-a-Judge” Anti-Pattern
Prompting another model with “Evaluate if this citation is correct” creates a circular epistemic loop:
- ✕Second-Order Hallucinations: The evaluator LLM can hallucinate statutory text just as easily as the generation LLM.
- ✕Sycophancy & Tone Bias: Evaluator LLMs routinely accept false propositions if written with high legal confidence.
- ✕Nondeterministic Flakiness: The same citation can pass at 9:00 AM and fail at 9:05 AM due to temperature variance.
- ✕Severe Latency & Cost: Doubles token expenditure and introduces 3-5 seconds of additional API roundtrip delay.
PatraSaar's Deterministic Verifier
Treating statutory verification as a syntax compilation check:
- ✓Binary Ground Truth: Quotes either appear verbatim in the Bare Act or they do not. There is no probability involved.
- ✓Zero Evaluator Hallucination: Regular expressions and string equality algorithms cannot hallucinate.
- ✓100% Reproducible: Deterministic unit tests assert exact match results on every Git commit in CI.
- ✓Sub-Millisecond Execution: AST parsing and substring matching execute in < 1ms on the Node.js server.
Inspect the pipeline in action.
Test our hybrid RRF retrieval and deterministic citation verification on real Indian statutory questions.