Back to selected work

PROJECT 02 / Legal AI & verified retrieval

Statute Finder India.

Making AI-generated legal references verifiable by design.

A legal retrieval system combining hybrid search with deterministic citation validation and source-linked statutory text.

THE APPLICATION RESULT

The complete result: model-written reading aid, verbatim statute text, the cited source page with the quotation highlighted, and the limits list.

Statute Finder India result for a phone-theft incident: the Bharatiya Nyaya Sanhita section 303 with a model-written reading aid, verbatim statute text, and below it the cited PDF page with the quoted text highlighted in yellow. The “Informational, not legal advice” disclaimer and a Limits list are visible. (cropped to the result)

Original, unmodified screenshot · Esc to close · Open full size

THE HIGHLIGHTED SOURCE PDF

The complete result: model-written reading aid, verbatim statute text, the cited source page with the quotation highlighted, and the limits list.

Statute Finder India result for a phone-theft incident: the Bharatiya Nyaya Sanhita section 303 with a model-written reading aid, verbatim statute text, and below it the cited PDF page with the quoted text highlighted in yellow. The “Informational, not legal advice” disclaimer and a Limits list are visible. (cropped to the highlighted source page)

Original, unmodified screenshot · Esc to close · Open full size

One genuine capture, shown as two crops: the result with its verbatim statute text (left) and the cited PDF page with the quotation highlighted (right). Informational, not legal advice. Click either crop for the complete image.ORIGINAL CAPTURE / CROPPED VIEWS
41evaluation cases
102passing automated tests
57/57source quotations verified²

² In one documented local-model (Qwen 2.5 7B) run: every displayed quotation resolved in its source PDF. This is not legal accuracy. Informational, not legal advice.

01 / OVERVIEW

The model should not write the law.

Statute Finder India is a standalone legal information discovery prototype. A user describes an incident, optionally with a date, and the system retrieves candidate provisions from five statutes. The model only selects from those candidates and offers explanations; the application does the rest.

  1. The LLM selects opaque candidate IDs, never section numbers or quotations.
  2. The server removes invalid and repeated IDs, then resolves each citation from stored records.
  3. It re-reads the extracted PDF page text at stored character offsets and verifies the quotation.
  4. The interface shows the source PDF page with the quotation highlighted, and keeps model-written reading aids visibly separate from verbatim text.

READ THIS FIRST

This is informational, not legal advice, and no claim of legal accuracy is made. Verifying that a quotation exists in the source is a different thing from the answer being relevant or correctly interpreted.

The complete result: model-written reading aid, verbatim statute text, the cited source page with the quotation highlighted, and the limits list.

Statute Finder India result for a phone-theft incident: the Bharatiya Nyaya Sanhita section 303 with a model-written reading aid, verbatim statute text, and below it the cited PDF page with the quoted text highlighted in yellow. The “Informational, not legal advice” disclaimer and a Limits list are visible.

Original, unmodified screenshot · Esc to close · Open full size

The complete result: model-written reading aid, verbatim statute text, the cited source page with the quotation highlighted, and the limits list.CITATION + SOURCE · CLICK TO EXPAND

02 / THE PROBLEM

A plausible answer is not a checkable one.

A fluent legal answer can cite a section that does not exist, or quote text that is not in the statute. The question here is narrower: can a retrieval system connect an incident description to candidate provisions while preserving a path back to the original source that a reader can inspect?

The corpus covers the BNS, the Motor Vehicles Act, the IT Act, the Arms Act and the Tamil Nadu Harassment of Women Act. For incidents before 1 July 2024 the application routes through a historical IPC mapping table; it does not contain the original IPC corpus.

03 / ARCHITECTURE

Retrieval, selection, verification.

Indexing: PyMuPDF section-aware parsing, then source hash, page and character provenance, then chunking and BGE-small embeddings via fastembed, then PostgreSQL with pgvector. Query: exact cosine search and BM25, combined with per-act and global reciprocal rank fusion, then structured Pydantic output where the LLM selects candidate IDs, then deterministic validation, then quote verification against the source PDF, then display with a highlighted source page. FastAPI serves the application.

