{"record":{"id":"621b69e2ba6299d5","repo":"alibaba/spring-ai-alibaba","slug":"invalid-params-621b69","errorCode":"INVALID_PARAMS","errorMessage":"api key is invalid.","messagePattern":"api key is invalid\\.","errorType":"error_code","errorClass":"BizException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/model/llm/ModelFactory.java","lineNumber":152,"sourceCode":"\t * @param modelConfig The model configuration\n\t * @return Cache key string\n\t */\n\tprivate String getModelInstanceKey(ModelConfigInfo modelConfig) {\n\t\treturn modelConfig.getProvider() + \":\" + modelConfig.getModelId();\n\t}\n\n\t/**\n\t * Retrieves model credentials for the specified provider and model\n\t * @param provider The provider name\n\t * @param modelId The model identifier\n\t * @return ModelCredential instance\n\t */\n\tprivate ModelCredential getModelCredential(String provider, String modelId) {\n\t\tProviderConfigInfo providerDetail = providerManager.getProviderDetail(provider, false);\n\t\tModelCredential credential = providerDetail.getCredential();\n\n\t\tif (StringUtils.isBlank(credential.getApiKey())) {\n\t\t\tthrow new BizException(ErrorCode.INVALID_PARAMS.toError(\"apikey\", \"api key is invalid.\"));\n\t\t}\n\n\t\treturn credential;\n\t}\n\n\t/**\n\t * Builds an OpenAI API instance with the provided credentials\n\t * @param credential The model credentials\n\t * @return OpenAiApi instance\n\t */\n\tprivate OpenAiApi buildOpenAiApi(ModelCredential credential) {\n\t\tOpenAiApi.Builder openAiApiBuilder = OpenAiApi.builder()\n\t\t\t.apiKey(credential.getApiKey())\n\t\t\t.responseErrorHandler(ErrorHandlerUtils.OPENAI_RESPONSE_ERROR_HANDLER)\n\t\t\t.headers(ApiUtils.getBaseHeaders());\n\t\tif (StringUtils.isNotBlank(credential.getEndpoint())) {\n\t\t\tString endpoint = credential.getEndpoint();\n","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/model/llm/ModelFactory.java#L134-L170","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set a valid API key for the provider in the studio/admin provider configuration and save.","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.","Check the credential with providerManager.getProviderDetail(provider, false).getCredential().getApiKey() before invoking the model to fail fast with a clearer message."],"exampleFix":"// before (config)\nprovider.dashscope.credential.api-key=\n// after\nprovider.dashscope.credential.api-key=sk-xxxxxxxxxxxxxxxx","handlingStrategy":"validation","validationCode":"// Java\nModelCredential c = providerManager.getProviderDetail(provider, false).getCredential();\nif (c == null || StringUtils.isBlank(c.getApiKey())) {\n    throw new IllegalStateException(\"API key for provider \" + provider + \" is not configured\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    ModelCredential c = modelFactory.credential(provider, modelId);\n} catch (BizException e) {\n    if (e.getMessage().contains(\"api key is invalid\")) {\n        // redirect user to provider settings to configure the key\n    }\n}","preventionTips":["Run a startup health check that validates all configured providers have non-blank API keys.","Ensure API keys are injected via env/secrets manager, not manually saved config.","Never import/export provider config with redacted or empty keys into production."],"tags":["api-key","credentials","llm"],"backgroundTag":"missing-api-key","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}