OrchardCMS/OrchardCore · error · InvalidOperationException

Unable to create a safe index prefix for AI Search…

Error message

Unable to create a safe index prefix for AI Search. Attempted to created a safe name using '{safePrefix}'.

What it means

Azure AI Search index names must be lowercase alphanumeric with hyphens, so the index name provider sanitizes the tenant/prefix using AzureAISearchIndexNamingHelper.TryGetSafePrefix. When no safe prefix can be derived (e.g., empty or entirely invalid input), this InvalidOperationException is thrown.

Solutions

  1. Set a valid, non-empty index prefix (lowercase letters, digits, hyphens) in the Azure AI Search settings
  2. Fix the tenant name used to derive the prefix so it sanitizes to a non-empty safe string
  3. Provide an explicit index name prefix in configuration instead of relying on auto-derivation

Example fix

// before
prefix: "My Tenant!"
// after
prefix: "my-tenant"
Defensive patterns

Strategy: validation

Validate before calling

bool prefixOk = !string.IsNullOrWhiteSpace(prefix) && AzureAISearchIndexNamingHelper.TryGetSafePrefix(prefix, out _);

Type guard

bool hasSafePrefix = AzureAISearchIndexNamingHelper.TryGetSafePrefix(prefix, out var safe) && safe.Length > 0;

Try / catch

try { prefix = GetIndexPrefix(tenant); } catch (InvalidOperationException ex) when (ex.Message.Contains("safe index prefix")) { /* fall back to a configured default prefix */ }

Prevention

When it happens

Trigger: Calling GetIndexPrefix (via GetFullIndexName) with a prefix that yields an empty or invalid safe name — typically an empty, null, or all-invalid-character prefix.

Common situations: Tenant names or configured index prefixes containing characters invalid for Azure AI Search; missing prefix configuration on a new tenant; whitespace-only prefix values in settings.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/1ba4847b086407b8. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.AzureAI.Core/Services/AzureAISearchIndexNameProvider.cs:43

        return GetIndexPrefix() + '-' + indexName;
    }

    private string GetIndexPrefix()
    {
        var prefix = _shellSettings.Name.ToLowerInvariant();
        var azureAIOptions = _azureAIOptions.CurrentValue;

        if (!string.IsNullOrWhiteSpace(azureAIOptions.IndexesPrefix))
        {
            prefix = $"{azureAIOptions.IndexesPrefix.ToLowerInvariant()}-{prefix}";
        }

        if (AzureAISearchIndexNamingHelper.TryGetSafePrefix(prefix, out var safePrefix))
        {
            return safePrefix;
        }

        throw new InvalidOperationException($"Unable to create a safe index prefix for AI Search. Attempted to created a safe name using '{safePrefix}'.");
    }
}

View on GitHub (pinned to 4306c0717f)