microsoft/semantic-kernel · error · ArgumentException

The {nameof(httpClient)}.{nameof(HttpClient.BaseAddress)} an

Error message

The {nameof(httpClient)}.{nameof(HttpClient.BaseAddress)} and {nameof(endpoint)} are both null or empty. Please ensure at least one is provided.

What it means

Thrown by the AzureOpenAITextToImageService constructor (apiKey overload) when both the endpoint string parameter and httpClient.BaseAddress are null or empty. The constructor resolves the final endpoint as endpoint ?? httpClient?.BaseAddress?.AbsoluteUri; if the result is null, there is no target URI for the Azure OpenAI service and construction cannot proceed.

Source

Thrown at dotnet/src/Connectors/Connectors.AzureOpenAI/Services/AzureOpenAITextToImageService.cs:53

    /// <param name="modelId">Azure OpenAI model id, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource</param>
    /// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
    /// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to use for logging. If null, no logging will be performed.</param>
    /// <param name="apiVersion">Azure OpenAI service API version, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param>
    public AzureOpenAITextToImageService(
        string deploymentName,
        string endpoint,
        string apiKey,
        string? modelId,
        HttpClient? httpClient = null,
        ILoggerFactory? loggerFactory = null,
        string? apiVersion = null)
    {
        Verify.NotNullOrWhiteSpace(apiKey);

        var connectorEndpoint = !string.IsNullOrWhiteSpace(endpoint) ? endpoint! : httpClient?.BaseAddress?.AbsoluteUri;
        if (connectorEndpoint is null)
        {
            throw new ArgumentException($"The {nameof(httpClient)}.{nameof(HttpClient.BaseAddress)} and {nameof(endpoint)} are both null or empty. Please ensure at least one is provided.");
        }

        var options = AzureClientCore.GetAzureOpenAIClientOptions(httpClient, apiVersion); // DALL-E 3 is supported in the latest API releases - https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#image-generation

        var azureOpenAIClient = new AzureOpenAIClient(new Uri(connectorEndpoint), new ApiKeyCredential(apiKey), options);

        this._client = new(deploymentName, azureOpenAIClient, loggerFactory?.CreateLogger(this.GetType()));

        if (modelId is not null)
        {
            this._client.AddAttribute(AIServiceExtensions.ModelIdKey, modelId);
        }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="AzureOpenAITextToImageService"/> class.
    /// </summary>
    /// <param name="deploymentName">Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource</param>

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Provide a non-empty endpoint string: new AzureOpenAITextToImageService("deployment", "https://myresource.openai.azure.com/", apiKey).
  2. If using a shared HttpClient, set its BaseAddress: httpClient.BaseAddress = new Uri("https://myresource.openai.azure.com/").
  3. Read the endpoint from configuration with a fallback or validation: var endpoint = config["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("Endpoint not configured").
  4. Ensure the endpoint string is the Azure OpenAI resource base URL (e.g. https://<resource>.openai.azure.com/), not just the resource name.

Example fix

// before — endpoint is null, httpClient has no BaseAddress
var svc = new AzureOpenAITextToImageService(
    "dalle3", endpoint: null, apiKey, httpClient: client);

// after — pass the endpoint explicitly
var svc = new AzureOpenAITextToImageService(
    "dalle3", "https://myresource.openai.azure.com/", apiKey);
Defensive patterns

Strategy: validation

Validate before calling

string endpoint = configEndpoint;
if (string.IsNullOrWhiteSpace(endpoint))
{
    endpoint = httpClient?.BaseAddress?.AbsoluteUri;
}
if (string.IsNullOrWhiteSpace(endpoint))
{
    throw new InvalidOperationException(
        "Azure OpenAI endpoint must be provided via the 'endpoint' parameter or HttpClient.BaseAddress.");
}
// safe to construct
var service = new AzureOpenAITextToImageService(deployment, endpoint, apiKey, httpClient);

Type guard

static bool HasValidEndpoint(string? endpoint, HttpClient? http) =>
    !string.IsNullOrWhiteSpace(endpoint) ||
    (http?.BaseAddress?.AbsoluteUri is not null && !string.IsNullOrWhiteSpace(http.BaseAddress.AbsoluteUri));

Try / catch

try
{
    var svc = new AzureOpenAITextToImageService(deploy, endpoint, apiKey, httpClient);
}
catch (ArgumentException ex) when (ex.Message.Contains("both null or empty"))
{
    throw new ConfigurationException("AzureOpenAI:Endpoint is missing. Configure it in appsettings or environment.", ex);
}

Prevention

When it happens

Trigger: Calling the constructor with endpoint set to null, empty string, or whitespace AND either httpClient is null or httpClient.BaseAddress is null. The Verify.NotNullOrWhiteSpace(apiKey) check passes first, so apiKey is valid but the endpoint resolution fails.

Common situations: Reading endpoint from configuration/environment that is not set (e.g. Environment.GetEnvironmentVariable returns null). Passing endpoint as null and relying on a shared HttpClient that was created without setting BaseAddress. Misconfigured dependency injection where the endpoint binding is missing.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/1513ff2295fc2ca7. Report an issue: GitHub.