alibaba/spring-ai-alibaba · warning
变量替换失败,使用原始模板: template={}, variables={}
Error message
变量替换失败,使用原始模板: template={}, variables={} What it means
ModelConfigParser.replaceVariables substitutes {{placeholder}} variables into a prompt template using a variables JSON string; if any step throws (malformed JSON, bad types, null values), it logs this warning and returns the raw template with unreplaced placeholders. The prompt is sent to the model containing literal {{variable}} tokens.
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:177
return template;
}
try {
JsonNode variables = objectMapper.readTree(variablesJson);
StringBuilder resultBuilder = new StringBuilder(template);
// 替换所有变量占位符
variables.fields().forEachRemaining(entry -> {
String placeholder = "{{" + entry.getKey() + "}}";
String value = entry.getValue().asText();
String current = resultBuilder.toString();
resultBuilder.setLength(0);
resultBuilder.append(current.replace(placeholder, value));
});
return resultBuilder.toString();
} catch (Exception e) {
log.warn("变量替换失败,使用原始模板: template={}, variables={}", template, variablesJson, e);
return template;
}
}
/**
* 验证模型配置的有效性 只验证必需字段,动态参数由模型服务自行验证
*
* @param modelConfigInfo 模型配置信息
* @throws IllegalArgumentException 如果配置无效
*/
public void validateModelConfig(ModelConfigInfo modelConfigInfo) {
if (modelConfigInfo == null) {
throw new IllegalArgumentException("模型配置不能为空");
}
if (modelConfigInfo.getModelId() == null) {
throw new IllegalArgumentException("模型ID不能为空");
}View on GitHub (pinned to f82da0b50f)
Solutions
- Validate variablesJson parses to a JSON object (JSONObject.parseObject) before calling replaceVariables
- Escape or remove literal {{...}} sequences in templates that are not variables
- Check the variable names in the template exactly match the JSON keys
- If a raw template with placeholders is unacceptable downstream, throw instead of returning the template
Example fix
// before
String prompt = ModelConfigParser.replaceVariables(tpl, "a=1;b=2"); // invalid JSON, raw template returned
// after
String prompt = ModelConfigParser.replaceVariables(tpl, "{\"a\":\"1\",\"b\":\"2\"}");
if (prompt.contains("{{")) { throw new IllegalArgumentException("Unresolved variables in prompt"); } Defensive patterns
Strategy: validation
Validate before calling
try { JSONObject.parseObject(variablesJson); } catch (Exception e) { throw new IllegalArgumentException("variables must be a JSON object"); } Type guard
boolean isValidVariablesJson(String s) { try { JSONObject.parseObject(s); return true; } catch (Exception e) { return false; } } Try / catch
String prompt = ModelConfigParser.replaceVariables(tpl, varsJson); if (prompt.contains("{{")) { throw new IllegalStateException("Unresolved template variables"); } Prevention
- Always pass variables as a valid JSON object
- Match template placeholder names to JSON keys exactly
- Escape literal {{ }} in templates
- Validate JSON before calling the parser
When it happens
Trigger: Passing a variablesJson that is invalid JSON (or non-object), or a template referencing placeholders with values of types the replacement cannot stringify, throwing inside the replacement lambda chain.
Common situations: Client sends variables as a comma-separated string instead of JSON object; nested/escaped quotes break JSON parsing; variable name mismatch leaving placeholders; template containing literal {{ }} syntax that was never meant as a variable.
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
AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09).
Data as JSON: /api/errors/fb3455223cda3c21.
Report an issue: GitHub.