alibaba/spring-ai-alibaba · error · IllegalArgumentException

模型配置不能为空

Error message

模型配置不能为空

What it means

An IllegalArgumentException thrown by ModelConfigParser.parseModelConfig when the modelConfigJson argument is null, empty, or blank (StringUtils.hasText fails). The parser requires a non-empty JSON string describing the model configuration before it attempts deserialization.

Source

Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/utils/ModelConfigParser.java:38

@Component
@RequiredArgsConstructor
public class ModelConfigParser {
    
    private final ObjectMapper objectMapper;
    
    private final ModelConfigRepository modelConfigRepository;
    
    private final ModelManager modelManager;
    
    /**
     * 解析模型配置JSON字符串
     *
     * @param modelConfigJson 模型配置JSON
     * @return 模型配置信息
     */
    public ModelConfigInfo parseModelConfig(String modelConfigJson) {
        if (!StringUtils.hasText(modelConfigJson)) {
            throw new IllegalArgumentException("模型配置不能为空");
        }
        
        try {
            return objectMapper.readValue(modelConfigJson, ModelConfigInfo.class);
        } catch (Exception e) {
            log.error("解析模型配置JSON失败: {}", modelConfigJson, e);
            throw new IllegalArgumentException("模型配置格式错误: " + e.getMessage(), e);
        }
    }
    
    public ModelConfigInfo checkAndGetModelConfigInfo(String modelConfig) {
        ModelConfigInfo modelConfigInfo = null;
        try {
            modelConfigInfo = parseModelConfig(modelConfig);
            validateModelConfig(modelConfigInfo);
            
            // 验证模型配置是否存在
            // 首先尝试从 ModelConfigRepository (YAML 文件) 查找

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Ensure the caller supplies a valid modelConfig JSON before invoking the parser (null-check and reject early).
  2. Backfill missing model_config values in the database for affected records.
  3. Wrap the call in checkAndGetModelConfigInfo or your own guard that skips/default-handles empty configs.
  4. Return a clear validation error to the upstream request instead of letting it reach the parser.

Example fix

// before
ModelConfigInfo info = parser.parseModelConfig(request.getModelConfig()); // NPE risk / IAE if empty
// after
if (StringUtils.hasText(request.getModelConfig())) {
    ModelConfigInfo info = parser.parseModelConfig(request.getModelConfig());
}
Defensive patterns

Strategy: validation

Validate before calling

if (modelConfigJson == null || modelConfigJson.isBlank()) throw new BadRequestException("modelConfig is required");

Type guard

boolean hasModelConfig = modelConfigJson != null && !modelConfigJson.isBlank();

Try / catch

try { return parser.parseModelConfig(cfg); } catch (IllegalArgumentException e) { if (!StringUtils.hasText(cfg)) return defaultConfig(); throw e; }

Prevention

When it happens

Trigger: Calling parseModelConfig(modelConfigJson) with null/empty/whitespace string — typically when the persisted prompt or agent record has no modelConfig column value, or the caller passes an unset variable.

Common situations: Legacy rows created before modelConfig was mandatory; prompts created via a UI path that skipped model selection; a migration that left the column NULL; passing request.getModelConfig() without null-checking.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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