microsoft/semantic-kernel · warning · KeyError
Record with key '{key}' does not exist
Error message
Record with key '{key}' does not exist What it means
Raised in `AstraDBMemoryStore.get`: after querying Astra by `_id`, if the returned documents list is empty, a `KeyError` is raised with the missing key. This is a lookup-miss error signalling that no record exists in the collection for the given key.
Source
Thrown at python/semantic_kernel/connectors/memory_stores/astradb/astradb_memory_store.py:216
"""Gets a record. Does not guarantee that the collection exists.
Args:
collection_name (str): The name of the collection to get the record from.
key (str): The unique database key of the record.
with_embedding (bool): Whether to include the embedding in the result. (default: {False})
Returns:
MemoryRecord: The record.
"""
filter = {"_id": key}
documents = await self._client.find_documents(
collection_name=collection_name,
filter=filter,
include_vector=with_embedding,
)
if len(documents) == 0:
raise KeyError(f"Record with key '{key}' does not exist")
return parse_payload(documents[0])
async def get_batch(
self, collection_name: str, keys: list[str], with_embeddings: bool = False
) -> list[MemoryRecord]:
"""Gets a batch of records. Does not guarantee that the collection exists.
Args:
collection_name (str): The name of the collection to get the records from.
keys (List[str]): The unique database keys of the records.
with_embeddings (bool): Whether to include the embeddings in the results. (default: {False})
Returns:
List[MemoryRecord]: The records.
"""
filter = {"_id": {"$in": keys}}
documents = await self._client.find_documents(View on GitHub (pinned to c028a0c7dc)
Solutions
- Confirm the key was actually inserted into that collection (check via a list/scan or your write log).
- Verify you are querying the correct collection and keyspace.
- Catch `KeyError` at the call site to handle the missing-record case gracefully (e.g. return a default or upsert).
- Normalize key formatting (encoding/trimming) so reads match writes exactly.
Example fix
// before
rec = await store.get("docs", key) # KeyError if absent
// after
try:
rec = await store.get("docs", key)
except KeyError:
rec = None # or default value Defensive patterns
Strategy: try-catch
Validate before calling
# optional existence pre-check via batch (cheaper than raising)
async def exists_or_none(store, collection, key):
recs = await store.get_batch(collection, [key], with_embeddings=False)
return recs[0] if recs else None Try / catch
try:
rec = await store.get("coll", key)
except KeyError:
rec = None # treat missing record as a normal case Prevention
- Use `get_batch` (no KeyError on miss) when missing keys are expected.
- Catch KeyError explicitly to distinguish missing vs. server errors.
- Normalize keys consistently between write and read paths.
When it happens
Trigger: Calling `get(collection_name, key)` (or `get_batch` indirectly) for a key that has never been inserted, was deleted, or lives in a different collection/keyspace.
Common situations: Reading before writing; querying the wrong collection; key formatting mismatch (e.g. stored with a prefix/suffix); record was deleted by another process; race where the record hasn't been committed yet.
Related errors
- Memory record not found
- Agent type '{recipient.type}' does not exist.
- Agent with name {agent_id.type} not found.
- Agent with name {id.type} not found.
- Astra DB request error - {response_dict['errors']}
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/0d0a496a90a74c6c.
Report an issue: GitHub.