Microsoft.Extensions.AI.OpenAI 9.4.4-preview.1.25259.16

Prefix Reserved
This is a prerelease version of Microsoft.Extensions.AI.OpenAI.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Microsoft.Extensions.AI.OpenAI --version 9.4.4-preview.1.25259.16
                    
NuGet\Install-Package Microsoft.Extensions.AI.OpenAI -Version 9.4.4-preview.1.25259.16
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.4.4-preview.1.25259.16" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="9.4.4-preview.1.25259.16" />
                    
Directory.Packages.props
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Microsoft.Extensions.AI.OpenAI --version 9.4.4-preview.1.25259.16
                    
#r "nuget: Microsoft.Extensions.AI.OpenAI, 9.4.4-preview.1.25259.16"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Microsoft.Extensions.AI.OpenAI@9.4.4-preview.1.25259.16
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Microsoft.Extensions.AI.OpenAI&version=9.4.4-preview.1.25259.16&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Microsoft.Extensions.AI.OpenAI&version=9.4.4-preview.1.25259.16&prerelease
                    
Install as a Cake Tool

Microsoft.Extensions.AI.OpenAI

Provides an implementation of the IChatClient interface for the OpenAI package and OpenAI-compatible endpoints.

Install the package

From the command-line:

dotnet add package Microsoft.Extensions.AI.OpenAI

Or directly in the C# project file:

<ItemGroup>
  <PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="[CURRENTVERSION]" />
</ItemGroup>

Usage Examples

Chat

using Microsoft.Extensions.AI;

IChatClient client =
    new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
    .AsIChatClient();

Console.WriteLine(await client.GetResponseAsync("What is AI?"));

Chat + Conversation History

using Microsoft.Extensions.AI;

IChatClient client =
    new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
    .AsIChatClient();

Console.WriteLine(await client.GetResponseAsync(
[
    new ChatMessage(ChatRole.System, "You are a helpful AI assistant"),
    new ChatMessage(ChatRole.User, "What is AI?"),
]));

Chat streaming

using Microsoft.Extensions.AI;

IChatClient client =
    new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
    .AsIChatClient();

await foreach (var update in client.GetStreamingResponseAsync("What is AI?"))
{
    Console.Write(update);
}

Tool calling

using System.ComponentModel;
using Microsoft.Extensions.AI;

IChatClient openaiClient =
    new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
    .AsIChatClient();

IChatClient client = new ChatClientBuilder(openaiClient)
    .UseFunctionInvocation()
    .Build();

ChatOptions chatOptions = new()
{
    Tools = [AIFunctionFactory.Create(GetWeather)]
};

await foreach (var message in client.GetStreamingResponseAsync("Do I need an umbrella?", chatOptions))
{
    Console.Write(message);
}

[Description("Gets the weather")]
static string GetWeather() => Random.Shared.NextDouble() > 0.5 ? "It's sunny" : "It's raining";

Caching

using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;

IDistributedCache cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));

IChatClient openaiClient =
    new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
    .AsIChatClient();

IChatClient client = new ChatClientBuilder(openaiClient)
    .UseDistributedCache(cache)
    .Build();

for (int i = 0; i < 3; i++)
{
    await foreach (var message in client.GetStreamingResponseAsync("In less than 100 words, what is AI?"))
    {
        Console.Write(message);
    }

    Console.WriteLine();
    Console.WriteLine();
}

Telemetry

using Microsoft.Extensions.AI;
using OpenTelemetry.Trace;

// Configure OpenTelemetry exporter
var sourceName = Guid.NewGuid().ToString();
var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
    .AddSource(sourceName)
    .AddConsoleExporter()
    .Build();

IChatClient openaiClient =
    new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
    .AsIChatClient();

IChatClient client = new ChatClientBuilder(openaiClient)
    .UseOpenTelemetry(sourceName: sourceName, configure: c => c.EnableSensitiveData = true)
    .Build();

