spring-projects/spring-ai · error · IllegalArgumentException

Unknown model provider:

Error message

Unknown model provider: 

What it means

calculateBaseUrl switches over the configured ModelProvider; if the value is not one of the handled providers (OpenAI, GitHub Models, Microsoft Foundry), it throws IllegalArgumentException("Unknown model provider: " + modelProvider). This guards against an unrecognized or future/unknown enum value reaching the URL resolution logic.

Source

Thrown at models/spring-ai-openai/src/main/java/org/springframework/ai/openai/setup/OpenAiSetup.java:334

			}
			if (baseUrl.startsWith(GITHUB_MODELS_URL)) {
				// To support GitHub Models for specific orgs
				return baseUrl;
			}
			return GITHUB_MODELS_URL;
		}
		else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY) {
			if (baseUrl == null || baseUrl.isBlank()) {
				throw new IllegalArgumentException("Base URL must be provided for Microsoft Foundry.");
			}
			String tmpUrl = baseUrl;
			if (baseUrl.endsWith("/") || baseUrl.endsWith("?")) {
				tmpUrl = baseUrl.substring(0, baseUrl.length() - 1);
			}
			return tmpUrl;
		}
		else {
			throw new IllegalArgumentException("Unknown model provider: " + modelProvider);
		}
	}

	static Credential azureAuthentication() {
		try {
			return AzureInternalOpenAiHelper.getAzureCredential();
		}
		catch (NoClassDefFoundError e) {
			throw new IllegalArgumentException("Microsoft Foundry was detected, but no credential was provided. "
					+ "If you want to use passwordless authentication, you need to add the Azure Identity library (groupId=`com.azure`, artifactId=`azure-identity`) to your classpath.");
		}
	}

	static @Nullable String detectApiKey(ModelProvider modelProvider) {
		if (modelProvider == ModelProvider.OPEN_AI && System.getenv(OPENAI_API_KEY) != null) {
			return System.getenv(OPENAI_API_KEY);
		}
		else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY && System.getenv(MICROSOFT_FOUNDRY_API_KEY) != null) {

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Check which ModelProvider value is being configured and use one supported by your spring-ai-openai version (OPEN_AI, GITHUB_MODELS, MICROSOFT_FOUNDRY).
  2. Upgrade spring-ai-openai if you intend to use a newer provider constant that your current version does not recognize.
  3. Log or inspect the resolved ModelProvider at startup to catch bad property binding early.

Example fix

// before
OpenAiApi.builder().modelProvider(myProvider).build(); // myProvider = unknown value
// after
OpenAiApi.builder()
    .modelProvider(ModelProvider.OPEN_AI) // or GITHUB_MODELS / MICROSOFT_FOUNDRY
    .build();
Defensive patterns

Strategy: validation

Validate before calling

EnumSet<ModelProvider> supported = EnumSet.of(ModelProvider.OPEN_AI, ModelProvider.GITHUB_MODELS, ModelProvider.MICROSOFT_FOUNDRY);
if (!supported.contains(modelProvider)) {
    throw new IllegalArgumentException("Unsupported provider: " + modelProvider);
}

Prevention

When it happens

Trigger: Passing a ModelProvider value not handled by calculateBaseUrl (e.g. a newly added enum constant used with an older library version, or a custom/null value) into OpenAiSetup.calculatedBaseUrl.

Common situations: Version mismatch where configuration supplies a provider enum value the library build does not know; programmatic construction of OpenAiApi with a wrong or partially initialized ModelProvider; binding a raw string config value into an unexpected enum state.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/49e9b1a94a5af220. Report an issue: GitHub.