llm icon indicating copy to clipboard operation
llm copied to clipboard

similar_by_id returns the source item for an empty-string ID

Open takagibit18 opened this issue 2 months ago • 1 comments

Collection.similar_by_id('', number=1) returns the item being queried when its ID is an empty string. That takes the only result slot, even when another item is available. Passing skip_id='' to similar_by_vector() has the same result.

Reproduced on main at 764dc38 with Python 3.12.13.

Reproduction

Save this as repro.py in a checkout with LLM's dependencies installed, then run python repro.py. The model supplies fixed vectors locally.

import llm
from sqlite_utils import Database


class ExampleEmbeddingModel(llm.EmbeddingModel):
    model_id = "empty-id-repro"

    def embed_batch(self, items, *, key=None):
        vectors = {"query": [1.0, 0.0], "neighbor": [1.0, 1.0]}
        for item in items:
            yield vectors[item]


db = Database(memory=True, execute_plugins=False)
collection = llm.Collection("repro", db=db, model=ExampleEmbeddingModel())
collection.embed("", "query")
collection.embed("neighbor", "neighbor")

print([entry.id for entry in collection.similar_by_id("", number=1)])
print(
    [
        entry.id
        for entry in collection.similar_by_vector(
            [1.0, 0.0], number=1, skip_id=""
        )
    ]
)
db.close()

Current output

['']
['']

Expected output

['neighbor']
['neighbor']

The truthiness check for skip_id in similar_by_vector() skips the exclusion filter for ''. Checking skip_id is not None would distinguish an empty ID from an omitted ID.

takagibit18 avatar Oct 02 '26 11:10 takagibit18

I checked the current llm/embeddings.py: similar_by_vector() still guards the exclusion filter with if skip_id:, so an empty-string ID bypasses the filter. similar_by_id() passes that ID through as skip_id, matching the reported reproduction. Thanks for including a self-contained, provider-free repro and expected output.

pylrn avatar Oct 06 '26 07:10 pylrn