Console.WriteLine(await client.GetResponseAsync("What is AI?"));

Telemetry, Caching, and Tool Calling

using System.ComponentModel;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using OpenTelemetry.Trace;

// Configure telemetry
var sourceName = Guid.NewGuid().ToString();
var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
    .AddSource(sourceName)
    .AddConsoleExporter()
    .Build();

// Configure caching
IDistributedCache cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));

// Configure tool calling
var chatOptions = new ChatOptions
{
    Tools = [AIFunctionFactory.Create(GetPersonAge)]
};

IChatClient openaiClient =
    new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
    .AsIChatClient();

IChatClient client = new ChatClientBuilder(openaiClient)
    .UseDistributedCache(cache)
    .UseFunctionInvocation()
    .UseOpenTelemetry(sourceName: sourceName, configure: c => c.EnableSensitiveData = true)
    .Build();

for (int i = 0; i < 3; i++)
{
    Console.WriteLine(await client.GetResponseAsync("How much older is Alice than Bob?", chatOptions));
}

[Description("Gets the age of a person specified by name.")]
static int GetPersonAge(string personName) =>
    personName switch
    {
        "Alice" => 42,
        "Bob" => 35,
        _ => 26,
    };

Text embedding generation

using Microsoft.Extensions.AI;

IEmbeddingGenerator<string, Embedding<float>> generator =
    new OpenAI.Embeddings.EmbeddingClient("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
    .AsIEmbeddingGenerator();

var embeddings = await generator.GenerateAsync("What is AI?");

Console.WriteLine(string.Join(", ", embeddings[0].Vector.ToArray()));

Text embedding generation with caching

using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;

IDistributedCache cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));

IEmbeddingGenerator<string, Embedding<float>> openAIGenerator =
    new OpenAI.Embeddings.EmbeddingClient("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
    .AsIEmbeddingGenerator();

IEmbeddingGenerator<string, Embedding<float>> generator = new EmbeddingGeneratorBuilder<string, Embedding<float>>(openAIGenerator)
    .UseDistributedCache(cache)
    .Build();

foreach (var prompt in new[] { "What is AI?", "What is .NET?", "What is AI?" })
{
    var embeddings = await generator.GenerateAsync(prompt);

    Console.WriteLine(string.Join(", ", embeddings[0].Vector.ToArray()));
}

Dependency Injection

using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

// App Setup
var builder = Host.CreateApplicationBuilder();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace));

