{"id":4853,"date":"2025-05-21T07:46:46","date_gmt":"2025-05-21T14:46:46","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=4853"},"modified":"2025-05-21T07:46:46","modified_gmt":"2025-05-21T14:46:46","slug":"transitioning-to-new-iembeddinggenerator-interface","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/transitioning-to-new-iembeddinggenerator-interface\/","title":{"rendered":"Transitioning to new Extensions AI IEmbeddingGenerator interface"},"content":{"rendered":"<p><center><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2025\/05\/fdc3edd7-fc8b-4e85-a664-c118dd1e0b43.png\"><img decoding=\"async\" class=\"aligncenter size-medium wp-image-4856\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2025\/05\/fdc3edd7-fc8b-4e85-a664-c118dd1e0b43-300x300.png\" alt=\"Transitioning from ITextEmbeddingGenerationService to IEmbeddingGenerator\" width=\"300\" height=\"300\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2025\/05\/fdc3edd7-fc8b-4e85-a664-c118dd1e0b43-300x300.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2025\/05\/fdc3edd7-fc8b-4e85-a664-c118dd1e0b43-150x150.png 150w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2025\/05\/fdc3edd7-fc8b-4e85-a664-c118dd1e0b43-768x768.png 768w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2025\/05\/fdc3edd7-fc8b-4e85-a664-c118dd1e0b43-24x24.png 24w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2025\/05\/fdc3edd7-fc8b-4e85-a664-c118dd1e0b43-48x48.png 48w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2025\/05\/fdc3edd7-fc8b-4e85-a664-c118dd1e0b43-96x96.png 96w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2025\/05\/fdc3edd7-fc8b-4e85-a664-c118dd1e0b43.png 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/center><\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"2\">As Semantic Kernel shifts its foundational abstractions to Microsoft.Extensions.AI, we are obsoleting and moving away from our experimental embeddings interfaces to the new standardized abstractions that provide a more consistent and powerful way to work with AI services across the .NET ecosystem.<\/p>\n<h2 id=\"the-evolution-of-embedding-generation-in-semantic-kernel\" class=\"code-line\" dir=\"auto\" data-line=\"4\">The Evolution of Embedding Generation in Semantic Kernel<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"6\">Semantic Kernel has always aimed to provide a unified way to interact with AI services, including embedding generation. Our initial approach used the\u00a0<code>ITextEmbeddingGenerationService<\/code>\u00a0interface, which served us well during the experimental phase. However, as the AI landscape has matured, so have our abstractions.<\/p>\n<p class=\"code-line code-active-line\" dir=\"auto\" data-line=\"8\">With Microsoft.Extensions.AI now generally available (no longer in preview), we&#8217;re standardizing on the more robust\u00a0<code>IEmbeddingGenerator&lt;string, Embedding&lt;float&gt;&gt;<\/code>\u00a0interface, which offers several advantages over our previous approach.<\/p>\n<h2 id=\"why-make-the-change\" class=\"code-line\" dir=\"auto\" data-line=\"10\">Why Make the Change?<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"12\">The transition to\u00a0<code>Microsoft.Extensions.AI.IEmbeddingGenerator<\/code>\u00a0brings several benefits:<\/p>\n<ol class=\"code-line\" dir=\"auto\" data-line=\"14\">\n<li class=\"code-line\" dir=\"auto\" data-line=\"14\"><strong>Standardization<\/strong>: Aligns with broader .NET ecosystem patterns and Microsoft.Extensions conventions<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"15\"><strong>Type Safety<\/strong>: Stronger typing with the generic\u00a0<code>Embedding&lt;float&gt;<\/code>\u00a0return type<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"16\"><strong>Flexibility<\/strong>: Support for different input types and embedding formats<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"17\"><strong>Consistency<\/strong>: Uniform approach across different AI service providers<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"18\"><strong>Integration<\/strong>: Seamless integration with other Microsoft.Extensions libraries<\/li>\n<\/ol>\n<h2 id=\"code-comparison-before-and-after\" class=\"code-line\" dir=\"auto\" data-line=\"20\">Code Comparison: Before and After<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"22\">Let&#8217;s look at how this transition affects your code:<\/p>\n<h3 id=\"before-using-itextembeddinggenerationservice\" class=\"code-line\" dir=\"auto\" data-line=\"24\">Before: Using ITextEmbeddingGenerationService<\/h3>\n<pre><code class=\"code-line language-csharp\" dir=\"auto\" data-line=\"26\"><span class=\"hljs-keyword\">using<\/span> Microsoft.SemanticKernel;\r\n<span class=\"hljs-keyword\">using<\/span> Microsoft.SemanticKernel.Embeddings;\r\n\r\n<span class=\"hljs-comment\">\/\/ Create a kernel builder<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> builder = Kernel.CreateBuilder();\r\n\r\n<span class=\"hljs-comment\">\/\/ Add the OpenAI embedding service<\/span>\r\nbuilder.Services.AddOpenAITextEmbeddingGeneration(\r\n    modelId: <span class=\"hljs-string\">\"text-embedding-ada-002\"<\/span>,\r\n    apiKey: \"your-api-key\");\r\n\r\n<span class=\"hljs-comment\">\/\/ Build the kernel<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> kernel = builder.Build();\r\n\r\n<span class=\"hljs-comment\">\/\/ Get the embedding service from the kernel<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> embeddingService = kernel.GetRequiredService&lt;ITextEmbeddingGenerationService&gt;();\r\n\r\n<span class=\"hljs-comment\">\/\/ Generate embeddings<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> text = <span class=\"hljs-string\">\"Semantic Kernel is a lightweight SDK that integrates Large Language Models (LLMs) with conventional programming languages.\"<\/span>;\r\n<span class=\"hljs-keyword\">var<\/span> embedding = <span class=\"hljs-keyword\">await<\/span> embeddingService.GenerateEmbeddingAsync(text);\r\n\r\n<span class=\"hljs-comment\">\/\/ Work with the embedding vector<\/span>\r\nConsole.WriteLine(<span class=\"hljs-string\">$\"Generated embedding with <span class=\"hljs-subst\">{embedding.Length}<\/span> dimensions\"<\/span>);\r\n<\/code><\/pre>\n<h3 id=\"after-using-iembeddinggenerator\" class=\"code-line\" dir=\"auto\" data-line=\"54\">After: Using IEmbeddingGenerator<\/h3>\n<pre><code class=\"code-line language-csharp\" dir=\"auto\" data-line=\"56\"><span class=\"hljs-keyword\">using<\/span> Microsoft.Extensions.AI;\r\n<span class=\"hljs-keyword\">using<\/span> Microsoft.SemanticKernel;\r\n\r\n<span class=\"hljs-comment\">\/\/ Create a kernel builder<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> builder = Kernel.CreateBuilder();\r\n\r\n<span class=\"hljs-comment\">\/\/ Add the OpenAI embedding generator<\/span>\r\nbuilder.Services.AddOpenAIEmbeddingGenerator(\r\n    modelId: <span class=\"hljs-string\">\"text-embedding-ada-002\"<\/span>,\r\n    apiKey: \"your-api-key\");\r\n\r\n<span class=\"hljs-comment\">\/\/ Build the kernel<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> kernel = builder.Build();\r\n\r\n<span class=\"hljs-comment\">\/\/ Get the embedding generator from the kernel<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> embeddingGenerator = kernel.GetRequiredService&lt;IEmbeddingGenerator&lt;<span class=\"hljs-built_in\">string<\/span>, Embedding&lt;<span class=\"hljs-built_in\">float<\/span>&gt;&gt;&gt;();\r\n\r\n<span class=\"hljs-comment\">\/\/ Generate embeddings<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> text = <span class=\"hljs-string\">\"Semantic Kernel is a lightweight SDK that integrates Large Language Models (LLMs) with conventional programming languages.\"<\/span>;\r\n<span class=\"hljs-keyword\">var<\/span> embedding = <span class=\"hljs-keyword\">await<\/span> embeddingGenerator.GenerateAsync(text);\r\n\r\n<span class=\"hljs-comment\">\/\/ Work with the embedding vector<\/span>\r\nConsole.WriteLine(<span class=\"hljs-string\">$\"Generated embedding with <span class=\"hljs-subst\">{embedding.Vector.Length}<\/span> dimensions\"<\/span>);\r\n<\/code><\/pre>\n<h2 id=\"key-differences\" class=\"code-line\" dir=\"auto\" data-line=\"83\">Key Differences<\/h2>\n<ol class=\"code-line\" dir=\"auto\" data-line=\"85\">\n<li class=\"code-line\" dir=\"auto\" data-line=\"85\"><strong>Method Names<\/strong>:\u00a0<code>GenerateEmbeddingsAsync<\/code>\u00a0becomes\u00a0<code>GenerateAsync<\/code><\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"86\"><strong>Return Type<\/strong>: Instead of returning\u00a0<code>IList&lt;ReadOnlyMemory&lt;float&gt;&gt;<\/code>, the new interface returns\u00a0<code>GeneratedEmbeddings&lt;Embedding&lt;float&gt;&gt;<\/code><\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"87\"><strong>Options<\/strong>: The new interface accepts an optional\u00a0<code>EmbeddingGenerationOptions<\/code>\u00a0parameter for more control<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"88\"><strong>Dimensions<\/strong>: Setting dimensions is now handled through options rather than attributes<\/li>\n<\/ol>\n<h2 id=\"migrating-your-code\" class=\"code-line\" dir=\"auto\" data-line=\"90\">Migrating Your Code<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"92\">If you&#8217;re currently using\u00a0<code>ITextEmbeddingGenerationService<\/code>, here&#8217;s how to migrate:<\/p>\n<ol class=\"code-line\" dir=\"auto\" data-line=\"94\">\n<li class=\"code-line\" dir=\"auto\" data-line=\"94\">Update your package references to include the latest Semantic Kernel packages<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"95\">Replace\u00a0<code>ITextEmbeddingGenerationService<\/code>\u00a0with\u00a0<code>IEmbeddingGenerator&lt;string, Embedding&lt;float&gt;&gt;<\/code><\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"96\">Update service registration to use the new embedding generator classes (e.g.,\u00a0<code>OpenAIEmbeddingGenerator<\/code>\u00a0instead of\u00a0<code>OpenAITextEmbeddingGenerationService<\/code>)<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"97\">Update method calls from\u00a0<code>GenerateEmbeddingsAsync<\/code>\u00a0to\u00a0<code>GenerateAsync<\/code><\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"98\">Update how you access the embedding vectors (now through the\u00a0<code>.Vector<\/code>\u00a0property)<\/li>\n<\/ol>\n<h2 id=\"transitional-support\" class=\"code-line\" dir=\"auto\" data-line=\"100\">Transitional Support<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"102\">To ease the transition, we&#8217;ve provided extension methods that allow you to convert between the old and new interfaces:<\/p>\n<pre><code class=\"code-line language-csharp\" dir=\"auto\" data-line=\"104\"><span class=\"hljs-keyword\">using<\/span> Microsoft.Extensions.AI;\r\n<span class=\"hljs-keyword\">using<\/span> Microsoft.SemanticKernel;\r\n<span class=\"hljs-keyword\">using<\/span> Microsoft.SemanticKernel.Embeddings;\r\n\r\n<span class=\"hljs-comment\">\/\/ Create a kernel with both old and new embedding services<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> builder = Kernel.CreateBuilder();\r\n\r\n<span class=\"hljs-comment\">\/\/ Add the old OpenAI embedding service<\/span>\r\nbuilder.Services.AddOpenAITextEmbeddingGeneration(\r\n    modelId: <span class=\"hljs-string\">\"text-embedding-ada-002\"<\/span>,\r\n    apiKey: \"your-api-key\");\r\n\r\n<span class=\"hljs-comment\">\/\/ Build the kernel<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> kernel = builder.Build();\r\n\r\n<span class=\"hljs-comment\">\/\/ Get the old embedding service<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> oldEmbeddingService = kernel.GetRequiredService&lt;ITextEmbeddingGenerationService&gt;();\r\n\r\n<span class=\"hljs-comment\">\/\/ Convert from old to new using extension method<\/span>\r\nIEmbeddingGenerator&lt;<span class=\"hljs-built_in\">string<\/span>, Embedding&lt;<span class=\"hljs-built_in\">float<\/span>&gt;&gt; newGenerator =\r\n    oldEmbeddingService.AsEmbeddingGenerator();\r\n\r\n<span class=\"hljs-comment\">\/\/ Use the new generator<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> newEmbedding = <span class=\"hljs-keyword\">await<\/span> newGenerator.GenerateAsync(<span class=\"hljs-string\">\"Converting from old to new\"<\/span>);\r\nConsole.WriteLine(<span class=\"hljs-string\">$\"Generated embedding with <span class=\"hljs-subst\">{newEmbedding.Vector.Length}<\/span> dimensions\"<\/span>);\r\n<\/code><\/pre>\n<h2 id=\"connector-support\" class=\"code-line\" dir=\"auto\" data-line=\"141\">Connector Support<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"143\">All our connectors have been updated to support the new interface:<\/p>\n<ul class=\"code-line\" dir=\"auto\" data-line=\"145\">\n<li class=\"code-line\" dir=\"auto\" data-line=\"145\">OpenAI and Azure OpenAI<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"146\">Google AI and Vertex AI<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"147\">Amazon Bedrock<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"148\">Hugging Face<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"149\">MistralAI<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"150\">And more&#8230;<\/li>\n<\/ul>\n<p class=\"code-line\" dir=\"auto\" data-line=\"152\">Each connector now provides both the legacy service (marked as obsolete) and the new generator implementation.<\/p>\n<h2 id=\"conclusion\" class=\"code-line\" dir=\"auto\" data-line=\"154\">Conclusion<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"156\">The transition to Microsoft.Extensions.AI represents an important step in the maturation of Semantic Kernel. By aligning with broader .NET ecosystem patterns, we&#8217;re making it easier for developers to integrate AI capabilities into their applications with a consistent, type-safe approach.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"158\">We encourage all Semantic Kernel users to migrate to the new\u00a0<code>IEmbeddingGenerator&lt;string, Embedding&lt;float&gt;&gt;<\/code>\u00a0interface as soon as possible. The old interface will continue to work for now but is marked as obsolete and will be removed in a future release.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"160\">For more information about Microsoft.Extensions.AI, check out the\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/microsoft-extensions-ai-simplifying-ai-integration-for-net-partners\/\" data-href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/microsoft-extensions-ai-simplifying-ai-integration-for-net-partners\/\">official announcement<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As Semantic Kernel shifts its foundational abstractions to Microsoft.Extensions.AI, we are obsoleting and moving away from our experimental embeddings interfaces to the new standardized abstractions that provide a more consistent and powerful way to work with AI services across the .NET ecosystem. The Evolution of Embedding Generation in Semantic Kernel Semantic Kernel has always aimed [&hellip;]<\/p>\n","protected":false},"author":63983,"featured_media":4856,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[78,47,25,2,1,19],"tags":[79,48,31,6,63,140,9],"class_list":["post-4853","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","category-announcement","category-ecosystem","category-samples","category-semantic-kernel","category-vector-database","tag-net","tag-ai","tag-c","tag-embeddings","tag-microsoft-semantic-kernel","tag-rag","tag-semantic-kernel"],"acf":[],"blog_post_summary":"<p>As Semantic Kernel shifts its foundational abstractions to Microsoft.Extensions.AI, we are obsoleting and moving away from our experimental embeddings interfaces to the new standardized abstractions that provide a more consistent and powerful way to work with AI services across the .NET ecosystem. The Evolution of Embedding Generation in Semantic Kernel Semantic Kernel has always aimed [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/4853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/63983"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=4853"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/4853\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/4856"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=4853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=4853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=4853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}