{"record":{"id":"0d0a496a90a74c6c","repo":"microsoft/semantic-kernel","slug":"record-with-key-key-does-not-exist","errorCode":null,"errorMessage":"Record with key '{key}' does not exist","messagePattern":"Record with key '(.+?)' does not exist","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"warning","filePath":"python/semantic_kernel/connectors/memory_stores/astradb/astradb_memory_store.py","lineNumber":216,"sourceCode":"        \"\"\"Gets a record. Does not guarantee that the collection exists.\n\n        Args:\n            collection_name (str): The name of the collection to get the record from.\n            key (str): The unique database key of the record.\n            with_embedding (bool): Whether to include the embedding in the result. (default: {False})\n\n        Returns:\n            MemoryRecord: The record.\n        \"\"\"\n        filter = {\"_id\": key}\n        documents = await self._client.find_documents(\n            collection_name=collection_name,\n            filter=filter,\n            include_vector=with_embedding,\n        )\n\n        if len(documents) == 0:\n            raise KeyError(f\"Record with key '{key}' does not exist\")\n\n        return parse_payload(documents[0])\n\n    async def get_batch(\n        self, collection_name: str, keys: list[str], with_embeddings: bool = False\n    ) -> list[MemoryRecord]:\n        \"\"\"Gets a batch of records. Does not guarantee that the collection exists.\n\n        Args:\n            collection_name (str): The name of the collection to get the records from.\n            keys (List[str]): The unique database keys of the records.\n            with_embeddings (bool): Whether to include the embeddings in the results. (default: {False})\n\n        Returns:\n            List[MemoryRecord]: The records.\n        \"\"\"\n        filter = {\"_id\": {\"$in\": keys}}\n        documents = await self._client.find_documents(","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/memory_stores/astradb/astradb_memory_store.py#L198-L234","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nrec = await store.get(\"docs\", key)  # KeyError if absent\n\n// after\ntry:\n    rec = await store.get(\"docs\", key)\nexcept KeyError:\n    rec = None  # or default value","handlingStrategy":"try-catch","validationCode":"# optional existence pre-check via batch (cheaper than raising)\nasync def exists_or_none(store, collection, key):\n    recs = await store.get_batch(collection, [key], with_embeddings=False)\n    return recs[0] if recs else None","typeGuard":null,"tryCatchPattern":"try:\n    rec = await store.get(\"coll\", key)\nexcept KeyError:\n    rec = None  # treat missing record as a normal case","preventionTips":["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."],"tags":["astra-db","key-not-found","lookup","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}