alibaba/spring-ai-alibaba · error · IllegalArgumentException

序列化 defaultParameters 失败

Error message

序列化 defaultParameters 失败

What it means

toEntity converts a YAML model config (m) into a persistence entity, serializing m.defaultParameters to a JSON string with a new ObjectMapper. Any exception during writeValueAsString is wrapped in this IllegalArgumentException. Because the value is a generic Map/List from user-authored YAML, Jackson failures typically indicate a non-serializable value type rather than malformed YAML.

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:152

        if (!names.add(m.name)) {
            throw new IllegalArgumentException("重复的模型 name: " + m.name);
        }
        if (m.status == null) {
            m.status = 1;
        }
    }
    
    private ModelConfigDO toEntity(YamlModel m) {
        ModelConfigDO.ModelConfigDOBuilder b = ModelConfigDO.builder().id(m.id).name(m.name)
                .provider(m.provider.toLowerCase()).modelName(m.modelName).baseUrl(m.baseUrl)
                .apiKey(environment != null ? environment.resolvePlaceholders(m.apiKey) : m.apiKey).status(m.status)
                .createTime(LocalDateTime.now()).updateTime(LocalDateTime.now());
        
        if (m.defaultParameters != null) {
            try {
                b.defaultParameters(new ObjectMapper().writeValueAsString(m.defaultParameters));
            } catch (Exception e) {
                throw new IllegalArgumentException("序列化 defaultParameters 失败", e);
            }
        }
        if (m.supportedParameters != null) {
            try {
                b.supportedParameters(new ObjectMapper().writeValueAsString(m.supportedParameters));
            } catch (Exception e) {
                throw new IllegalArgumentException("序列化 supportedParameters 失败", e);
            }
        }
        return b.build();
    }
    
    private void startWatchService() {
        try {
            Path dir = this.configPath.getParent();
            if (dir == null) {
                return;
            }

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Inspect the wrapped cause (e) in the stack trace to identify the exact Jackson failure
  2. Replace problematic values in defaultParameters in the YAML with plain JSON-compatible scalars, lists, and maps
  3. Remove YAML anchors/aliases that could expand to cyclic references; inline the values
  4. If custom types are required, register a suitable Jackson serializer/module instead of the default ObjectMapper

Example fix

// before
model.defaultParameters = Map.of("self", model.defaultParameters); // cyclic
// after
model.defaultParameters = Map.of("temperature", 0.7, "maxTokens", 2048);
Defensive patterns

Strategy: validation

Validate before calling

// Java: ensure defaultParameters is JSON-serializable before assignment
new ObjectMapper().writeValueAsString(model.getDefaultParameters()); // throws early if not

Type guard

boolean isJsonSafe(Object v) {
    try { new ObjectMapper().writeValueAsString(v); return true; }
    catch (JsonProcessingException e) { return false; }
}

Try / catch

try {
    entity = toEntity(model);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("defaultParameters")) {
        log.error("defaultParameters is not JSON-serializable", e.getCause());
    }
}

Prevention

When it happens

Trigger: Calling toEntity (via entity()) on a model whose defaultParameters contains objects Jackson cannot serialize, e.g., self-referencing maps or exotic value types loaded from YAML.

Common situations: Placing non-JSON-friendly constructs in defaultParameters in the YAML (anchors/aliases creating cyclic references); passing programmatically built Maps containing custom objects without serializers into the config pipeline.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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