EXPLANATORY ILLUSTRATION · NOT A LIVE UI The model never writes the quotation, section reference or page number shown to the reader.

Quotes, citations, page numbers and source URLs come from stored database records. The validator rejects candidate IDs outside the retrieved set, and a source quotation can be re-read from its PDF offsets.

Incident input with an optional date, the date-routing note, and a result whose reading aid is explicitly labelled as model-written, not statute text. “Punishment: null” is a raw missing value visible in this capture.

Statute Finder India interface: an incident description, optional incident date, a date-routing note, and a result for BNS section 303 with a plain-words reading aid and verbatim source text.

Original, unmodified screenshot · Esc to close · Open full size

Incident input with an optional date, the date-routing note, and a result whose reading aid is explicitly labelled as model-written, not statute text. “Punishment: null” is a raw missing value visible in this capture.APPLICATION · CROPPED VIEW, CLICK FOR FULL IMAGE

04 / DECISIONS & TRADE-OFFS

Keep provenance out of the model.

Deterministic citation validation.

The model selects from retrieved records. It does not author the quotation, section reference or page number.

Trade-off. The model has less freedom: it cannot cite anything outside the retrieved candidates, even when it is right.

Exact search for a small corpus.

At this size an approximate index would add recall loss without a demonstrated speed benefit, so I used exact cosine search. No HNSW index is used.

Trade-off. Exact cosine does not scale to a much larger corpus the way an approximate index would.

Hybrid retrieval with rank fusion.

Vector search and BM25 are combined with per-act and global reciprocal rank fusion, because layperson wording often misses statutory phrasing.

Trade-off. Two retrievers and a fusion step are more to tune; BM25 parameters here are untuned defaults.

Make verification visible.

The application shows the cited PDF page with the quote highlighted so the reader can inspect the source instead of trusting the explanation.

Trade-off. Showing source pages adds interface weight, and a verified quote can still be irrelevant.

The same incident with a 2022 date (left, IPC routing through a comparison table that is not an official publication) and a 2025 date (right, BNS). The IPC text itself is not in the corpus.

Two captures side by side. Left: incident date 2022, routed to the IPC, showing an IPC-to-BNS comparison-table note and BNS section 303. Right: incident date 2025, routed to the BNS with no IPC equivalent shown.

Original, unmodified screenshot · Esc to close · Open full size

The same incident with a 2022 date (left, IPC routing through a comparison table that is not an official publication) and a 2025 date (right, BNS). The IPC text itself is not in the corpus.TEMPORAL ROUTING · CLICK TO EXPAND

Date routing is deterministic: incidents before 1 July 2024 are treated under the IPC, mapped through a comparison table that is not an official publication, and incidents from that date under the BNS.

05 / DEBUGGING

Where the pipeline actually broke.

  • Missing headings and overbroad footnote filtering in section-aware parsing silently dropped real sections.
  • Table-of-contents checks were needed so contents pages were not mistaken for statute text.
  • Weak text search was replaced with BM25 after full-text search failed on realistic queries.
  • Invalid or repeated candidate IDs from the model are removed by the server, with a visible notice.
  • Insufficient facts now produce a clarifying question with conditional candidates instead of a guess.

Insufficient facts produce a clarifying question with conditional candidates. A notice shows a repeated model-suggested reference (C2) being removed by the server.

For the incident “A person stabs someone with a knife during an argument and causes an injury” the interface asks how serious the injury was and shows provisional BNS section 118 candidates. A notice says one model-suggested reference was removed because it was repeated.

Original, unmodified screenshot · Esc to close · Open full size

Insufficient facts produce a clarifying question with conditional candidates. A notice shows a repeated model-suggested reference (C2) being removed by the server.CLARIFICATION · CROPPED VIEW, CLICK FOR FULL IMAGE

An out-of-scope incident (a security deposit dispute) ends with no matching provision rather than a forced answer.

For the incident “My landlord is refusing to return my security deposit” the interface shows “No matching provision shown. None of the retrieved provisions applies to the facts described.”

Original, unmodified screenshot · Esc to close · Open full size

An out-of-scope incident (a security deposit dispute) ends with no matching provision rather than a forced answer.ABSTENTION · CLICK TO EXPAND

