{"record":{"id":"7fdf993ec7626085","repo":"microsoft/semantic-kernel","slug":"key-not-found","errorCode":null,"errorMessage":"Key not found","messagePattern":"Key not found","errorType":"exception","errorClass":"ServiceResourceNotFoundError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/memory_stores/postgres/postgres_memory_store.py","lineNumber":278,"sourceCode":"        with self._connection_pool.connection() as conn, conn.cursor() as cur:\n            if not await self.__does_collection_exist(cur, collection_name):\n                raise ServiceResourceNotFoundError(f\"Collection '{collection_name}' does not exist\")\n            cur.execute(\n                SQL(\n                    \"\"\"\n                        SELECT key, embedding, metadata, timestamp\n                        FROM {scm}.{tbl}\n                        WHERE key = %s\n                        \"\"\"\n                ).format(\n                    scm=Identifier(self._schema),\n                    tbl=Identifier(collection_name),\n                ),\n                (key,),\n            )\n            result = cur.fetchone()\n            if result is None:\n                raise ServiceResourceNotFoundError(\"Key not found\")\n            return MemoryRecord.local_record(\n                id=result[0],\n                embedding=(\n                    np.fromstring(result[1].strip(\"[]\"), dtype=float, sep=\",\") if with_embedding else np.array([])\n                ),\n                text=result[2][\"text\"],\n                description=result[2][\"description\"],\n                additional_metadata=result[2][\"additional_metadata\"],\n                timestamp=result[3],\n            )\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.\n\n        Args:\n            collection_name: The name of the collection to get the records from.","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/memory_stores/postgres/postgres_memory_store.py#L260-L296","documentation":"Raised by PostgresMemoryStore.get() after the SELECT returns no row for the supplied key (cur.fetchone() is None). It is a ServiceResourceNotFoundError distinct from the collection-missing case: the collection is fine, but the specific row is absent. The message is the literal string 'Key not found' with no interpolation.","triggerScenarios":"Awaiting `store.get(collection_name, key)` against an existing collection where no row has `key = <key>`. Common when the record was never upserted, was removed, or the key string differs (whitespace, case, type).","commonSituations":"Reading a key right after upsert that silently failed; key generated client-side differs from stored id; record deleted by remove()/remove_batch(); concurrent writer deleted the row between operations.","solutions":["Catch ServiceResourceNotFoundError around get() and treat as a cache miss.","Prefer get_batch() if partial misses are acceptable; it returns only found rows rather than throwing.","Normalize the key before get (strip, lowercase) to match what was stored.","Verify the row exists with a direct check or by re-upserting before read."],"exampleFix":"// before\nrec = await store.get('mycol', key)\n// after\nfrom semantic_kernel.exceptions import ServiceResourceNotFoundError\ntry:\n    rec = await store.get('mycol', key)\nexcept ServiceResourceNotFoundError:\n    rec = None","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions import ServiceResourceNotFoundError\ntry:\n    record = await store.get(collection_name, key)\nexcept ServiceResourceNotFoundError as e:\n    if str(e) == 'Key not found':\n        record = None\n    else:\n        raise","preventionTips":["Distinguish the two ServiceResourceNotFoundError messages ('Key not found' vs collection-missing) by string, or use get_batch() which tolerates missing keys.","Normalize keys (strip/case) before read so they match stored ids.","Treat get() failures as cache misses in read-through caches."],"tags":["postgres","memory-store","key-not-found","service-resource-not-found"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}