Skip to main content

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.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for delali-narsil 0.2.0
File Actions
delali_narsil-0.2.0-cp311-abi3-win_arm64.whl CPython 3.11 abi3 Windows ARM64 Details for delali_narsil-0.2.0-cp311-abi3-win_arm64.whl
delali_narsil-0.2.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for delali_narsil-0.2.0-cp311-abi3-win_amd64.whl
delali_narsil-0.2.0-cp311-abi3-musllinux_1_2_x86_64.whl CPython 3.11 abi3 Linux musl 1.2+ x86-64 Details for delali_narsil-0.2.0-cp311-abi3-musllinux_1_2_x86_64.whl
delali_narsil-0.2.0-cp311-abi3-musllinux_1_2_aarch64.whl CPython 3.11 abi3 Linux musl 1.2+ ARM64 Details for delali_narsil-0.2.0-cp311-abi3-musllinux_1_2_aarch64.whl
delali_narsil-0.2.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details for delali_narsil-0.2.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
delali_narsil-0.2.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for delali_narsil-0.2.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
delali_narsil-0.2.0-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for delali_narsil-0.2.0-cp311-abi3-macosx_11_0_arm64.whl
delali_narsil-0.2.0-cp311-abi3-macosx_10_12_x86_64.whl CPython 3.11 abi3 macOS 10.12+ x86-64 Details for delali_narsil-0.2.0-cp311-abi3-macosx_10_12_x86_64.whl

Total release size: 29.8 MB

Release files / delali_narsil-0.2.0-cp311-abi3-win_arm64.whl

Download URL delali_narsil-0.2.0-cp311-abi3-win_arm64.whl
Size 3.5 MB
Tags CPython 3.11 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
93254ffef1dea20029f482002f42fb4ee13a14c8440ab456f9b0257d420c5b26
BLAKE2b-256 checksum
How to use checksums
24ed2dbf973d82c6e931a1f63fd09a83c57555b21d0dd11b051478dabebf4cf6
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 19, 2026.

Transparency log

Release files / delali_narsil-0.2.0-cp311-abi3-win_amd64.whl

Download URL delali_narsil-0.2.0-cp311-abi3-win_amd64.whl
Size 3.7 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
01cba1d07f056617ac080d85c6b00f67ee10445e968a485cf924722428f8fc1d
BLAKE2b-256 checksum
How to use checksums
cc160935c6af7b243b763276db57253d50ead2675cfa3941206d5cd80004c846
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 19, 2026.

Transparency log

Release files / delali_narsil-0.2.0-cp311-abi3-musllinux_1_2_x86_64.whl

Download URL delali_narsil-0.2.0-cp311-abi3-musllinux_1_2_x86_64.whl
Size 4.1 MB
Tags CPython 3.11 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
5c69b479472751586a17d99bde353a52bd13125cb767dc738c26e52c39400956
BLAKE2b-256 checksum
How to use checksums
0e37935ba74c602c4926bddec6ad3c95c7fb47ed459c29028c03cdecd5b99291
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 19, 2026.

Transparency log

Release files / delali_narsil-0.2.0-cp311-abi3-musllinux_1_2_aarch64.whl

Download URL delali_narsil-0.2.0-cp311-abi3-musllinux_1_2_aarch64.whl
Size 3.9 MB
Tags CPython 3.11 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
81a0f72e6d2f4db3516ffad8d3cd70124958dac57b3e2b60107739e7e4c26457
BLAKE2b-256 checksum
How to use checksums
8181abd2c8de9d920565a3ba7712eb63066ebfd555b7bfdb9f759a55d946ad34
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 19, 2026.

Transparency log

Release files / delali_narsil-0.2.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL delali_narsil-0.2.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 3.8 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
b146eb414a04e4a096d4f155c9b19f2743e0791919f03176275b2cf7da8eacc7
BLAKE2b-256 checksum
How to use checksums
3c777657d418a2fd4ae85cc828d6343993fd6ed51a61204ce1dfd3101f4cdb5c
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 19, 2026.

Transparency log

Release files / delali_narsil-0.2.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL delali_narsil-0.2.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 3.7 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
540421bfbc582c2bb3186446bdd32d4d924dd1eec81f68f9e41dbbaf2afcc11c
BLAKE2b-256 checksum
How to use checksums
5c30bd0527cbc492751f2450c4f41d9df8131bfa3b7e45aec0758a96063f0049
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 19, 2026.

Transparency log

Release files / delali_narsil-0.2.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL delali_narsil-0.2.0-cp311-abi3-macosx_11_0_arm64.whl
Size 3.5 MB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
eba3fea4d2041e48f633cdba6c484015bc567f49dd22efa1c0232793d342e338
BLAKE2b-256 checksum
How to use checksums
8c5993b44b08d5e760a7adaed421b2faa2f13436ce5d429abe1cac0deccd51f2
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 19, 2026.

Transparency log

Release files / delali_narsil-0.2.0-cp311-abi3-macosx_10_12_x86_64.whl

Download URL delali_narsil-0.2.0-cp311-abi3-macosx_10_12_x86_64.whl
Size 3.7 MB
Tags CPython 3.11 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
20a9506049d368a570ae46473677f4c36d2d4267db78a1ded5f5759b392cc952
BLAKE2b-256 checksum
How to use checksums
6c03b3ceee95398e6e9cfdbafb904fda5c18a1726dd4166ea60cf934b4d46e05
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 19, 2026.

Transparency log

Security for delali-narsil 0.2.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.

Select from 8 files

Attestation for delali_narsil-0.2.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 19, 2026.

Transparency log

A 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.

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