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.

The documents that a read returns leave out their vector fields, so engine.get("films", "heat") answers without embedding. To get the vectors back, set "returnVectors": True in the index settings, or pass {"vectors": True} as the document argument of get and get_multiple, or as the document member of a query or a listing.

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

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.1
File Actions
delali_narsil-0.2.1-cp311-abi3-win_arm64.whl CPython 3.11 abi3 Windows ARM64 Details for delali_narsil-0.2.1-cp311-abi3-win_arm64.whl
delali_narsil-0.2.1-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for delali_narsil-0.2.1-cp311-abi3-win_amd64.whl
delali_narsil-0.2.1-cp311-abi3-musllinux_1_2_x86_64.whl CPython 3.11 abi3 Linux musl 1.2+ x86-64 Details for delali_narsil-0.2.1-cp311-abi3-musllinux_1_2_x86_64.whl
delali_narsil-0.2.1-cp311-abi3-musllinux_1_2_aarch64.whl CPython 3.11 abi3 Linux musl 1.2+ ARM64 Details for delali_narsil-0.2.1-cp311-abi3-musllinux_1_2_aarch64.whl
delali_narsil-0.2.1-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.1-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
delali_narsil-0.2.1-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for delali_narsil-0.2.1-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
delali_narsil-0.2.1-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for delali_narsil-0.2.1-cp311-abi3-macosx_11_0_arm64.whl
delali_narsil-0.2.1-cp311-abi3-macosx_10_12_x86_64.whl CPython 3.11 abi3 macOS 10.12+ x86-64 Details for delali_narsil-0.2.1-cp311-abi3-macosx_10_12_x86_64.whl

Total release size: 30.4 MB

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

Download URL delali_narsil-0.2.1-cp311-abi3-win_arm64.whl
Size 3.6 MB
Tags CPython 3.11 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
e27b31b6e6cd329db700272e2ee98c6c6883c8367e330a65eb7d41a8ead0c068
BLAKE2b-256 checksum
How to use checksums
84b799fa5049425c1dfe61266961178c01856fa3fbab507c58c07bc4a5f2c224
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 24, 2026.

Transparency log

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

Download URL delali_narsil-0.2.1-cp311-abi3-win_amd64.whl
Size 3.8 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
32194c50471bee5f7b8c811d644760167937a8798e3b9e3cf648422e853b3cad
BLAKE2b-256 checksum
How to use checksums
854f19332560f20b48b7831d38317abc2e20ad2cd5dfa63bed7a366b9ca9c388
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 24, 2026.

Transparency log

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

Download URL delali_narsil-0.2.1-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
c9c63d4fb2caced619de193ea4d16b98e02915c5f9c9938421fe3171534e9345
BLAKE2b-256 checksum
How to use checksums
8dbd7831fed57646260bba88040fef4f214e1320404539416337d1ec903cd558
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 24, 2026.

Transparency log

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

Download URL delali_narsil-0.2.1-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
9b8dff2cf33a75201c3151ccae5b5769c05a74bbfe02d0d1ff6ddfdf645376dd
BLAKE2b-256 checksum
How to use checksums
8fb17a1070259c1dcc7d36b302e8c2fc288adc997a9f694c4bf9c76702ab1e5e
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 24, 2026.

Transparency log

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

Download URL delali_narsil-0.2.1-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 3.9 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
72429488dfd3f8a60aeeed3a10cd83d6eb90a5dcf1b30ebaf74b483d8feb602b
BLAKE2b-256 checksum
How to use checksums
a695ffa431061c11a6282946aea7f4341032a8dc8bdaf4148416bf23d14583bb
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 24, 2026.

Transparency log

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

Download URL delali_narsil-0.2.1-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 3.8 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
e13bd056f849fe75e2c199bda74b677bfc606d858928fe7a50d94c151ecf000a
BLAKE2b-256 checksum
How to use checksums
effa13a844aae25705928b121398d3ba24c65bf655d9c51f3879b8f1b32c9332
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 24, 2026.

Transparency log

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

Download URL delali_narsil-0.2.1-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
574889237ddb8face223996ae0198274a57113837b2064fcd3483a31d430b3bf
BLAKE2b-256 checksum
How to use checksums
85851070bb288b31c50507ca504eeae89a502af3338380b0667adb86a6d0b6c7
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 24, 2026.

Transparency log

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

Download URL delali_narsil-0.2.1-cp311-abi3-macosx_10_12_x86_64.whl
Size 3.8 MB
Tags CPython 3.11 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
7f61c1257690cdcf3b4e619e4dbeb97ac00199a71a21fda77a60073752aa8f70
BLAKE2b-256 checksum
How to use checksums
1d73eb2a1014af08027f60d7e559eac6dc3d26583d0ffbe5e4aad5838a07520b
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 24, 2026.

Transparency log

Security for delali-narsil 0.2.1

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.1-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 24, 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