Skip to content
DocsTry Aspire
DocsTry

OpenAIExtensions Methods

ClassMethods5 members
Provides extension methods for adding OpenAI Model resources to the application model.
AddModel(IResourceBuilder<OpenAIResource>, string, string)Section titled AddModel(IResourceBuilder<OpenAIResource>, string, string)extensionIResourceBuilder<OpenAIModelResource>
Adds an OpenAI Model child to the provided OpenAI resource.
public static class OpenAIExtensions
{
public static IResourceBuilder<OpenAIModelResource> AddModel(
this IResourceBuilder<OpenAIResource> builder,
string name,
string model)
{
// ...
}
}
builderIResourceBuilder<OpenAIResource>The OpenAI resource builder.
namestringThe name of the model resource. This name is used as the connection string name.
modelstringThe model identifier, e.g., "gpt-4o-mini".
IResourceBuilder<OpenAIModelResource>The model resource builder.
AddOpenAI(IDistributedApplicationBuilder, string)Section titled AddOpenAI(IDistributedApplicationBuilder, string)extensionIResourceBuilder<OpenAIResource>
Adds an OpenAI parent resource that can host multiple models.
public static class OpenAIExtensions
{
public static IResourceBuilder<OpenAIResource> AddOpenAI(
this IDistributedApplicationBuilder builder,
string name)
{
// ...
}
}
builderIDistributedApplicationBuilderThe Hosting.IDistributedApplicationBuilder.
namestringThe name of the OpenAI resource.
IResourceBuilder<OpenAIResource>The OpenAI resource builder.
WithApiKey(IResourceBuilder<OpenAIResource>, IResourceBuilder<ParameterResource>)Section titled WithApiKey(IResourceBuilder<OpenAIResource>, IResourceBuilder<ParameterResource>)extensionIResourceBuilder<OpenAIResource>
Configures the API key for the OpenAI parent resource from a parameter.
public static class OpenAIExtensions
{
public static IResourceBuilder<OpenAIResource> WithApiKey(
this IResourceBuilder<OpenAIResource> builder,
IResourceBuilder<ParameterResource> apiKey)
{
// ...
}
}
builderIResourceBuilder<OpenAIResource>
apiKeyIResourceBuilder<ParameterResource>
WithEndpoint(IResourceBuilder<OpenAIResource>, string)Section titled WithEndpoint(IResourceBuilder<OpenAIResource>, string)extensionIResourceBuilder<OpenAIResource>
Sets a custom OpenAI-compatible service endpoint URI on the parent resource.
public static class OpenAIExtensions
{
public static IResourceBuilder<OpenAIResource> WithEndpoint(
this IResourceBuilder<OpenAIResource> builder,
string endpoint)
{
// ...
}
}
builderIResourceBuilder<OpenAIResource>The OpenAI parent resource builder.
endpointstringThe endpoint URI, e.g., https://mygateway.example.com/v1.
WithHealthCheck(IResourceBuilder<OpenAIModelResource>)Section titled WithHealthCheck(IResourceBuilder<OpenAIModelResource>)extensionIResourceBuilder<OpenAIModelResource>
Adds a health check to the OpenAI Model resource.
public static class OpenAIExtensions
{
public static IResourceBuilder<OpenAIModelResource> WithHealthCheck(
this IResourceBuilder<OpenAIModelResource> builder)
{
// ...
}
}
builderIResourceBuilder<OpenAIModelResource>The resource builder.
IResourceBuilder<OpenAIModelResource>The resource builder.

This method adds a health check that verifies the OpenAI endpoint is accessible, the API key is valid, and the specified model is available. The health check will:

  • Return HealthStatus.Healthy when the endpoint returns HTTP 200
  • Return HealthStatus.Unhealthy with details when the API key is invalid (HTTP 401)
  • Return HealthStatus.Unhealthy with error details when the model is unknown (HTTP 404)

Because health checks are included in the rate limit of the OpenAI API, it is recommended to use this health check sparingly, such as when you are having issues understanding the reason the model is not working as expected. Furthermore, the health check will run a single time per application instance.