{"record":{"id":"fb3455223cda3c21","repo":"alibaba/spring-ai-alibaba","slug":"template-variables","errorCode":null,"errorMessage":"变量替换失败，使用原始模板: template={}, variables={}","messagePattern":"变量替换失败，使用原始模板: template=(.+?), variables=(.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/utils/ModelConfigParser.java","lineNumber":177,"sourceCode":"            return template;\n        }\n        \n        try {\n            JsonNode variables = objectMapper.readTree(variablesJson);\n            StringBuilder resultBuilder = new StringBuilder(template);\n            \n            // 替换所有变量占位符\n            variables.fields().forEachRemaining(entry -> {\n                String placeholder = \"{{\" + entry.getKey() + \"}}\";\n                String value = entry.getValue().asText();\n                String current = resultBuilder.toString();\n                resultBuilder.setLength(0);\n                resultBuilder.append(current.replace(placeholder, value));\n            });\n            \n            return resultBuilder.toString();\n        } catch (Exception e) {\n            log.warn(\"变量替换失败，使用原始模板: template={}, variables={}\", template, variablesJson, e);\n            return template;\n        }\n    }\n    \n    /**\n     * 验证模型配置的有效性 只验证必需字段，动态参数由模型服务自行验证\n     *\n     * @param modelConfigInfo 模型配置信息\n     * @throws IllegalArgumentException 如果配置无效\n     */\n    public void validateModelConfig(ModelConfigInfo modelConfigInfo) {\n        if (modelConfigInfo == null) {\n            throw new IllegalArgumentException(\"模型配置不能为空\");\n        }\n        \n        if (modelConfigInfo.getModelId() == null) {\n            throw new IllegalArgumentException(\"模型ID不能为空\");\n        }","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/utils/ModelConfigParser.java#L159-L195","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nString prompt = ModelConfigParser.replaceVariables(tpl, \"a=1;b=2\"); // invalid JSON, raw template returned\n// after\nString prompt = ModelConfigParser.replaceVariables(tpl, \"{\\\"a\\\":\\\"1\\\",\\\"b\\\":\\\"2\\\"}\");\nif (prompt.contains(\"{{\")) { throw new IllegalArgumentException(\"Unresolved variables in prompt\"); }","handlingStrategy":"validation","validationCode":"try { JSONObject.parseObject(variablesJson); } catch (Exception e) { throw new IllegalArgumentException(\"variables must be a JSON object\"); }","typeGuard":"boolean isValidVariablesJson(String s) { try { JSONObject.parseObject(s); return true; } catch (Exception e) { return false; } }","tryCatchPattern":"String prompt = ModelConfigParser.replaceVariables(tpl, varsJson); if (prompt.contains(\"{{\")) { throw new IllegalStateException(\"Unresolved template variables\"); }","preventionTips":["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"],"tags":["template","variables","json"],"backgroundTag":"json-parse-error","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}