microsoft/semantic-kernel · error · MemoryConnectorInitializationError

Failed to create MongoDB Atlas settings.

Error message

Failed to create MongoDB Atlas settings.

What it means

Raised as a MemoryConnectorInitializationError in MongoDBAtlasMemoryStore.__init__ when MongoDBAtlasSettings construction raises a ValidationError. The settings object validates required fields (connection string, database name, index name) from constructor args and/or environment variables; any missing or invalid required field triggers the pydantic ValidationError which is then wrapped here.

Source

Thrown at python/semantic_kernel/connectors/memory_stores/mongodb_atlas/mongodb_atlas_memory_store.py:70

            connection_string (str): The connection string for the MongoDB Atlas instance.
            database_name (str): The name of the database.
            read_preference (ReadPreference): The read preference for the connection.
            env_file_path (str): The path to the .env file containing the connection string.
            env_file_encoding (str): The encoding of the .env file.

        """
        from semantic_kernel.connectors.mongodb import MongoDBAtlasSettings

        try:
            mongodb_settings = MongoDBAtlasSettings(
                database_name=database_name,
                index_name=index_name,
                connection_string=connection_string,
                env_file_path=env_file_path,
                env_file_encoding=env_file_encoding,
            )
        except ValidationError as ex:
            raise MemoryConnectorInitializationError("Failed to create MongoDB Atlas settings.") from ex

        self.mongo_client: motor_asyncio.AsyncIOMotorClient = motor_asyncio.AsyncIOMotorClient(
            mongodb_settings.connection_string.get_secret_value(),
            read_preference=read_preference,
            driver=DriverInfo("Microsoft Semantic Kernel", metadata.version("semantic-kernel")),
        )
        self.database_name: str = mongodb_settings.database_name
        self.index_name: str = mongodb_settings.index_name

    @property
    def database(self) -> core.AgnosticDatabase:
        """The database object."""
        return self.mongo_client[self.database_name]

    @property
    def num_candidates(self) -> int:
        """The number of candidates to return."""
        return self.__num_candidates

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set the required environment variables: MONGODB_ATLAS_CONNECTION_STRING, MONGODB_ATLAS_DATABASE_NAME, and the index name, or pass them as constructor args.
  2. If using a .env file, verify env_file_path is correct and the file is readable.
  3. Inspect the chained ValidationError (__cause__) for the specific missing/invalid field.
  4. Use MongoDBAtlasSettings() standalone first to surface detailed field-level errors before constructing the store.

Example fix

// before
store = MongoDBAtlasMemoryStore()  # MemoryConnectorInitializationError (missing settings)
// after
# .env
# MONGODB_ATLAS_CONNECTION_STRING=mongodb+srv://...
# MONGODB_ATLAS_DATABASE_NAME=mydb
store = MongoDBAtlasMemoryStore(index_name='vector_index')
Defensive patterns

Strategy: validation

Validate before calling

from semantic_kernel.connectors.mongodb import MongoDBAtlasSettings

def validate_mongodb_settings(**kwargs):
    try:
        return MongoDBAtlasSettings(**kwargs)
    except Exception as e:
        raise ValueError(f'MongoDB Atlas settings invalid: {e}') from e

settings = validate_mongodb_settings(
    database_name='mydb', index_name='vector_index', connection_string=conn_str
)

Try / catch

from semantic_kernel.exceptions.memory_connector_exceptions import MemoryConnectorInitializationError

try:
    store = MongoDBAtlasMemoryStore(index_name='vector_index')
except MemoryConnectorInitializationError as e:
    logging.error('MongoDB Atlas settings invalid: %s', e.__cause__)
    raise

Prevention

When it happens

Trigger: Constructing MongoDBAtlasMemoryStore without MONGODB_ATLAS_CONNECTION_STRING / database name / index name in the environment or args. Passing an invalid connection string format. A .env file that is missing or unreadable at the given env_file_path.

Common situations: Missing .env file or MONGODB_ATLAS_* env vars in the deployment environment. Typo in the env var name. Connection string lacking required MongoDB Atlas vector search parameters. Running locally without loading the env file.

Related errors


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