Aspire.OpenAI
13.0.0-preview.1.25560.3
Prefix Reserved
See the version list below for details.
dotnet add package Aspire.OpenAI --version 13.0.0-preview.1.25560.3
NuGet\Install-Package Aspire.OpenAI -Version 13.0.0-preview.1.25560.3
<PackageReference Include="Aspire.OpenAI" Version="13.0.0-preview.1.25560.3" />
<PackageVersion Include="Aspire.OpenAI" Version="13.0.0-preview.1.25560.3" />
<PackageReference Include="Aspire.OpenAI" />
paket add Aspire.OpenAI --version 13.0.0-preview.1.25560.3
#r "nuget: Aspire.OpenAI, 13.0.0-preview.1.25560.3"
#:package Aspire.OpenAI@13.0.0-preview.1.25560.3
#addin nuget:?package=Aspire.OpenAI&version=13.0.0-preview.1.25560.3&prerelease
#tool nuget:?package=Aspire.OpenAI&version=13.0.0-preview.1.25560.3&prerelease
Aspire.OpenAI library
Registers OpenAIClient as a singleton in the DI container for using the OpenAI REST API. Enables corresponding metrics, logging and telemetry.
Getting started
Prerequisites
- An OpenAI REST API compatible service like OpenAI.com, ollama.com, and others.
- An API key.
Install the package
Install the Aspire OpenAI library with NuGet:
dotnet add package Aspire.OpenAI
Usage example
In the AppHost.cs file of your project, call the AddOpenAIClient extension method to register an OpenAIClient for use via the dependency injection container. The method takes a connection name parameter.
builder.AddOpenAIClient("openaiConnectionName");
You can then retrieve the OpenAIClient instance using dependency injection. For example, to retrieve the client from a Web API controller:
private readonly OpenAIClient _client;
public ChatController(OpenAIClient client)
{
_client = client;
}
To learn how to use the OpenAI client library refer to Using the OpenAIClient class.
Configuration
The Aspire OpenAI library provides multiple options to configure the OpenAI service based on the requirements and conventions of your project. Note that either an Endpoint or a ConnectionString is required to be supplied.
Use a connection string
A connection can be constructed from an Endpoint and a Key value with the format Endpoint={endpoint};Key={key};. If the Endpoint value is omitted the default OpenAI endpoint will be used. You can provide the name of the connection string when calling builder.AddOpenAIClient():
builder.AddOpenAIClient("openaiConnectionName");
Connection string
Alternatively, a custom connection string can be used.
{
"ConnectionStrings": {
"openaiConnectionName": "Endpoint=https://{openai_rest_api_url};Key={account_key};"
}
}
Use configuration providers
The Aspire OpenAI library supports Microsoft.Extensions.Configuration. It loads the OpenAISettings and OpenAIClientOptions from configuration by using the Aspire:OpenAI key. Example appsettings.json that configures some of the options:
{
"Aspire": {
"OpenAI": {
"DisableTracing": false,
"ClientOptions": {
"UserAgentApplicationId": "myapp"
}
}
}
}
Use inline delegates
You can also pass the Action<OpenAISettings> configureSettings delegate to set up some or all the options inline, for example to disable tracing from code:
builder.AddOpenAIClient("openaiConnectionName", settings => settings.DisableTracing = true);
You can also setup the OpenAIClientOptions using the optional Action<OpenAIClientOptions>? configureOptions parameter of the AddOpenAIClient method. For example, to set a custom NetworkTimeout value for this client:
builder.AddOpenAIClient("openaiConnectionName", configureOptions: options => options.NetworkTimeout = TimeSpan.FromSeconds(2));
AppHost extensions
There is no specific AppHost extension corresponding to the OpenAI integration. Instead a connection string resource can be registered:
In the AppHost.cs file of AppHost, add an OpenAI REST API connection string name:
var openai = builder.AddConnectionString("openai");
var myService = builder.AddProject<Projects.MyService>()
.WithReference(openai);
The AddConnectionString can be used to read connection information from the AppHost's configuration (for example, from "user secrets") under the ConnectionStrings:openai config key. The WithReference method passes that connection information into a connection string named openai in the MyService project. In the Program.cs file of MyService, the connection can be consumed using:
builder.AddOpenAIClient("openai");
Experimental Telemetry
OpenAI telemetry support is experimental, the shape of traces may change in the future without notice. It can be enabled by invoking
AppContext.SetSwitch("OpenAI.Experimental.EnableOpenTelemetry", true);
or by setting the "OPENAI_EXPERIMENTAL_ENABLE_OPEN_TELEMETRY" environment variable to "true".
Additional documentation
- https://github.com/openai/openai-dotnet
- https://github.com/dotnet/aspire/tree/main/src/Components/README.md
Feedback & contributing
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.AI (>= 10.0.0)
- Microsoft.Extensions.AI.OpenAI (>= 10.0.0-preview.1.25559.3)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Options (>= 10.0.0)
- Microsoft.Extensions.Primitives (>= 10.0.0)
- OpenAI (>= 2.6.0)
- OpenTelemetry.Extensions.Hosting (>= 1.9.0)
- System.Text.Json (>= 10.0.0)
-
net8.0
- Microsoft.Extensions.AI (>= 10.0.0)
- Microsoft.Extensions.AI.OpenAI (>= 10.0.0-preview.1.25559.3)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Options (>= 10.0.0)
- Microsoft.Extensions.Primitives (>= 10.0.0)
- OpenAI (>= 2.6.0)
- OpenTelemetry.Extensions.Hosting (>= 1.9.0)
- System.Text.Json (>= 10.0.0)
-
net9.0
- Microsoft.Extensions.AI (>= 10.0.0)
- Microsoft.Extensions.AI.OpenAI (>= 10.0.0-preview.1.25559.3)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Options (>= 10.0.0)
- Microsoft.Extensions.Primitives (>= 10.0.0)
- OpenAI (>= 2.6.0)
- OpenTelemetry.Extensions.Hosting (>= 1.9.0)
- System.Text.Json (>= 10.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Aspire.OpenAI:
| Package | Downloads |
|---|---|
|
Aspire.Azure.AI.OpenAI
A client for Azure OpenAI that integrates with Aspire, including logging and telemetry. |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Aspire.OpenAI:
| Repository | Stars |
|---|---|
|
davidfowl/aspire-ai-chat-demo
Aspire AI Chat is a full-stack chat sample that combines modern technologies to deliver a ChatGPT-like experience.
|
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 13.0.2-preview.1.25603.5 | 314 | 12/4/2025 | |
| 13.0.1-preview.1.25575.3 | 862 | 11/26/2025 | |
| 13.0.0-preview.1.25560.3 | 12,718 | 11/11/2025 | |
| 9.5.2-preview.1.25522.3 | 3,054 | 10/23/2025 | |
| 9.5.1-preview.1.25502.11 | 14,528 | 10/3/2025 | |
| 9.5.0-preview.1.25474.7 | 13,627 | 9/25/2025 | |
| 9.4.2-preview.1.25428.12 | 12,383 | 9/2/2025 | |
| 9.4.1-preview.1.25408.4 | 10,318 | 8/12/2025 | |
| 9.4.0-preview.1.25378.8 | 9,635 | 7/29/2025 | |
| 9.3.1-preview.1.25305.6 | 30,594 | 6/10/2025 | |
| 9.3.0-preview.1.25265.20 | 21,137 | 5/19/2025 | |
| 9.2.1-preview.1.25222.1 | 2,727 | 4/24/2025 | |
| 9.2.0-preview.1.25209.2 | 6,994 | 4/10/2025 | |
| 9.1.0-preview.1.25121.10 | 61,599 | 2/25/2025 | |
| 9.0.0-preview.5.24551.3 | 73,504 | 11/12/2024 | |
| 9.0.0-preview.4.24511.1 | 7,033 | 10/15/2024 |