Skip to content

Latest commit

 

History

History
442 lines (333 loc) · 44 KB

AGENTS.md

File metadata and controls

442 lines (333 loc) · 44 KB

Repository Guidelines

Project Overview

LightRAG is a Retrieval-Augmented Generation (RAG) framework that uses graph-based knowledge representation for enhanced information retrieval. The system extracts entities and relationships from documents, builds a knowledge graph, and uses multiple retrieval modes (local, global, hybrid, mix, naive) for queries.

Project Structure

Top-level directories:

  • lightrag/: Core Python package — see Module Layout below.
  • lightrag_webui/: React 19 + TypeScript client (Bun + Vite + Tailwind). UI components in src/.
  • scripts/: test.sh (preferred test runner), setup/ interactive environment wizard (use make env-* rather than calling setup.sh directly — see Configuration > Setup Wizard Outputs), and release tooling.
  • tests/: Pytest coverage, organized into subdirectories that mirror lightrag/ (see Testing below for layout). Working datasets stay in inputs/, rag_storage/, and temp/; deployment collateral lives in docs/, k8s-deploy/, and compose files.

Module Layout (lightrag/)

  • lightrag.py: Main orchestrator class (LightRAG) — assembled from mixins (see LightRAG class composition). Hosts ainsert_custom_kg, _insert_done, _process_extract_entities, _refresh_addon_params_cache, and addon_params accessors. Critical: always call await rag.initialize_storages() after instantiation.
  • pipeline.py: _PipelineMixin — owns the document ingestion pipeline (apipeline_enqueue_documents, apipeline_process_enqueue_documents, apipeline_process_error_documents), the parse_native / parse_mineru / parse_docling parser dispatchers, multimodal analysis, validation, and the worker scaffolding.
  • utils_pipeline.py: Pure helpers shared by the pipeline mixin and other entry points: doc-status field access, document identity (source key, content hash), parsed-artifact path resolution, parser payload normalization, multimodal entity augmentation, and make_lightrag_doc_content.
  • llm_roles.py: RoleSpec / RoleLLMConfig / _RoleLLMState / ROLES registry plus _RoleLLMMixin — role normalization, builder registration, wrapper rebuild, runtime config update, queue cleanup, sanitized config export, queue status reporting. Route role-specific behavior here rather than into provider modules.
  • storage_migrations.py: _StorageMigrationMixin — check_and_migrate_data, _migrate_entity_relation_data, _migrate_chunk_tracking_storage.
  • addon_params.py: ObservableAddonParams plus default_addon_params / normalize_addon_params helpers.
  • operate.py: Core extraction and query operations including entity/relation extraction, chunking, and multi-mode retrieval logic.
  • base.py: Abstract base classes for storage backends (BaseKVStorage, BaseVectorStorage, BaseGraphStorage, BaseDocStatusStorage).
  • kg/: Storage implementations (JSON, NetworkX, Neo4j, PostgreSQL, MongoDB, Redis, Milvus, Qdrant, Faiss, Memgraph, OpenSearch, NanoVectorDB). The backend registry (STORAGE_IMPLEMENTATIONS / STORAGES) lives in kg/__init__.py; kg/factory.py::get_storage_class() resolves backend classes from configuration.
  • llm/: LLM and embedding provider bindings (OpenAI, Ollama, Azure, Gemini, Bedrock, Anthropic, etc.). All async with caching support.
  • parser/: Unified parsing layer. parser/routing.py resolves engine and filename hints for legacy, native, mineru, and docling flows; parser/debug.py provides an offline LightRAG stub for the parser/cli.py debug entry point (python -m lightrag.parser.cli). Native format parsers live as sibling sub-packages under parser/ (currently parser/docx/); external HTTP-based adapters live under parser/external/ (mineru, docling) with shared helpers in parser/external/_common.py, _manifest.py, _zip.py.
  • chunker/: Chunking strategies (token-size, recursive character, semantic vector, paragraph semantic).
  • api/: FastAPI service (lightrag_server.py) with REST endpoints and Ollama-compatible API; routers under routers/, static Swagger assets, packaged WebUI output, and Gunicorn launcher.

Core Architecture

LightRAG class composition

LightRAG is assembled from focused mixins (split out of the previously monolithic lightrag.py):

LightRAG → _RoleLLMMixin → _StorageMigrationMixin → _PipelineMixin → object

The @final decorator on LightRAG is preserved — the mixin layering is an internal implementation detail, not an external subclassing surface. The public API (ainsert, aquery, ainsert_custom_kg, initialize_storages, etc.) is unchanged. ainsert_custom_kg and its internal construction logic, _insert_done, _process_extract_entities, _refresh_addon_params_cache, and the addon_params property accessors stay on LightRAG itself because they cut across multiple flows or depend on prompt-profile state.

Storage Layer

