microsoft/semantic-kernel · error · ServiceResourceNotFoundError

Key '{key}' not found in collection '{collection_name}'

Error message

Key '{key}' not found in collection '{collection_name}'

What it means

Raised by USearchMemoryStore.get (ServiceResourceNotFoundError) after delegating to get_batch and receiving an empty result for the requested key. The collection exists, but no record with that key (id) is present in the collection's id-to-label map, so a lookup-by-key returns nothing.

Source

Thrown at python/semantic_kernel/connectors/memory_stores/usearch/usearch_memory_store.py:384

        return all_records_id

    async def get(
        self,
        collection_name: str,
        key: str,
        with_embedding: bool,
        dtype: ScalarKind = ScalarKind.F32,
    ) -> MemoryRecord:
        """Retrieve a single MemoryRecord using its key."""
        collection_name = collection_name.lower()
        result = await self.get_batch(
            collection_name=collection_name,
            keys=[key],
            with_embeddings=with_embedding,
            dtype=dtype,
        )
        if not result:
            raise ServiceResourceNotFoundError(f"Key '{key}' not found in collection '{collection_name}'")
        return result[0]

    async def get_batch(
        self,
        collection_name: str,
        keys: list[str],
        with_embeddings: bool,
        dtype: ScalarKind = ScalarKind.F32,
    ) -> list[MemoryRecord]:
        """Retrieve a batch of MemoryRecords using their keys."""
        collection_name = collection_name.lower()
        if collection_name not in self._collections:
            raise ServiceResourceNotFoundError(f"Collection {collection_name} does not exist")

        ucollection = self._collections[collection_name]
        labels = [ucollection.embeddings_id_to_label[key] for key in keys if key in ucollection.embeddings_id_to_label]
        if not labels:
            return []

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Use get_batch (which returns [] instead of raising) when absence is expected, then handle empty.
  2. Verify the key against the ids returned by upsert_batch.
  3. Guard/try-catch ServiceResourceNotFoundError for single-key lookups.

Example fix

// before
rec = await store.get('docs', key, with_embedding=True)
// after
recs = await store.get_batch('docs', [key], with_embeddings=True)
rec = recs[0] if recs else None
Defensive patterns

Strategy: try-catch

Validate before calling

# prefer get_batch to avoid raising on miss
recs = await store.get_batch(collection_name, [key], with_embeddings=True)
rec = recs[0] if recs else None

Try / catch

from semantic_kernel.exceptions import ServiceResourceNotFoundError
try:
    rec = await store.get(collection_name, key, with_embedding=True)
except ServiceResourceNotFoundError:
    rec = None  # treat missing key as absent

Prevention

When it happens

Trigger: Calling `await store.get('docs', 'key123', with_embedding=True)` where 'key123' was never inserted, was already removed, or the key string differs from what was stored (the store maps record._id to a label).

Common situations: Reading a key that was deleted; key mismatch (e.g. stored as a generated id vs. the value you query with); querying immediately after an upsert that failed silently; case/format differences in the id.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/178b6bdaaf118da3. Report an issue: GitHub.