alibaba/spring-ai-alibaba · error · BizException

INVALID_PARAMS

INVALID_PARAMS

Error message

api key is invalid.

What it means

ModelFactory.getModelCredential fetches provider detail from the ProviderManager and returns the stored ModelCredential. If the credential's apiKey is blank it throws a BizException with ErrorCode.INVALID_PARAMS (field "apikey", message "api key is invalid."). The library requires a non-empty API key for the model provider before any model call is made.

Source

Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/model/llm/ModelFactory.java:152

	 * @param modelConfig The model configuration
	 * @return Cache key string
	 */
	private String getModelInstanceKey(ModelConfigInfo modelConfig) {
		return modelConfig.getProvider() + ":" + modelConfig.getModelId();
	}

	/**
	 * Retrieves model credentials for the specified provider and model
	 * @param provider The provider name
	 * @param modelId The model identifier
	 * @return ModelCredential instance
	 */
	private ModelCredential getModelCredential(String provider, String modelId) {
		ProviderConfigInfo providerDetail = providerManager.getProviderDetail(provider, false);
		ModelCredential credential = providerDetail.getCredential();

		if (StringUtils.isBlank(credential.getApiKey())) {
			throw new BizException(ErrorCode.INVALID_PARAMS.toError("apikey", "api key is invalid."));
		}

		return credential;
	}

	/**
	 * Builds an OpenAI API instance with the provided credentials
	 * @param credential The model credentials
	 * @return OpenAiApi instance
	 */
	private OpenAiApi buildOpenAiApi(ModelCredential credential) {
		OpenAiApi.Builder openAiApiBuilder = OpenAiApi.builder()
			.apiKey(credential.getApiKey())
			.responseErrorHandler(ErrorHandlerUtils.OPENAI_RESPONSE_ERROR_HANDLER)
			.headers(ApiUtils.getBaseHeaders());
		if (StringUtils.isNotBlank(credential.getEndpoint())) {
			String endpoint = credential.getEndpoint();

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Set a valid API key for the provider in the studio/admin provider configuration and save.
  2. Verify via environment/config (e.g. DASHSCOPE_API_KEY / OPENAI_API_KEY) that the secret is actually present where the server runs, then re-register the provider.
  3. Check the credential with providerManager.getProviderDetail(provider, false).getCredential().getApiKey() before invoking the model to fail fast with a clearer message.

Example fix

// before (config)
provider.dashscope.credential.api-key=
// after
provider.dashscope.credential.api-key=sk-xxxxxxxxxxxxxxxx
Defensive patterns

Strategy: validation

Validate before calling

// Java
ModelCredential c = providerManager.getProviderDetail(provider, false).getCredential();
if (c == null || StringUtils.isBlank(c.getApiKey())) {
    throw new IllegalStateException("API key for provider " + provider + " is not configured");
}

Try / catch

try {
    ModelCredential c = modelFactory.credential(provider, modelId);
} catch (BizException e) {
    if (e.getMessage().contains("api key is invalid")) {
        // redirect user to provider settings to configure the key
    }
}

Prevention

When it happens

Trigger: Resolving a model credential (via credential() -> getModelCredential) for a provider whose registered ModelCredential has a null/empty/whitespace apiKey. Happens when a provider is configured in the platform but the API key was never saved, or was saved as empty.

Common situations: Fresh deployments where DashScope/OpenAI provider config was imported without keys; keys redacted when exporting/importing provider config; env secret not injected so the stored key is empty; key cleared from Nacos/config without re-saving.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/621b69e2ba6299d5. Report an issue: GitHub.