Narsil for Python
delali-narsil lets a Python program create, change, and search a Narsil index inside its process, with no server beside it. The package wraps the Rust implementation of Narsil, so it supports full-text, vector, hybrid, and geo searches, and it saves every file in the layouts of the Narsil specification. This package can open an index that the TypeScript package saves, and the TypeScript package can open an index that this package saves.
Install
pip install delali-narsil
The package needs Python 3.11 or later. PyPI holds wheels for Linux with glibc or musl, macOS, and Windows, each on x64 and arm64, so pip installs the package without a Rust toolchain. A program imports it as narsil.
Create, fill, and search an index
from narsil import Narsil
with Narsil() as engine:
engine.create_index("films", {"schema": {"title": "string", "year": "number", "embedding": "vector[3]"}})
engine.insert_batch(
"films",
[
{"id": "heat", "title": "Heat", "year": 1995, "embedding": [0.9, 0.1, 0.2]},
{"id": "ronin", "title": "Ronin", "year": 1998, "embedding": [0.8, 0.3, 0.1]},
{"id": "solaris", "title": "Solaris", "year": 1972, "embedding": [0.1, 0.9, 0.7]},
],
)
keyword = engine.query("films", {"term": "ronin", "filters": {"fields": {"year": {"gte": 1990}}}})
print(keyword["count"], keyword["hits"][0]["id"])
nearest = engine.query("films", {"vector": {"field": "embedding", "value": [0.85, 0.2, 0.15]}, "limit": 2})
for hit in nearest["hits"]:
print(hit["id"], hit["score"])
The program prints 1 ronin, then heat and ronin with their cosine scores. When a query sets both term and vector, the engine fuses the two rankings into one hybrid ranking.
Every setting, query, and result is a plain dict whose keys are the field names of the Narsil specification, the same names that the TypeScript package and the HTTP server use, so you can copy a query from their documentation without changing it. Method names and keyword arguments use Python's style, such as insert_batch and doc_id.
Use it from asyncio
import asyncio
from narsil import AsyncNarsil
async def main() -> None:
async with await AsyncNarsil.open() as engine:
await engine.create_index("films", {"schema": {"title": "string", "year": "number"}})
await engine.insert_batch(
"films",
[
{"id": "heat", "title": "Heat", "year": 1995},
{"id": "ronin", "title": "Ronin", "year": 1998},
],
)
found = await engine.query("films", {"term": "heat"})
print(found["count"], found["hits"][0]["id"])
asyncio.run(main())
The program prints 1 heat. AsyncNarsil makes each call on a worker thread, so the event loop keeps serving other tasks while the engine works. When you cancel a call, the waiting task receives asyncio.CancelledError at once, while the engine finishes that call on its thread and leaves the index usable.
Threads and the interpreter lock
Every method releases the interpreter lock while the engine works, so several Python threads can search one engine in parallel. The engine never holds a search back for a write, because it serves each search from the index as it stood when the search began.
Keep the index on disk
from narsil import Narsil
engine = Narsil({"durability": {"directory": "./search-data"}})
The engine writes each change to a write-ahead log before the call returns, writes checkpoints in the background, and finds every index again when a later program opens the same directory. The engine flushes its logs as it shuts down, which happens when a with block ends, when the program calls shutdown(), or when the interpreter exits.
Errors
A failure inside the engine raises narsil.NarsilError, whose message describes the failure. Where the TypeScript package raises NarsilError for the same input, the code is the same, such as DOC_VALIDATION_FAILED for a malformed document. The Python package also checks the shape of every setting and query, so an unknown key, a negative limit, or a value of the wrong type raises NarsilError with CONFIG_INVALID, or with the code that the Narsil specification gives for that field, where the TypeScript package can accept the value or fail with a JavaScript TypeError. Where the TypeScript package raises NarsilError for null, a None in the same place raises the same code. Where the TypeScript package accepts null or fails with a TypeError, a None can either keep the default or raise CONFIG_INVALID, so leave a key out when you want its default. When get or a search returns a document that holds a date outside the range of Python's datetime, the call raises NarsilError with the code INTERNAL_ERROR. An argument of the wrong Python type, such as an index name that is not a string, raises TypeError before the engine receives the call, as it does for any Python function.
Write adapters, tokenizers, and plugins in Python
An embedding adapter is an object with a dimensions attribute and an embed(text, purpose) method, and the engine calls it to fill the vector fields that an index embeds. A rerank adapter has score(query, documents), a tokenizer has tokenize(text), and a plugin has a name and any of the hooks before_insert, after_insert, before_update, after_update, before_remove, after_remove, before_search, after_search, on_index_create, on_index_drop, and on_partition_split. narsil.EmbeddingAdapter, narsil.RerankAdapter, and narsil.Tokenizer describe each shape for a type checker.
from narsil import Narsil, NarsilError
class Letters:
dimensions = 3
def embed(self, text: str, purpose: str) -> list[float]:
return [text.count("a"), text.count("e"), text.count("o")]
with Narsil({"embeddingAdapters": {"letters": Letters()}}) as engine:
engine.create_index(
"notes",
{
"schema": {"body": "string", "embedding": "vector[3]"},
"embedding": {"adapter": "letters", "fields": {"embedding": "body"}},
},
)
engine.insert("notes", {"body": "a short note"})
When Python code raises an exception inside the engine, the caller of the operation receives a NarsilError whose message holds the exception's type and text. When an embedding adapter raises, the insert fails with EMBEDDING_FAILED, and the index keeps every document that it held before. Raise a NarsilError with a code from the Narsil specification to choose the code and message that the caller receives.
The engine can call Python code from any of its threads, so an adapter, a tokenizer, or a plugin must be safe to call from any thread.
When a tokenizer, a stop word function, a persistence adapter, an idGenerator, an embedding adapter, the read of a restore or import source, or the write of a snapshot or export destination calls the engine that is calling it, that call raises NarsilError with the code CONFIG_INVALID at once, because the engine waits for the callback to return and can hold a lock of the index while it waits. Each of these callbacks can call any other engine, and a plugin hook, an event listener, a rerank adapter, or a reducer can call any engine, including the one that called it. When such a callback creates, drops, restores, closes, or reopens an index, or changes the query suggestions of an index, while the call that runs the callback holds that index, the engine raises NarsilError with the code CONFIG_INVALID at once. A shutdown() from a listener or a hook returns at once while another shutdown is under way. A callback that hands a call on the same engine to another thread and then waits for that thread can wait forever, because the call on the other thread can need the lock that the engine holds while it waits for the callback.
Build from source
maturin builds the package. From packages/python-engine, python3 scripts/tools.py develop builds the extension into a local environment, and python3 scripts/tools.py pytest tests it. The build needs Rust 1.88 or later and a C11 compiler.
Licence
Apache-2.0. See LICENSE and NOTICE.
Metadata
Release files for delali-narsil 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 28.3 MB
Release files / delali_narsil-0.1.0-cp311-abi3-win_arm64.whl
| Download URL | delali_narsil-0.1.0-cp311-abi3-win_arm64.whl |
|---|---|
| Size | 3.3 MB |
| Tags | CPython 3.11 Windows ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
fb7b15063b69896f108f157930f83e3cad3b5b0eb847b08d48ae144972f2fdf0
|
|
BLAKE2b-256 checksum How to use checksums |
f8c449498d0a24bdd2175b275500875d6cf0bbe6b1a406beeaf083013667b9e0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logRelease files / delali_narsil-0.1.0-cp311-abi3-win_amd64.whl
| Download URL | delali_narsil-0.1.0-cp311-abi3-win_amd64.whl |
|---|---|
| Size | 3.5 MB |
| Tags | CPython 3.11 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
fc25e8be679e0ce63f5246c52b5650af0e077b64104c0e75c8878de0a84ab842
|
|
BLAKE2b-256 checksum How to use checksums |
64a3b32140ba7b3f4e3116fa48a44439392db3cf42b1e0599e6a0386fe876adb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logRelease files / delali_narsil-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl
| Download URL | delali_narsil-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 3.9 MB |
| Tags | CPython 3.11 Linux musl 1.2+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
7dd53291fa36e5f91652c9fa1c82298f3c0915b7de5ea44ace477b2a304c68bc
|
|
BLAKE2b-256 checksum How to use checksums |
eb37374b1f4e7517c459893fecb76109f3effd3bb61bb843fd39189a60263431
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logRelease files / delali_narsil-0.1.0-cp311-abi3-musllinux_1_2_aarch64.whl
| Download URL | delali_narsil-0.1.0-cp311-abi3-musllinux_1_2_aarch64.whl |
|---|---|
| Size | 3.7 MB |
| Tags | CPython 3.11 Linux musl 1.2+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
d67cd410e971003cf612296d737acafa19c944ddf52082bed89a819b830a2e02
|
|
BLAKE2b-256 checksum How to use checksums |
4e33790418b499b6b1ebd203c461c47a8557850515e7bad42ea6033fec8020c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logRelease files / delali_narsil-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | delali_narsil-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 3.6 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
f257e61e1d71d91d3839418e9628eae650aa9cbe960a4d8d2332d5a31f8fbd8a
|
|
BLAKE2b-256 checksum How to use checksums |
c39282e590c7c4682317ef41cdaf37e4c7917cea20780c72125fa8fb06fb3aa8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logRelease files / delali_narsil-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
| Download URL | delali_narsil-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
|---|---|
| Size | 3.5 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
5657b63179f1e5900c6d5b3cb23f0820d34763c06e968bef4ccd19af95770081
|
|
BLAKE2b-256 checksum How to use checksums |
d81510680853b5ba859d681156d6b0b76c09894070be0b91b8ea5231569c7db2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logRelease files / delali_narsil-0.1.0-cp311-abi3-macosx_11_0_arm64.whl
| Download URL | delali_narsil-0.1.0-cp311-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 3.3 MB |
| Tags | CPython 3.11 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
741dd53be7e9df09982185d36056338eae7edc5fe672a7f2ce99c7c975f57cac
|
|
BLAKE2b-256 checksum How to use checksums |
01871157f8e12fd839e0ab316b910ec0bd715402002496e9815bc7accc9bd92e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logRelease files / delali_narsil-0.1.0-cp311-abi3-macosx_10_12_x86_64.whl
| Download URL | delali_narsil-0.1.0-cp311-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 3.5 MB |
| Tags | CPython 3.11 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
56b25021e490604cc51687e7bb5e6b3c0d81edb03351269f17e60dbf9c4b29d6
|
|
BLAKE2b-256 checksum How to use checksums |
693308e32a4ab4f6c34e90695f2c533b05a4708d750290a0f30a1de1da902698
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logSecurity for delali-narsil 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 8 files in this release have been uploaded with provenance metadata.
Attestation for delali_narsil-0.1.0-cp311-abi3-win_arm64.whl
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.