{"record":{"id":"9794dc82cdfb2986","repo":"vxcontrol/pentagi","slug":"template-content-is-empty","errorCode":null,"errorMessage":"template content is empty","messagePattern":"template content is empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/templates/validator/validator.go","lineNumber":104,"sourceCode":"\t}\n\n\t// Test template rendering with mock data\n\tmockData := CreateDummyTemplateData()\n\tif err := testTemplateRendering(prompt, mockData); err != nil {\n\t\treturn &ValidationError{\n\t\t\tType:    ErrorTypeRenderingFailed,\n\t\t\tMessage: fmt.Sprintf(\"template rendering failed: %v\", err),\n\t\t\tDetails: extractRenderingDetails(err),\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// ExtractTemplateVariables parses a template and extracts all top-level variables\nfunc ExtractTemplateVariables(templateContent string) ([]string, error) {\n\tif strings.TrimSpace(templateContent) == \"\" {\n\t\treturn nil, fmt.Errorf(\"template content is empty\")\n\t}\n\n\t// Create function map with all builtin functions as nil values for the parser\n\tfuncMap := template.FuncMap{\n\t\t// Builtin comparison and logic functions\n\t\t\"and\": nil, \"or\": nil, \"not\": nil,\n\t\t\"eq\": nil, \"ne\": nil, \"lt\": nil, \"le\": nil, \"gt\": nil, \"ge\": nil,\n\t\t// Builtin utility functions\n\t\t\"len\": nil, \"index\": nil, \"slice\": nil, \"print\": nil, \"printf\": nil, \"println\": nil,\n\t\t\"html\": nil, \"js\": nil, \"urlquery\": nil, \"call\": nil,\n\t\t// Additional common functions that might be used\n\t\t\"add\": nil, \"sub\": nil, \"mul\": nil, \"div\": nil, \"mod\": nil,\n\t\t\"upper\": nil, \"lower\": nil, \"title\": nil, \"trim\": nil, \"trimSpace\": nil,\n\t\t\"default\": nil, \"empty\": nil, \"contains\": nil, \"hasPrefix\": nil, \"hasSuffix\": nil,\n\t}\n\n\t// Parse template with function map to get AST\n\tparsed, err := parse.Parse(\"validation\", templateContent, \"{{\", \"}}\", funcMap)","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/templates/validator/validator.go#L86-L122","documentation":"ExtractTemplateVariables parses Go text/template content to extract top-level variables. It rejects whitespace-only or empty template content up front because there is nothing to parse. It is a guard against silently returning zero variables for blank input.","triggerScenarios":"Calling validator.ExtractTemplateVariables(\"\") or with a string of only spaces/tabs/newlines, directly or indirectly through ValidatePrompt with an empty prompt template.","commonSituations":"A prompt stored in the DB or config file is blank (misconfigured seed data); an env var supplying the template was never set; a user saved an empty custom prompt in settings.","solutions":["Supply non-empty template content before calling ExtractTemplateVariables/ValidatePrompt.","Check where the template is loaded from (env var, DB row, file) — the source is empty or unset.","Add an earlier validation layer (zod on frontend, required field on backend) so blank prompts never reach the validator."],"exampleFix":"// before\nvars, err := validator.ExtractTemplateVariables(cfg.SystemPrompt) // cfg.SystemPrompt == \"\"\n// after\nif strings.TrimSpace(cfg.SystemPrompt) == \"\" {\n    return fmt.Errorf(\"system prompt template must be configured\")\n}\nvars, err := validator.ExtractTemplateVariables(cfg.SystemPrompt)","handlingStrategy":"validation","validationCode":"func templateNonEmpty(s string) bool { return strings.TrimSpace(s) != \"\" }\nif !templateNonEmpty(cfg.SystemPrompt) { return errors.New(\"template must be non-empty\") }","typeGuard":"func hasTemplateContent(s string) bool {\n    return len(strings.TrimSpace(s)) > 0\n}","tryCatchPattern":"vars, err := validator.ExtractTemplateVariables(tmpl)\nif err != nil && strings.Contains(err.Error(), \"template content is empty\") {\n    // fall back to a default template or surface a config error\n}","preventionTips":["Validate templates at load time (config/env/DB seed) before use.","Make prompt fields required in settings UI and API schemas.","Trim and check template strings immediately after reading env vars or files.","Keep a known-good default template as fallback."],"tags":["validation","templates","go-template"],"backgroundTag":"empty-template-content","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}