{"record":{"id":"18a1b2eec08a89b5","repo":"microsoft/semantic-kernel","slug":"upsert-failed","errorCode":null,"errorMessage":"Upsert failed","messagePattern":"Upsert failed","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/memory_stores/mongodb_atlas/mongodb_atlas_memory_store.py","lineNumber":161,"sourceCode":"        Does not guarantee that the collection exists.\n            If the record already exists, it will be updated.\n            If the record does not exist, it will be created.\n\n        Args:\n            collection_name (str): The name associated with a collection of embeddings.\n            record (MemoryRecord): The memory record to upsert.\n\n        Returns:\n            str: The unique identifier for the memory record.\n        \"\"\"\n        document: Mapping[str, Any] = memory_record_to_mongo_document(record)\n\n        update_result: results.UpdateResult = await self.database[collection_name].update_one(\n            document, {\"$set\": document}, upsert=True\n        )\n\n        if not update_result.acknowledged:\n            raise ValueError(\"Upsert failed\")\n        return record._id\n\n    async def upsert_batch(self, collection_name: str, records: list[MemoryRecord]) -> list[str]:\n        \"\"\"Upserts a group of memory records into the data store.\n\n        Does not guarantee that the collection exists.\n            If the record already exists, it will be updated.\n            If the record does not exist, it will be created.\n\n        Args:\n            collection_name (str): The name associated with a collection of embeddings.\n            records (MemoryRecord): The memory records to upsert.\n\n        Returns:\n            List[str]: The unique identifiers for the memory records.\n        \"\"\"\n        upserts: list[UpdateOne] = []\n        for record in records:","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/memory_stores/mongodb_atlas/mongodb_atlas_memory_store.py#L143-L179","documentation":"Raised as a ValueError in MongoDBAtlasMemoryStore.upsert when update_result.acknowledged is False. MongoDB write operations are 'acknowledged' when the server confirms the write per the write concern; an unacknowledged result (write concern w=0) means the driver did not wait for confirmation. The store treats this as a failure.","triggerScenarios":"The MongoDB client is configured with an unacknowledged write concern (w=0). This can happen if the connection string or client options set w=0, or if a server/driver edge case returns unacknowledged.","commonSituations":"Connection string includes ?w=0 for low-latency fire-and-forget writes. Client configured with WriteConcern(w=0) for throughput. Misconfigured motor client options passed through from settings.","solutions":["Ensure the write concern is acknowledged (default w=majority or w=1); remove ?w=0 from the connection string.","If you intentionally use unacknowledged writes, note this store rejects them — switch to acknowledged.","Verify the motor AsyncIOMotorClient is constructed with default or majority write concern.","Check that update_one is not being called with explicit unacknowledged options."],"exampleFix":"// before\n# connection string has w=0 -> unacknowledged\nstore = MongoDBAtlasMemoryStore(connection_string='mongodb+srv://...?w=0')\nawait store.upsert('docs', record)  # ValueError: Upsert failed\n// after\n# remove w=0 so writes are acknowledged\nstore = MongoDBAtlasMemoryStore(connection_string='mongodb+srv://...?w=majority')\nawait store.upsert('docs', record)","handlingStrategy":"validation","validationCode":"def is_acknowledged_write_concern(conn_str: str) -> bool:\n    s = conn_str.lower()\n    if 'w=0' in s:\n        return False\n    return True\n\nif not is_acknowledged_write_concern(connection_string):\n    raise ValueError('Connection string uses unacknowledged write concern (w=0)')","typeGuard":null,"tryCatchPattern":"try:\n    await store.upsert('docs', record)\nexcept ValueError as e:\n    if 'Upsert failed' in str(e):\n        logging.error('Write was unacknowledged; check write concern')\n    raise","preventionTips":["Avoid w=0 in the MongoDB Atlas connection string.","Use default or majority write concern for data integrity.","Verify motor client construction does not override the write concern.","Test upsert in a staging environment to confirm acknowledged writes."],"tags":["mongodb-atlas","upsert","write-concern","configuration","python"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}