06 / TESTING & EVALUATION

Verification is not legal accuracy.

The project has 41 AI-assisted evaluation cases (33 with an expected section) and 102 passing automated tests in the captured CI run.

Retrieval hit@3 over the 33 cases with an expected section
Modehit@3Note
Keyword (BM25)12/33-
Vector21/33-
Hybrid (default)22/33The shipped default
Hybrid + query rewrite26/33Experimental; tuned on the same data, so not held-out

In one local run with Qwen 2.5 7B, all 57 displayed quotations resolved in their source PDFs and no unretrieved ID was shown. It declined 7 of 8 out-of-scope cases and displayed the expected section in 21 of 33 cases. Only 26 of 56 displayed sections were labelled gold or acceptable.

WHAT THIS DOES NOT SHOW

The labels were drafted by an AI agent, are unreviewed by counsel, and come from a single non-deterministic run. The hosted model used by the public demo was not fully benchmarked. A real quotation can still be irrelevant or misapplied.

The evaluation report states its limits first: labels drafted by an AI agent, unreviewed by counsel, one run, not a measure of legal accuracy.

Evaluation report: 41 cases drafted by an AI agent and labelled unreviewed, not reviewed by counsel. Retrieval hit@3 over 33 cases: vector 21/33, keyword 12/33, hybrid 22/33, hybrid plus rewrite 26/33, with per-act and per-case-type breakdowns.

Original, unmodified screenshot · Esc to close · Open full size

The evaluation report states its limits first: labels drafted by an AI agent, unreviewed by counsel, one run, not a measure of legal accuracy.EVAL REPORT · CLICK TO EXPAND

A captured GitHub Actions run: 102 passed in 32.83s.

GitHub Actions log of a pytest run ending with 102 passed in 32.83 seconds.

Original, unmodified screenshot · Esc to close · Open full size

A captured GitHub Actions run: 102 passed in 32.83s.CI RUN · CROPPED VIEW, CLICK FOR FULL IMAGE

The FastAPI OpenAPI page: retrieve, answer, analyze, health and page-image endpoints.

FastAPI OpenAPI documentation for statute-finder-india listing POST /retrieve, POST /answer, POST /analyze, GET and HEAD /health, and GET /page-image.

Original, unmodified screenshot · Esc to close · Open full size

The FastAPI OpenAPI page: retrieve, answer, analyze, health and page-image endpoints.OPENAPI · CLICK TO EXPAND
Read the evaluation report and failure analysis

07 / DEPLOYMENT

Containerised, with the public demo kept off this page.

The repository includes Docker, Render and Neon configuration. The demo's current health has not been verified for this site, so it is not linked here; the source and this case study are the supported way to review the work.

Free-tier cold starts and model-provider rate limits would affect any hosted demo.

08 / LIMITATIONS

Explicit boundaries matter.

  • A valid quotation does not mean the section is relevant or the legal interpretation is correct.
  • Model-written summaries can reverse meaning even when the quoted source text is genuine.
  • The corpus is five statutes, not a legal database, with no automatic amendment tracking and incomplete historical source text.
  • Scanned PDFs are not handled, and retrieval remains sensitive to wording.
  • The interface can expose raw missing values, such as “Punishment: null” in the capture above.

09 / NEXT STEPS

What I would do next.

  • Make the source text primary and the generated reading aid secondary.
  • Improve section selection and cross-reference handling for penalty provisions.
  • Evaluate on a held-out set, with labels reviewed by a qualified lawyer, and benchmark the hosted model.
  • Present missing values clearly instead of showing raw nulls.

These are planned improvements; none is claimed as already fixed.

Read the source and documentation on GitHub

WORK WITH ME

Need something like this built?

I take on focused tasks as well as larger implementations. Tell me what you are trying to build.

Let's build something
that works.

HIRING AN ENGINEER?Discuss a role

Open to full-time engineering roles. Relocation across India is fine.

View résumé
HAVE A PROJECT?Discuss a project

Focused tasks or full builds. Tell me what you are trying to ship.

See what I can build

Draft an email

What is this about?

This opens a draft in your email app. Nothing is sent or stored from this page.