Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Microsoft Semantic kernel InMemoryVectorStore

From Leeroopedia

Overview

This page documents the InMemoryVectorStore class and the GetCollection method for creating typed vector store collections in Microsoft Semantic Kernel. The in-memory vector store is the simplest backend, ideal for development, testing, and prototyping, but the collection access pattern is identical across all supported backends.

Source Reference

  • File: dotnet/src/VectorData/InMemory/InMemoryVectorStore.cs (lines 19-100)
  • Type: API Doc

API Reference

InMemoryVectorStore Constructor

var vectorStore = new InMemoryVectorStore();
Aspect Detail
Namespace Microsoft.SemanticKernel.Data
Returns InMemoryVectorStore instance
Parameters None required
Thread safety Thread-safe for concurrent collection access

The InMemoryVectorStore stores all data in process memory using concurrent dictionaries. Data is not persisted across application restarts. For production scenarios, use a persistent backend such as AzureAISearchVectorStore or QdrantVectorStore.

GetCollection<TKey, TRecord>

VectorStoreCollection<string, Glossary> collection = vectorStore.GetCollection<string, Glossary>("skglossary");
Aspect Detail
Returns VectorStoreCollection<TKey, TRecord>
Type parameters TKey — the key type (e.g., string); TRecord — the record class with vector store attributes
Parameters collectionName (string) — the name of the collection
Behavior Returns a client-side collection reference; does not create backend resources

EnsureCollectionExistsAsync

await collection.EnsureCollectionExistsAsync();
Aspect Detail
Returns Task
Behavior Creates the collection in the backend if it does not already exist; no-op if it exists
Idempotent Yes — safe to call multiple times

Complete Setup Example

The following example demonstrates the full setup flow from store creation to a collection ready for data operations:

using Microsoft.SemanticKernel.Data;

// Step 1: Create the vector store
var vectorStore = new InMemoryVectorStore();

// Step 2: Get a typed collection
var collection = vectorStore.GetCollection<string, Glossary>("skglossary");

// Step 3: Ensure the collection exists in the backend
await collection.EnsureCollectionExistsAsync();

// The collection is now ready for UpsertAsync, SearchAsync, etc.

Backend Swappability

The same collection access pattern works across all backends. Only the store instantiation changes:

// In-memory (development)
var vectorStore = new InMemoryVectorStore();

// Azure AI Search (production)
var vectorStore = new AzureAISearchVectorStore(
    new Uri("https://my-search.search.windows.net"),
    new AzureKeyCredential("my-api-key"));

// Qdrant (self-hosted)
var vectorStore = new QdrantVectorStore(
    new QdrantClient("localhost", 6334));

After store creation, the collection access code is identical:

var collection = vectorStore.GetCollection<string, Glossary>("skglossary");
await collection.EnsureCollectionExistsAsync();

Collection Naming

The collectionName parameter maps to different backend concepts:

Backend Collection Name Maps To
InMemory Dictionary key
Azure AI Search Index name
Qdrant Collection name
Pinecone Index name
Weaviate Class name

Collection names should use lowercase alphanumeric characters and avoid special characters to ensure compatibility across all backends.

Type Parameter Constraints

The TRecord type parameter must be a class that:

  • Has exactly one property with [VectorStoreKey]
  • Has at least one property with [VectorStoreVector] for search scenarios
  • Uses supported property types for data fields

The TKey type parameter must match the type of the property marked with [VectorStoreKey] in the TRecord class.

Error Scenarios

Scenario Behavior
TKey does not match key property type Runtime exception
TRecord missing [VectorStoreKey] Runtime exception on collection creation
Collection name contains unsupported characters Backend-specific error during EnsureCollectionExistsAsync

Relationship to Principle

This implementation page corresponds to the Vector Store Collection Setup principle, which explains the factory pattern and provider abstraction philosophy.

Principle:Microsoft_Semantic_kernel_Vector_Store_Collection_Setup

See Also

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment