alibaba/spring-ai-alibaba · error · IllegalArgumentException

模型缺少 id

Error message

模型缺少 id

What it means

FileModelConfigRepository.validateYamlModel() validates each model entry parsed from the models YAML file and throws IllegalArgumentException "模型缺少 id" ("model missing id") when the YamlModel's id field is null. Every persisted model must carry a unique Long id, since ids are collected into a duplicate-checking set afterwards.

Source

Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/repository/impl/FileModelConfigRepository.java:116

        YamlRoot root = yamlMapper.readValue(bytes, YamlRoot.class);
        if (root == null || root.models == null) {
            return new HashMap<>();
        }
        
        Map<Long, ModelConfigDO> map = new HashMap<>();
        Set<Long> ids = new HashSet<>();
        Set<String> names = new HashSet<>();
        for (YamlModel m : root.models) {
            validateYamlModel(m, ids, names);
            ModelConfigDO entity = toEntity(m);
            map.put(entity.getId(), entity);
        }
        return map;
    }
    
    private void validateYamlModel(YamlModel m, Set<Long> ids, Set<String> names) {
        if (m.id == null) {
            throw new IllegalArgumentException("模型缺少 id");
        }
        if (m.name == null || m.name.isBlank()) {
            throw new IllegalArgumentException("模型缺少 name");
        }
        if (m.provider == null || m.provider.isBlank()) {
            throw new IllegalArgumentException("模型缺少 provider");
        }
        if (m.modelName == null || m.modelName.isBlank()) {
            throw new IllegalArgumentException("模型缺少 modelName");
        }
        //        if (m.baseUrl == null || m.baseUrl.isBlank()) throw new IllegalArgumentException("模型缺少 baseUrl");
        if (m.apiKey == null || m.apiKey.isBlank()) {
            throw new IllegalArgumentException("模型缺少 apiKey");
        }
        if (!ids.add(m.id)) {
            throw new IllegalArgumentException("重复的模型 id: " + m.id);
        }
        if (!names.add(m.name)) {

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Open the models YAML file and add a unique numeric `id` to every model entry.
  2. Check for duplicate ids at the same time — validateYamlModel also rejects ids already present in the ids set.
  3. Verify YAML indentation so `id` sits directly under the model entry, not nested deeper.
  4. Confirm the YamlModel field name matches the YAML key (id) exactly.

Example fix

// before (yaml)
models:
  - name: qwen
    provider: dashscope
// after (yaml)
models:
  - id: 1
    name: qwen
    provider: dashscope
Defensive patterns

Strategy: validation

Validate before calling

// pre-validate yaml before load
models.forEach(m -> {
    if (m.id == null) throw new IllegalArgumentException("entry '" + m.name + "' missing id");
    if (ids.contains(m.id)) throw new IllegalArgumentException("duplicate id " + m.id);
    ids.add(m.id);
});

Type guard

static boolean hasId(YamlModel m) { return m != null && m.id != null; }

Try / catch

try { repo = FileModelConfigRepository.loadFromFile(path); }
catch (IllegalArgumentException e) { log.error("Model config invalid: {}", e.getMessage()); throw new ConfigurationException(e); }

Prevention

When it happens

Trigger: Loading a models YAML file where a model entry omits the `id` key, or where YAML parsing leaves id null (wrong key name, wrong indentation, or id written as a non-mappable value).

Common situations: Hand-editing the YAML and forgetting id on a new model block; copy-pasting a template that has id commented out; YAML indentation mistakes nesting id under the wrong node.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


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