LightRAG uses 4 storage types with pluggable backends:

  • KV_STORAGE: LLM response cache, text chunks, document info
  • VECTOR_STORAGE: Entity/relation/chunk embeddings
  • GRAPH_STORAGE: Entity-relation graph structure
  • DOC_STATUS_STORAGE: Document processing status tracking

Each LightRAG instance can pass a workspace parameter for data isolation. Implementation differs per storage type:

  • File-based: subdirectories under working_dir.
  • Collection-based: collection name prefixes.
  • Relational DB: workspace column filtering.
  • Qdrant: payload-based partitioning.

Consistency without transactions

LightRAG writes to independent stores — graph, KV, vector, doc-status — with no transaction across them. Every multi-store operation therefore has intermediate states, and no ordering removes them; an ordering only chooses which one it keeps.

  • The rule: an inconsistency is acceptable when it heals itself later (a retry, a rebuild, or the next run rewrites it) or is harmless in direction. Losing data is never acceptable; retaining an object that could have been deleted, or surfacing a chunk a query did not need, is.
  • A change must improve on an accepted residue, not swap it for its mirror. "An inconsistent state exists" is not by itself a defect report — the questions are which state, how it heals, and whether the alternative is better.
  • Every accepted residue is written down with its reason and recovery path, next to the code or in the relevant contract. An undocumented residue is a defect; a documented one is a decision.
  • This licenses nothing for silent failure. A durable write must never be reported as one that did not happen, and a failure must never be swallowed: fail loud, then let the documented residue heal.

File-backed storage contracts

Full contracts: docs/design/NetworkXSingleWriterContract.md — read it before touching lightrag/kg/networkx_impl.py or any caller of index_done_callback on the graph store; docs/design/FileBackedSnapshotContract.md — read it before touching lightrag/kg/nano_vector_db_impl.py, lightrag/kg/faiss_impl.py, lightrag/kg/json_kv_impl.py, lightrag/kg/json_doc_status_impl.py or lightrag/kg/file_fingerprint.py.

Five storages keep their data in memory and publish it by rewriting a whole file. They are supported for small-scale testing and validation only (no change may be justified by write throughput), and they do not share one model — read the right contract before assuming:

  • NetworkXStorage, NanoVectorDBStorage, FaissVectorDBStorage: one in-memory copy per process, reconciled by reloading the file through the two-channel fence (file fingerprint OR storage_updated). On a write conflict the graph store declines the commit, the vector stores reload and replay; do not reopen reload-then-replay for the graph store without addressing the accumulate-over-read argument.
  • JsonKVStorage, JsonDocStatusStorage: one Manager().dict() shared by every worker, so there is nothing to reload and storage_updated means the OPPOSITE ("dirty, still to flush"). Their load claim (namespace_init_claim) must be handed back on a failed or cancelled load, and JsonDocStatusStorage reimplements the protocol rather than inheriting it — a change to one of the pair almost always needs the other.
  • A file-backed configuration storage additionally claims its workspace's snapshot directory (lightrag/kg/working_dir_lock.py) — see Configuration storage contract below. NetworkXStorage alone declares requires_single_writer, which puts the admin flows under LightRAG._admin_write_gate (so does chunk tracking on OpenSearchKVStorage, for a different reason: see Pipeline concurrency contract).

Server instance contract