builder.Services.AddChatClient(services =>
    new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY")).AsIChatClient())
    .UseDistributedCache()
    .UseLogging();

var app = builder.Build();

// Elsewhere in the app
var chatClient = app.Services.GetRequiredService<IChatClient>();
Console.WriteLine(await chatClient.GetResponseAsync("What is AI?"));

Minimal Web API

using Microsoft.Extensions.AI;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddChatClient(services =>
    new OpenAI.Chat.ChatClient("gpt-4o-mini", builder.Configuration["OPENAI_API_KEY"]).AsIChatClient());

builder.Services.AddEmbeddingGenerator(services =>
    new OpenAI.Embeddings.EmbeddingClient("text-embedding-3-small", builder.Configuration["OPENAI_API_KEY"]).AsIEmbeddingGenerator());

var app = builder.Build();

app.MapPost("/chat", async (IChatClient client, string message) =>
{
    var response = await client.GetResponseAsync(message);
    return response.Message;
});

app.MapPost("/embedding", async (IEmbeddingGenerator<string, Embedding<float>> client, string message) =>
{
    var response = await client.GenerateAsync(message);
    return response[0].Vector;
});

app.Run();

Feedback & Contributing

We welcome feedback and contributions in our GitHub repo.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (31)

Showing the top 5 NuGet packages that depend on Microsoft.Extensions.AI.OpenAI:

Package Downloads
Microsoft.SemanticKernel.Connectors.OpenAI

Semantic Kernel connectors for OpenAI. Contains clients for chat completion, embedding and DALL-E text to image.

Aspire.Azure.AI.OpenAI

A client for Azure OpenAI that integrates with Aspire, including logging and telemetry.

Aspire.OpenAI

A client for OpenAI that integrates with Aspire, including metrics and telemetry.

Weavy.Core

A class library containing core business logic, data access and utility methods required by Weavy.

SemanticValidation

A library to validate strings semantically using OpenAI

GitHub repositories (29)

Showing the top 20 popular GitHub repositories that depend on Microsoft.Extensions.AI.OpenAI:

Repository Stars
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
dotnet/aspire
Tools, templates, and packages to accelerate building observable, production-ready apps
dotnet/maui-samples
Samples for .NET Multi-Platform App UI (.NET MAUI)
modelcontextprotocol/csharp-sdk
The official C# SDK for Model Context Protocol servers and clients. Maintained in collaboration with Microsoft.
microsoft/Generative-AI-for-beginners-dotnet
Five lessons, learn how to really apply AI to your .NET Applications
abpframework/abp-samples
Sample solutions built with the ABP Framework
bitfoundation/bitplatform
Build all of your apps using what you already know and love ❤️
microsoft/ai-dev-gallery
An open-source project for Windows developers to learn how to add AI with local models and APIs to Windows apps.
tryAGI/LangChain
C# implementation of LangChain. We try to be as close to the original as possible in terms of abstractions, but are open to new entities.
getcellm/cellm
Use LLMs in Excel formulas
VladislavAntonyuk/MauiSamples
.NET MAUI Samples
dotnet/ai-samples
PowerShell/AIShell
An interactive shell to work with AI-powered assistance providers
DevExpress/Blazor
DevExpress UI for Blazor
Deali-Axy/StarBlog
☀StarBlog 是一个基于 .NET Core 开发的现代博客系统,支持 Markdown 文章导入,遵循 RESTful 接口规范。前端基于 Vue + ElementUI 开发,可作为 .NET Core 入门学习项目,同时配套了一系列开发笔记,记录了从零开始构建这个博客系统的全过程,可以帮助学习理解 .Net Core 项目的开发流程。
thangchung/practical-dotnet-aspire
The practical .NET Aspire builds on the coffeeshop app business domain
axzxs2001/Asp.NetCoreExperiment
原来所有项目都移动到**OleVersion**目录下进行保留。新的案例装以.net 5.0为主,一部分对以前案例进行升级,一部分将以前的工作经验总结出来,以供大家参考!
syncfusion/wpf-demos
This repository contains the samples for Syncfusion WPF UI Controls and File Format libraries and the guide to use them.
PederHP/mcpdotnet
.NET implementation of the Model Context Protocol (MCP)
syncfusion/maui-demos
This repository contains the Syncfusion .NET MAUI control’s samples and the guide to use them
Version Downloads Last Updated
9.8.0-preview.1.25412.6 89,706 8/12/2025
9.7.1-preview.1.25365.4 153,617 7/15/2025
9.7.0-preview.1.25356.2 17,778 7/8/2025
9.6.0-preview.1.25310.2 47,952 6/10/2025
9.5.0-preview.1.25265.7 583,453 5/16/2025
9.5.0-preview.1.25262.9 6,032 5/13/2025
9.4.4-preview.1.25259.16 7,118 5/10/2025
9.4.3-preview.1.25230.7 27,640 5/1/2025
9.4.0-preview.1.25207.5 76,533 4/8/2025
9.3.0-preview.1.25161.3 100,690 3/11/2025
9.3.0-preview.1.25114.11 92,750 2/16/2025
9.1.0-preview.1.25064.3 50,423 1/14/2025
9.0.1-preview.1.24570.5 82,789 11/21/2024
9.0.0-preview.9.24556.5 51,193 11/12/2024
9.0.0-preview.9.24525.1 43,929 10/26/2024
9.0.0-preview.9.24507.7 14,649 10/8/2024