Skip to main content

langchain-whoosh

A pure-Python BM25 (lexical) retriever for LangChain, powered by Whoosh.

LangChain pipelines usually reach for a vector store, but dense retrieval has a well-known blind spot: it can quietly miss the exact tokens that matter most — product SKUs, function names, error codes like ERR_2043, gene symbols, ticket IDs. A lexical BM25 retriever is the classic complement, and Whoosh gives you one in pure Python: no server, no native wheels, and an index that is just a folder on disk.

Install

pip install langchain-whoosh

This pulls in langchain-core and whoosh3 (the maintained Whoosh fork).

Quick start

from langchain_whoosh import WhooshRetriever

retriever = WhooshRetriever.from_texts(
    texts=[
        "Whoosh is a pure-Python full-text search library.",
        "BM25 ranks documents by term rarity and frequency.",
    ],
    ids=["a", "b"],
    metadatas=[{"src": "readme"}, {"src": "docs"}],
    k=4,
)

docs = retriever.invoke("pure python search")
for d in docs:
    print(d.metadata["score"], d.page_content)

Each result is a standard langchain_core.documents.Document; the original id, the BM25 score, and any metadata you supplied are attached under Document.metadata.

Persist an index to disk

# Build once …
WhooshRetriever.from_texts(texts=texts, ids=ids, path="./my_index")

# … reopen later without re-indexing.
retriever = WhooshRetriever.from_index("./my_index", k=8)

Hybrid search (lexical + vector)

Drop this retriever and your vector retriever into LangChain's EnsembleRetriever; it does Reciprocal Rank Fusion for you:

from langchain.retrievers import EnsembleRetriever

hybrid = EnsembleRetriever(
    retrievers=[whoosh_retriever, vector_retriever],
    weights=[0.5, 0.5],
)

License

BSD-2-Clause, matching Whoosh. See the Whoosh repository for the full project, docs, and issue tracker.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_whoosh-0.1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

langchain_whoosh-0.1.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file langchain_whoosh-0.1.1.tar.gz.

File metadata

  • Download URL: langchain_whoosh-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for langchain_whoosh-0.1.1.tar.gz
Algorithm Hash digest
SHA256 37cf8f4b5d13614df2ad757e5e3eeb1015233661c4645f2f3e77de7d283a6ef6
MD5 12b74ab9439166563601970ba93355e3
BLAKE2b-256 94116565050527971d7ec02c16425922fcf92265431d0770b7903cde8ace6fd9

See more details on using hashes here.

File details

Details for the file langchain_whoosh-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_whoosh-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1e157db13199d48e97a8e403bf02b5e53bd6d0d3bd76527204b9c572f1800ae3
MD5 e77aab8e6dd930aa4c99dd27491cba8a
BLAKE2b-256 23262e8917620144462911fc73e93d8dc5f2c42be140a10327f440ec77ed6677

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 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