alibaba/spring-ai-alibaba · error · IllegalArgumentException
模型配置格式错误:
Error message
模型配置格式错误:
What it means
An IllegalArgumentException thrown by ModelConfigParser.parseModelConfig when Jackson (objectMapper.readValue) cannot deserialize modelConfigJson into ModelConfigInfo. The original exception is logged and wrapped, so the message carries Jackson's reason (syntax error, unknown fields failing, type mismatch, etc.).
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:45
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 文件) 查找
boolean exists = modelConfigRepository.existsById(modelConfigInfo.getModelId());
if (!exists) {
// 如果 ModelConfigRepository 中不存在,尝试从 ModelManager (数据库) 查找
// 安全地获取 workspaceId
String workspaceId = null;
try {View on GitHub (pinned to f82da0b50f)
Solutions
- Validate the JSON with a linter/parser before storing or passing it (e.g. objectMapper.readTree in a pre-check).
- Fix the JSON syntax so it parses (check the wrapped e.getMessage() for line/column).
- Compare the JSON keys/types against ModelConfigInfo fields and align them.
- Re-save the model config through the official UI/API so it is serialized by the same ObjectMapper.
Example fix
// before
String cfg = "{modelId: 'qwen-max'}"; // invalid JSON
ModelConfigInfo info = parser.parseModelConfig(cfg);
// after
String cfg = "{\"modelId\":\"qwen-max\"}";
ModelConfigInfo info = parser.parseModelConfig(cfg); Defensive patterns
Strategy: validation
Validate before calling
try { new ObjectMapper().readTree(cfg); } catch (Exception e) { throw new BadRequestException("modelConfig is not valid JSON: " + e.getMessage()); } Type guard
null
Try / catch
try { return parser.parseModelConfig(cfg); } catch (IllegalArgumentException e) { throw new BadRequestException("Invalid modelConfig JSON: " + e.getMessage()); } Prevention
- Validate JSON before persisting configs
- Only edit configs through the official editor/API
- Check the wrapped cause message for the exact parse error position
When it happens
Trigger: Calling parseModelConfig with a string that is not valid JSON (truncated, HTML error page, single quotes, trailing comma) or valid JSON whose shape doesn't match ModelConfigInfo (e.g. modelId as an object instead of string).
Common situations: Hand-edited config stored in the DB; config produced by a different tool/version with a different schema; encoding issues corrupting the stored JSON; copy-paste errors including markdown fences.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- 序列化 defaultParameters 失败
- 序列化 supportedParameters 失败
- JSON processing failed:
- CreateMCPServerError
- UpdateMCPServerError
AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09).
Data as JSON: /api/errors/8abb6929c69ec011.
Report an issue: GitHub.