Full contract: docs/design/ServerInstanceContract.md — read it before changing anything one server could share with another on the same host (files under WORKING_DIR / INPUT_DIR, anything keyed without the workspace, locks taken at startup). In short: several servers may share one WORKING_DIR on one host when each serves its own workspace, all use the same configuration container (a database backend, or JsonKVStorage's per-workspace snapshots under one anchor), and each has its own port; first starts are serialized by the anchor bind lock, so on a filesystem without locks start one instance alone until every JSON workspace has registered. A new server-wide record needs a guard the instances share.

Pipeline concurrency contract

Full contract: docs/design/PipelineConcurrencyContract.md — read it before touching lightrag/pipeline.py, lightrag/kg/pipeline_ingress.py, pipeline_status fields, or any /documents/* endpoint.

  • Concurrent writers coordinate through pipeline_status (per-workspace shared dict in lightrag.kg.shared_storage), mutated under get_namespace_lock("pipeline_status", workspace=...).
  • busy alone does NOT block enqueue — enqueue + processing are allowed to run concurrently. Three states do refuse it: destructive_busy (clear / delete, which drops storages), scanning_exclusive (scan's classification phase), and manual_freeze_requested (a manual retry draining the pipeline to idle).
  • Admin graph writes (acreate_* / aedit_* / adelete_by_* / amerge_entities / ainsert_custom_kg) run inside LightRAG._admin_write_gate when the graph storage declares requires_single_writer (NetworkXStorage only) or a chunk-tracking KV storage declares buffers_writes_per_process (OpenSearchKVStorage only): a workspace admin lock (waited for) then the busy reservation (kind="admin", refuses on busy / scanning), in that fixed order and OUTSIDE the per-entity keyed locks. A pipeline start during the hold is deferred into the ingress mailbox and driven once on release. The routes' check_pipeline_busy_or_raise preflight exempts an admin-owned busy so a second REST edit reaches the admin lock and queues. Never re-acquire the admin lock inside lightrag/utils_graph.py.
  • The workspace ingress mailbox (get_pipeline_ingress(workspace)) is the pipeline's only wake-up channel; doc_status stays the source of truth, so a dropped notification is recovered by the next strict scan.
  • FAILED documents never resume automatically: they re-enter only through a sticky manual retry request (/documents/scan, /documents/reprocess_failed), granting ONE attempt each.
  • All scheduling-control-plane doc_status queries use get_docs_by_statuses(..., strict=True); scheduler full_docs reads must distinguish confirmed-absent (None) from backend errors (raise).

Purge recovery contract

Full contract: docs/design/PurgeRecoveryContract.md — read it before touching _purge_kg_contributions, adelete_by_doc_id, the anchor writes in merge_nodes_and_edges, the kg_write_state / kg_purge metadata, or the cache write ordering in use_llm_func_with_cache.

  • "What did this document contribute?" is answerable only from the per-document write-ahead anchors (full_entities / full_relations). The reverse lookup through text_chunks is not a fallback — purge deletes those chunks.
  • Governing invariant: a purge must never delete something that CARRIES attribution — a chunk row or an anchor row that names objects — and leave those objects behind. _purge_kg_contributions fails closed (RecoveryAnchorMissingError → HTTP 409, nothing deleted) unless one of four proofs holds: anchors, pre_graph, journal, empty_scope.
  • kg_write_state must never be inferred or backfilled — it is written once at enqueue and is monotonic. A backfill reproduces the original silent-skip defect.
  • kg_write_state and kg_purge must stay in both _DOC_STATUS_METADATA_CARRY_OVER_KEYS and _DOC_STATUS_METADATA_DIRECTIVE_KEYS (lightrag/utils_pipeline.py); dropping either turns a resumable purge into a permanent refusal.
  • Chunk tracking (entity_chunks / relation_chunks) outranks graph source_id; code folding a source_id delta back into tracking must append genuine additions only.
  • LLM extraction cache rows are reachable only through the owning chunk's llm_cache_list, which makes that list an attribution carrier too: LLM extraction cache reachability states the reference-before-row ordering, why a reference that cannot be recorded skips the cache write instead, and what the ordering does not close.
  • Merge and rename apply Consistency without transactions above: the failure model lists their ordering invariants, accepted residues and already-rejected remedies. Read it before reordering _merge_entities_impl or the rename branch of _edit_entity_impl.
  • The deletion rebuild attributes a cache row through llm_cache_list, never the row's chunk_id (rows are shared across documents), and re-extracts a surviving chunk whose initial extraction is missing or unparseable (a lost gleaning row is accepted; ainsert_custom_kg chunks are never re-extracted). An object no surviving record names is kept with repaired provenance, never removed. Relations rebuild before entities, and any per-object failure fails the deletion (retryable). Deletion rebuild evidence has the evidence table, the residues and the operator recovery for name drift.
  • A change to what normalize_entity_name produces may only merge names that used to differ, never split names that used to be equal: a rebuild re-parses cached LLM output with the current rules, and a split leaves some of a stored object's records unmatched, so their facts are dropped. Entity name normalization changes has the reasoning and the consequences in the current rules.

Configuration storage contract

Full contract: docs/design/ConfigurationStorageContract.md — read it before touching lightrag/config_store.py, lightrag/config_anchor.py, lightrag/config_shards.py, lightrag/kg/anchor_lock.py, lightrag/kg/working_dir_lock.py, LightRAG.initialize_storages / finalize_storages, the config KV namespace on any backend, config_storage or where the JSON configuration snapshots live, the baseline writes in lightrag/tools/rebuild_vdb.py, lightrag/tools/migrate_config.py, or the configuration cleanup in /documents/clear. Its Rules at a glance section is the summary; do not restate it here. docs/design/ConfigurationStorageDesign.md is the design overview: the components, the reasons behind them, the departures from the original plan and the known gaps -- start there before redesigning anything.

Relation weight contract

Full contract: docs/ProgramingWithCore.md — keep it synchronized with the core API docstrings, REST graph documentation, and custom-KG examples whenever relation write behavior changes.

  • weight >= len(distinct real source IDs) on the graph edge; a larger value is an optional importance boost. Empty IDs and the legacy placeholders manual_creation / UNKNOWN are not evidence, so a source-less relation may use any non-negative fractional weight.
  • Public ingress paths (create_relation, edit_relation, insert_custom_kg) must validate the complete relation before the first storage mutation. To go below the current evidence count, creation callers omit source_id, edit callers set it to an empty string in the same operation.
  • Entity merges use max(all input weights, distinct merged real source IDs).
  • Legacy-row repair is opportunistic, not a sweep: _merge_edges_then_upsert's KEEP-cap skip branch returns the stored edge without writing graph or vector record, so an undersized legacy row stays undersized until a merge, an unrelated edit, or a rebuild rewrites it.

Query Modes

  • local: Context-dependent retrieval focused on specific entities
  • global: Community/summary-based broad knowledge retrieval
  • hybrid: Combines local and global
  • naive: Direct vector search without graph
  • mix: Integrates KG and vector retrieval (recommended with reranker)

Development Commands

Setup

# Install with uv
uv sync
source .venv/bin/activate  # Or: .venv\Scripts\activate on Windows

# Install with API support
uv sync --extra api

# Install specific extras
uv sync --extra offline-storage  # Storage backends
uv sync --extra offline-llm      # LLM providers
uv sync --extra test             # Testing dependencies

API Server

# Copy and configure environment
cp env.example .env  # Edit with your LLM/embedding configs

# Build WebUI
cd lightrag_webui
bun install --frozen-lockfile
bun run build
cd ..

# Run server
lightrag-server                                           # Production
uvicorn lightrag.api.lightrag_server:app --reload        # Development
lightrag-gunicorn                                         # Multi-worker (gunicorn)

WebUI

Every command below is run from lightrag_webui/, not the repository root. bun test in particular resolves bunfig.toml — and the preload paths inside it — relative to the working directory, so running it from the root silently loads no DOM (see React component tests).

cd lightrag_webui
bun install --frozen-lockfile      # REQUIRED after any change to package.json /
                                   # bun.lock, including a branch switch across
                                   # one: `git checkout` does not update
                                   # node_modules, and a stale tree surfaces as
                                   # a wall of TS2307 "Cannot find module".
bun run dev                        # Dev server (Node + Vite)
bun run dev:bun                    # Dev server (Bun native)
bun run build                      # Production build
bun run preview                    # Preview production build
bun run lint                       # ESLint over *.ts/tsx/js/jsx

# Testing — Bun built-in runner (NOT Vitest/Jest)
bun test                           # All tests
bun test --watch                   # Watch mode
bun test --coverage                # With coverage report
bun test src/api/lightrag.test.ts  # Single test file
bunx tsc --noEmit                  # Typecheck (`bun run build` does NOT typecheck)

Testing

  • Use mock-based tests for external services (Redis, httpx, etc.) — do not depend on live services in unit tests.
  • Every bug fix needs a regression pin. Put it in the existing test of that path when one exists (an assertion, or a parametrize case) and write a new test only when none does; see Extend before you add under Writing backend tests.
  • Run only the test directories that mirror the modules you changed, and report which subset you ran plus its pass count. The suite is ~13000 offline tests: about 5 minutes serially, ~70 s with -n auto on an 8-core machine — still too slow for the edit loop. Every PR's CI runs the full offline suite (plus tests/setup/ and, with the real RAGAS stack, tests/evaluation/ when their inputs change) — proving nothing else broke is its job, not yours.
  • Derive the subset from the mirror layout below: lightrag/api/config.py → tests/api/config/, lightrag/kg/redis_impl.py → tests/kg/redis_impl/, lightrag/chunker/ → tests/chunker/. When a change spans several modules, run each of their directories rather than widening to tests/.
  • tests/setup/ is the setup wizard's suite and runs only when a wizard input changed: scripts/setup/**, env.example, docker-compose*.yml, Makefile, or tests/setup/ itself. The only lightrag modules it imports are the configuration-storage ones its differential tests compare the wizard against (config_anchor, config_shards, config_store, exceptions, kg/__init__.py); a change to one of those runs it too, and any other lightrag/ change never needs it. norecursedirs in pyproject.toml already skips it in every recursive run (pytest, pytest tests, the full suite, CI's main job); run it by naming it: ./scripts/test.sh tests/setup. The easy one to miss is env.example: adding a config knob for lightrag usually edits it, and the wizard parses it, so that change runs tests/setup/ too.
  • Run the full suite locally only at a milestone. A cross-cutting change (lightrag/base.py, lightrag/utils.py, lightrag/kg/shared_storage.py, or anything every backend inherits) is no exception: run the mirror directories plus those of every test that imports or patches the changed names, and leave the rest to CI. Run it with -n auto --dist worksteal (pytest-xdist, in the pytest extra): on an 8-core machine it cuts ~5 min to ~1 min. CI does not use -n because hosted 4-vCPU runners showed no gain; it splits the suite by directory into parallel shard jobs instead (.github/workflows/tests.yml, where the shard lists live). Do not add -n to subset runs: every worker re-collects and re-imports the suite (~12 s), which costs more than a single directory saves. Drop -n when you need pdb/-s or ordered output.
  • Backend tests use pytest; frontend unit tests use Bun's built-in runner — see WebUI above and React component tests below.
  • Capturing lightrag logs in tests: the lightrag logger sets propagate = False, and pytest < 9.1 attaches caplog's handler to the root logger only, so a bare caplog captures nothing from it there. The supported floor is pytest>=8.4.2, and the lock file's newer pytest hides the failure. Use the lightrag_caplog fixture (tests/conftest.py) when you need caplog's API, lightrag_log_records when you only count or inspect records, or capture_lightrag_logs (tests/_log_capture.py) inside a helper that receives caplog. Never set propagate = True in a test: on pytest >= 9.1 every record is then captured twice.
  • A WebUI change runs the WHOLE frontend check set, from lightrag_webui/: bun install --frozen-lockfile (see WebUI above — skip it after a branch switch and every later step fails on missing modules), then bun test, bunx tsc --noEmit, and bun run lint. The subsetting rule above is a backend rule and does not apply — all three together take well under a minute (test ~2 s, typecheck ~14 s, lint ~21 s), so there is nothing to save by running less. Report the pass count. bun run build transpiles WITHOUT checking types, so skipping tsc --noEmit means nothing checks them.
# Preferred for fresh shells and automation; resolves PYTHON, venv, uv, .venv, venv, python, python3
# Default during development: only the directories mirroring the changed modules
./scripts/test.sh tests/api/config
./scripts/test.sh tests/kg/redis_impl

# Run specific test file
./scripts/test.sh tests/kg/test_graph_storage.py

# Full suite — ~13000 offline tests; milestones only.
# Parallel via pytest-xdist: ~70 s on 8 cores (vs ~5 min serial).
# tests/setup is skipped by default (norecursedirs); name it explicitly when
# scripts/setup/, env.example, docker-compose*.yml or Makefile change.
./scripts/test.sh tests -m offline -n auto --dist worksteal
./scripts/test.sh tests/setup -m offline -n auto --dist worksteal

# Integration-test concurrency (a LightRAG fixture, NOT pytest parallelism)
./scripts/test.sh tests --test-workers 4
  • tests/: main test suite, mirrors feature folders. Place new tests under the subdirectory matching the module under test:
    • tests/api/{auth,config,routes}/ for FastAPI server tests (auth/token, config loading, route handlers); top-level tests/api/ for app-wide concerns (path prefixes, Ollama-compatible endpoint).
    • tests/chunker/, tests/evaluation/, tests/extraction/ for the like-named modules.
    • tests/operate/ for lightrag/operate.py (query context building, chunk selection, truncation, answer post-processing), and tests/utils/ for lightrag/utils.py.
    • tests/kg/<backend>_impl/ for backend-specific storage tests, mirroring the lightrag/kg/<backend>_impl.py file naming. The _impl suffix on every subdirectory keeps the layout uniform and avoids sys.path shadowing on names that overlap with top-level PyPI/stdlib packages (faiss, json, neo4j, networkx, redis) when a test is launched directly via python tests/kg/.... Current backends: faiss_impl/, json_impl/, memgraph_impl/, milvus_impl/, mongo_impl/, nano_impl/, neo4j_impl/, networkx_impl/, opensearch_impl/, postgres_impl/, qdrant_impl/, redis_impl/. tests/kg/ root holds cross-backend tests (test_graph_storage, test_batch_graph_operations, test_unified_lock_safety, test_file_atomic).
    • tests/llm/<provider>_impl/ for provider-specific behavior, same _impl convention: bedrock_impl/, gemini_impl/, ollama_impl/, openai_impl/, voyageai_impl/, zhipu_impl/. tests/llm/ root holds cross-provider concerns (embedding, rerank, VLM, cache, role).
    • tests/parser/, tests/parser/docx/, tests/parser/external/{mineru,docling}/ for parser implementations.
    • tests/pipeline/ for ingestion pipeline and doc-status behavior (including test_pipeline_*, test_doc_status_*, test_multimodal_*, test_graph_keyed_locks).
    • tests/sidecar/, tests/setup/, tests/workspace/ for the like-named cross-cutting concerns. tests/setup/ imports only the lightrag modules named above; it runs in its own path-filtered CI job (.github/workflows/setup-tests.yml) and norecursedirs keeps it out of every recursive run, so a new input the wizard reads must be added to that workflow's paths.
    • When adding a new backend or LLM provider, create a new subdirectory plus an empty __init__.py rather than dropping the file in the parent directory root.
    • tests/ root keeps the repository-wide checks (test_readme_translations, test_docstring_budget, test_requirements_sync, test_no_return_in_finally, test_dotenv_isolation), the tests of test helpers (test_sleep_patch, test_log_capture), and tests of single-file modules with no mirror directory yet (base, prompt, query_validation, private_file, table_markup, tiktoken_cache, storage_migrations, and LightRAG / QueryParam configuration). A module that gains a mirror directory takes its root tests with it.
  • Markers (registered in [tool.pytest.ini_options] in pyproject.toml): offline, integration, requires_db, requires_api, pg_smoke. Integration tests are skipped by default via -m "not integration"; opt in with --run-integration.
  • A path-filtered workflow lists every module its tests pin, not only the obvious one: a job whose paths omit a module its tests import and exercise never runs on a change to it, and if the main job skips those tests too (tests/setup/ via norecursedirs) nothing does. Add the module to paths in the same PR that makes a filtered test depend on it.
  • Every test must carry one of those five markers, usually a module-level pytestmark = pytest.mark.offline. CI selects with -m offline, so an unmarked test is deselected without a trace; tests/conftest.py therefore fails any unmarked test at setup with the fix in the message. In a file mixing offline and service-backed cases, mark per test or per pytest.param instead of per module.
  • Integration env vars: LIGHTRAG_RUN_INTEGRATION=true, LIGHTRAG_KEEP_ARTIFACTS=true, LIGHTRAG_TEST_WORKERS=4, plus storage-specific connection strings.

Writing backend tests

Full guide: docs/design/BackendTestingGuide.md — read it before writing or refactoring a test under tests/.

  • Extend before you add. Before writing a new test function, find the tests that already call the code you changed (its mirror directory, plus a search for the function's name). If one already runs your scenario, add your assertion to it; if one drives the same function with other inputs, add a parametrize case; if a fake or helper already builds your input, reuse it. Write a new test function only when none of these fits, and say in the PR which existing test you considered. A fix to a function that already has tests rarely needs one. See Every test must earn its place.
  • Every test asserts an observable outcome, including what a no-op leaves unchanged. Pin only what a docstring, contract or sibling test promises, and prove that the setup reaches the path under test (an injected fault actually fired, a stubbed prompt selected the branch).
  • Inputs must tell the right answer from the wrong one: expect a value no default, preset or stub produces, and give sibling fields different values, so a restore-vs-default or copy-vs-swap regression goes red. Import-time behavior needs a fresh import; a subprocess needs PYTHONPATH set to the checkout under test.
  • Expected exceptions go through pytest.raises, never try/except. Never wrap the call under test in except Exception: pass.
  • A fake that must never be called records the call as well as raising: a broad except Exception in the code under test swallows AssertionError.
  • Concurrent workers must surface their exceptions (ThreadPoolExecutor.map, future.result(), asyncio.gather/TaskGroup), with max_workers at least the Barrier parties and a Barrier timeout.
  • Cases are @pytest.mark.parametrize, not a for loop, unless the repetition is the behavior. An exhaustive value sweep qualifies only under the three conditions in Failures name the case (independent oracle, millisecond loop, collected mismatch list), and branch-selecting dimensions stay parametrized. Reduce large values before comparing them so the failure names the case.
  • Every test must earn its place in a ~13000-test suite: it pins a behavior no existing test pins, tests that share a scenario and differ only in what they assert are one test, and nothing waits on real time, spawns processes or builds a full LightRAG where a stub does. See Every test must earn its place.
  • Removing or merging a test must leave a kept test that goes red on the same break, named in the PR's inventory. Each data-table entry (allow-list member, regex alternative) is its own branch; a trimmed sweep keeps its strongest assertion; a merge keeps the more discriminating input; what nothing pins any more is stated in the PR. See Removing or merging a test.

React component tests

Full guide: docs/design/WebUITestingGuide.md — read it before writing or converting any test under lightrag_webui/src/.

  • Tests are colocated next to the module they cover (src/features/SiteHeader.test.ts); the tests/ mirror layout above is a backend rule. A test file containing JSX must be named .test.tsx.
  • Run bun test from lightrag_webui/. The bunfig.toml preload that installs the DOM is resolved against the CWD, and from the repository root the failure is silent — pure logic tests still pass, only component tests break.
  • Test rendered behavior by rendering it, through renderWithProviders (src/test/render.tsx). Do not write new tests that readFileSync a .tsx and match substrings.
  • Never assert toBeNull() / not.toBeInTheDocument() / toBe(element) on a DOM node — Bun serialises the whole happy-dom element and the run appears to HANG instead of failing. Assert a count or an extracted boolean/string instead.
  • The DOM is process-wide: never leave a bare delete of the window / document global behind; use withoutDomGlobals() from src/test/domGlobals.ts.
  • Never import the test harness from production code — one import from src/ ships happy-dom to the browser.
  • Prove the test can fail: break the behavior it pins, see it go red, then restore. Seed non-default and unequal values so the break is visible, and import a module under test for import-time behavior as a fresh instance.
  • The backend's earn its place and removing or merging rules apply to WebUI tests too: one rendered scenario per test, one case per branch, the smallest tree that shows the behavior.

Linting

ruff check .

Key Implementation Patterns

LightRAG Initialization (Critical)

The most common error is forgetting to initialize storages (manifests as AttributeError: __aenter__ or KeyError: 'history_messages'):

import asyncio
from lightrag import LightRAG
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed

async def main():
    rag = LightRAG(
        working_dir="./rag_storage",
        llm_model_func=gpt_4o_mini_complete,
        embedding_func=openai_embed
    )

    # REQUIRED: Initialize storage backends
    await rag.initialize_storages()

    # Now safe to use
    await rag.ainsert("Your text here")
    result = await rag.aquery("Your question", param=QueryParam(mode="hybrid"))

    # Cleanup
    await rag.finalize_storages()

asyncio.run(main())

Custom Embedding Functions

Full guide: docs/ProgramingWithCore.md — the .func unwrapping rule, max_token_size, the (len(texts), embedding_dim) return contract that EmbeddingFunc.__call__ enforces on every call, and the clear-the-data-directory pitfall when switching models. Read it before writing or wrapping an embedding function.

Storage Configuration

Configure via environment variables or constructor params:

# Environment-based (recommended for production)
# See env.example for full list

# Constructor-based
rag = LightRAG(
    working_dir="./storage",
    workspace="project_name",  # For data isolation
    kv_storage="PGKVStorage",
    vector_storage="PGVectorStorage",
    graph_storage="Neo4JStorage",
    doc_status_storage="PGDocStatusStorage",
    vector_db_storage_cls_kwargs={
        "cosine_better_than_threshold": 0.2
    }
)

Document Insertion

# Single document
await rag.ainsert("Text content")

# Batch insertion
await rag.ainsert(["Text 1", "Text 2", ...])

# With custom IDs
await rag.ainsert("Text", ids=["doc-123"])

# With file paths (for citation)
await rag.ainsert(["Text 1", "Text 2"], file_paths=["doc1.pdf", "doc2.pdf"])

# Configure batch size
rag = LightRAG(..., max_parallel_insert=4)  # Default: 3, max recommended: 10

Query Configuration

from lightrag import QueryParam

result = await rag.aquery(
    "Your question",
    param=QueryParam(
        mode="mix",                    # Recommended with reranker
        top_k=60,                      # KG entities/relations to retrieve
        chunk_top_k=20,                # Text chunks to retrieve
        max_entity_tokens=6000,
        max_relation_tokens=8000,
        max_total_tokens=30000,
        enable_rerank=True,
        user_prompt="Additional instructions for LLM",
        stream=False
    )
)

Frontend Debugging via Playwright

For WebUI bugs whose symptoms only surface in the rendered DOM — layout/overflow/scrollbar issues, transient flashes, third-party libraries attaching helpers to <body> outside React's tree, or end-to-end verification of a fix — drive the running dev server (http://localhost:5173) with the document-skills:webapp-testing skill instead of reasoning from source alone. Seed state directly via localStorage (persist key settings-storage, schema in lightrag_webui/src/stores/settings.ts) to skip live LLM calls. Use wait_until="domcontentloaded" plus a selector wait — Vite dev's long-lived polling makes networkidle time out.

Configuration

.env Configuration

Primary configuration file for API server. Generate it with make env-base or copy env.example manually. Key sections:

  • Server settings (HOST, PORT, CORS)
  • Storage backends (connection strings via environment variables)
  • Query parameters (TOP_K, MAX_TOTAL_TOKENS, etc.)
  • Reranking configuration (RERANK_BINDING, RERANK_MODEL)
  • Authentication (AUTH_ACCOUNTS, LIGHTRAG_API_KEY)

See env.example for comprehensive template.

Setup Wizard Outputs

  • Keep .env host-usable. Container-only hostnames and staged SSL paths belong in the wizard-managed compose layer, not persisted back into .env.
  • Treat docker-compose.final.yml as generated output assembled from scripts/setup/templates/*.yml.
  • For setup workflow changes, prefer make env-* targets over direct scripts/setup/setup.sh calls.

Code Style

Language

Comments, backend code, log messages, and Git commit messages in English. Frontend uses i18next for multi-language support.

README translations

README.md has three translations: README-zh.md, README-ja.md and README-id.md. All four are kept line-for-line aligned:

  • A change to any README goes into all four in the same commit. Never leave a translation for later. Changes that reached README.md alone are how the translations drifted before: they were missing a banner, a documentation table row, renamed env vars and whole paragraphs, and their line counts had drifted apart.
  • Same line count, same structure. Line N plays the same role in every file: a heading of the same level, a list item, a table row, a code fence, an HTML line, or a blank line. Keep the English line breaks. A two-line code comment stays two lines, and a bullet list does not become a numbered one.
  • Translate the prose only. Code, commands, env var names, file paths and anchors stay identical. Link targets do too, except that README-zh.md may point at a *-zh.md translation of the linked doc.
  • Enforced by tests/test_readme_translations.py, which is offline and runs in CI. It fails on a line-count mismatch and on the first line whose kind differs, and names that line in all files. Run it after any README edit: ./scripts/test.sh tests/test_readme_translations.py. A new translation must be added to its test_translations_exist list and to this section.

Docstrings and comments

Docstrings state the rules: what a caller must do, what it must not do, and the gotchas it will otherwise be caught by. The mechanism — how it works, the accepted residues, and the alternatives already rejected — goes in docs/design/ with a pointer from the docstring. A docstring that has grown into a design document is the thing this separates: it buries the code, and the same facts in two places drift apart.

Two rules are enforced by tests/test_docstring_budget.py rather than by review, because both are properties of the tree rather than of any one change:

  • No docstring over 80 lines (100 for a class). The limit is generous on purpose — it catches a document, not a thorough docstring.
  • No source line may cite a GitHub issue number. The referent does not survive a fork, so a comment saying something is "documented in #NNNN" leaves nothing that documents it. Name the thing instead ("the two-channel fence"), or move the content into docs/design/ and cite that. Never delete such a reference bare — migrate what it pointed at first. Enforced over lightrag/ only: tests may cite issue numbers, and many do. There the number names the defect the test pins, and the test itself — its name, docstring and assertions — is the documentation, so the reference is provenance rather than the thing carrying the meaning.

A third test requires every docs/**.md path named in the package to resolve; nothing imports those strings, so a typo is otherwise silent.

Python

  • Follow PEP 8 with 4-space indentation
  • Use type annotations
  • Prefer dataclasses for state management
  • Use lightrag.utils.logger instead of print
  • Async/await patterns throughout

TypeScript / React (incl. WebUI ESLint)

  • Functional components with hooks; PascalCase for components
  • 2-space indentation, single quotes (enforced by @stylistic rules)
  • Tailwind utility-first styling
  • ESLint stack: TypeScript-ESLint + React Hooks plugin + Prettier; @typescript-eslint/no-explicit-any is disabled (allowed)

PR Review Guidelines

These apply to every review finding, automated reviews included.

  • Verify before prescribing. A finding names its trigger, reachable call path, affected behavior and evidence; check dependency behavior, platform semantics and real trust boundaries in source rather than assuming them. An unverified hypothesis is a question, not a defect.
  • Justify the change, not just the failure. Showing the current design can fail is not enough: compare it with the smallest sufficient fix and the proposal on likelihood, impact, complexity, compatibility and new failure modes. No abstractions, options or authorization machinery for hypothetical needs; guarding against accidental misuse is not guarding against an adversary.
  • Respect documented decisions. A residue or rejected alternative recorded in a contract (see Consistency without transactions and docs/design/) is a decision; its known limitation is not a new finding. Reopening it needs new evidence that its rationale no longer holds.
  • Classify honestly. Only demonstrated correctness problems are findings, and they may block on their impact. Preferences, optional hardening and extensions are non-blocking suggestions, never inflated in severity.
  • Major redesigns need the author's agreement. Changes to public APIs, storage formats, lifecycle, concurrency or permission models, or cross-module structure are proposed with evidence, alternatives and costs, and are not implemented in the PR or made a merge condition without the author's explicit agreement.
  • Review every added or changed test for whether it earns its cost. The offline suite is past 13,000 tests, so a PR's tests get their own review pass, test by test, after the correctness review. Criteria and remedies: Every test must earn its place.
    • Ask of each test whether it is necessary (pins something no other test pins), mergeable (one scenario, one test; one case per branch), and cheap (no real waiting, processes, network or full LightRAG where a stub does; measured with --durations=20). The guide holds the full criteria; do not restate them here.
    • Report the result as concrete edits (which tests to merge, drop or speed up), not a general remark. When you are the PR's author, or took the PR over, apply them yourself. A bug fix that lacks its regression test is still a finding. These are findings the author is asked to address, not the optional suggestions of Classify honestly: the suite's size and runtime are a cost every later PR pays.

Commit and Pull Request Guidance

  • If this repo is a fork of HKUDS/LightRAG. Target to HKUDS/LightRAG when creating PRs, not the fork's own repo.
  • PR descriptions should include: summary, motivation, linked issues if applied, what's changed, what's broken and how it works.
  • Write commit messages (subject and body) in English. Commit messages are repository artifacts — like code comments and log messages — not conversational replies, so they follow the English code-style rule above regardless of any per-conversation working language.