Skip to main content

Domain-structured RAG pipeline template

Project description

right-rag

PyPI CI

right-rag is a domain-structured RAG template for documents where plain text chunking loses important context: laws, policies, contracts, manuals, academic papers, handbooks, and other hierarchical corpora.

It is built around one rule: model the document structure first, then choose the retrieval method. Vector search, keyword search, SQL, graph retrieval, and custom domain lookup are all implementation choices, not framework assumptions.

Important: each project should adapt the spec/ plugins to its own documents, domain structure, retrieval behavior, answer style, and evaluation rules.

Why right-rag

Most RAG quality problems come from weak evidence modeling:

  • rules are separated from their exceptions
  • answers cannot be traced to exact source units
  • metadata exists but is not usable by retrieval
  • document versions and changes are flattened away
  • the index technology dictates the design

right-rag keeps the core generic and lets each project define what valid evidence means for its domain.

What It Helps With

Capability Why it matters
Structure preservation Keeps document hierarchy such as chapter, section, clause, rule, exception, table, and procedure step.
Evidence traceability Every unit and chunk can carry source spans, citation data, file path, page, heading path, and domain attributes.
Temporal and change tracking Unit state is replayable through changes, so evolving documents can be indexed incrementally instead of treated as unrelated blobs.
Domain-specific retrieval Retrievers work from saver artifacts, so a project can use vector search, SQL filters, graph traversal, exact lookup, or custom rules.
Evaluation workflow Query outputs can be checked against expected evidence, required answer text, forbidden text, and domain-specific evaluators.
Template-first customization New projects start from right-rag init and replace only the plugins that need domain knowledge.

Where It Fits

right-rag is useful when answers must be grounded in auditable document evidence:

  • policy assistants that must include exceptions and current policy versions
  • contract review tools that must cite exact clauses and party obligations
  • legal or regulatory research where section hierarchy controls meaning
  • technical manual assistants that must preserve ordered steps and warnings
  • academic paper search where methods, metrics, tables, and limitations matter
  • product knowledge bases that must distinguish current and legacy versions

Architecture

flowchart LR
    A[Source documents] --> B[Document loaders]
    B --> C[Unit parsers]
    C --> D[Semantic units]
    D --> E[Chunk parsers]
    E --> F[Retrieval-ready chunks]
    D --> G[Unit savers]
    F --> H[Chunk savers]
    G --> I[Unit artifacts]
    H --> J[Chunk artifacts]
    I --> K[Retrievers]
    J --> K
    K --> L[Answer generator]
    L --> M[Answer with evidence]
    M --> N[Evaluators and reports]
flowchart TD
    subgraph Index
      A[Load documents] --> B[Parse units] --> C[Parse chunks] --> D[Save artifacts]
    end

    subgraph Query
      E[User query] --> F[Retrieve from artifacts] --> G[Generate answer]
    end

    subgraph Evaluate
      H[Evaluation cases] --> E
      G --> I[Evaluate] --> J[Report]
    end

Install

pip install right-rag

Or run without a permanent install:

uvx --from right-rag right-rag --help

Requires Python 3.13+.

Quick Start

right-rag init my-rag
cd my-rag

Put documents in:

data/documents/

Run the pipeline:

right-rag index
right-rag query "your question"
right-rag evaluate

Generated local artifacts:

  • output/units/
  • output/chunks/
  • .right-rag-state.json
  • output/evaluation/

The packaged plugins are intentionally generic starting points. Each project should replace them with domain-specific plugins under spec/ so parsing, chunking, saving, retrieval, answer generation, and evaluation match the actual dataset. In practice, adapting right-rag means editing spec/ first, not changing the framework core.

Project Template

right-rag init creates:

my-rag/
  right_rag.toml
  data/documents/
  eval/cases.example.json
  output/
  spec/
    document_loaders/
    unit_parsers/
    chunk_parsers/
    unit_savers/
    chunk_savers/
    retrievers/
    answer_generators/
    evaluators/
    reporters/

Documentation

Extension Points

Each folder under spec/ is a project-owned customization point. Start with the document structure, then implement only the plugins your corpus needs.

Folder Purpose
spec/document_loaders/ Load source documents from files, APIs, stores, or custom extractors.
spec/unit_parsers/ Convert documents into a faithful ParsedUnit tree.
spec/chunk_parsers/ Convert semantic Unit objects into retrieval-ready Chunk records.
spec/unit_savers/ Persist units and return unit artifacts.
spec/chunk_savers/ Persist chunks and return chunk artifacts.
spec/retrievers/ Retrieve evidence from artifacts.
spec/answer_generators/ Generate answers from retrieved evidence.
spec/evaluators/ Score query outputs.
spec/reporters/ Write evaluation reports.

Domain Modeling

Start by inspecting the source documents. Choose units that match the author's real structure:

  • laws: act -> chapter -> section -> clause -> subclause
  • contracts: agreement -> article -> clause -> schedule item
  • policies: policy -> section -> rule -> exception
  • manuals: manual -> chapter -> procedure -> step
  • academic papers: paper -> section -> subsection -> paragraph/table/figure note

Good unit parsers:

  • preserve hierarchy with ParsedUnit.subunits
  • set source spans when possible
  • keep fulltext faithful to the source
  • store provenance in attributes or metadata
  • reject unsupported formats visibly
  • avoid catch-all parsers

Good chunk parsers:

  • chunk inside a single unit
  • keep rules with their exceptions and limitations
  • carry citation and filter attributes
  • prefer fewer useful chunks over many tiny fragments

Evaluation

right-rag evaluate eval/cases.example.json --output output/evaluation/report.md

The default evaluator is for smoke tests and regression checks. Real projects should add domain-specific evaluators under spec/evaluators/.

Development

git clone https://github.com/sunmodza/right-rag.git
cd right-rag
uv sync
python -m compileall src/right_rag
python -m unittest discover -s tests
uv build

Run plugin discovery:

uv run python -c "from right_rag.registry import build_registry; r=build_registry(); print(r.document_loaders, r.unit_parsers, r.chunk_parsers, r.unit_savers, r.chunk_savers)"

License

right-rag is released under the MIT License. See LICENSE.

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

right_rag-0.1.3.tar.gz (199.6 kB view details)

Uploaded Source

Built Distribution

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

right_rag-0.1.3-py3-none-any.whl (34.8 kB view details)

Uploaded Python 3

File details

Details for the file right_rag-0.1.3.tar.gz.

File metadata

  • Download URL: right_rag-0.1.3.tar.gz
  • Upload date:
  • Size: 199.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for right_rag-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c1fb291e8872b6ed731a2c707b92e1bce8844087ec1af4afc714605149436c33
MD5 c7c8d04f447ae9f307d60fff8b5f8b25
BLAKE2b-256 0db0ef3b1e7fde4092236add569618c57a0500174202d08d92e15b2845aa72e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for right_rag-0.1.3.tar.gz:

Publisher: publish.yml on sunmodza/right-rag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file right_rag-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: right_rag-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 34.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for right_rag-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 958bdc16b685214cafd9247e5ee99040a156a159b4aaffa1cda59e2ea2f2e1f3
MD5 4d649ea55627ff3a3af73e97cdf8c665
BLAKE2b-256 79b62f771f190670af2c2ecf751b5645a8a60513607f097d10edebf1cc28d40d

See more details on using hashes here.

Provenance

The following attestation bundles were made for right_rag-0.1.3-py3-none-any.whl:

Publisher: publish.yml on sunmodza/right-rag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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