{"record":{"id":"f52d5e541e1acd82","repo":"vxcontrol/pentagi","slug":"error-executing-template-v-f52d5e","errorCode":null,"errorMessage":"error executing template: %v","messagePattern":"error executing template: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/tools/searchers/perplexity.go","lineNumber":347,"sourceCode":"\ttemplateContext := map[string]any{\n\t\t\"Query\":        query,\n\t\t\"MaxLength\":    maxRawContentLength,\n\t\t\"Content\":      content,\n\t\t\"HasCitations\": citations != nil && len(*citations) > 0,\n\t}\n\n\tif citations != nil && len(*citations) > 0 {\n\t\ttemplateContext[\"Citations\"] = *citations\n\t}\n\n\ttmpl, err := template.New(\"summarize\").Funcs(funcMap).Parse(templateText)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"error creating template: %v\", err)\n\t}\n\n\tvar buf bytes.Buffer\n\tif err := tmpl.Execute(&buf, templateContext); err != nil {\n\t\treturn \"\", fmt.Errorf(\"error executing template: %v\", err)\n\t}\n\n\treturn buf.String(), nil\n}\n\n// isAvailable checks the availability of the API\nfunc (p *perplexity) IsAvailable() bool {\n\treturn p.apiKey() != \"\"\n}\n\nfunc (p *perplexity) apiKey() string {\n\tif p.cfg == nil {\n\t\treturn \"\"\n\t}\n\n\treturn p.cfg.PerplexityAPIKey\n}\n","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/tools/searchers/perplexity.go#L329-L365","documentation":"This error is returned when tmpl.Execute fails while rendering the summarize prompt template against the templateContext map. Unlike parse errors, execution errors come from bad data at runtime: calling a function with wrong argument types, nil pointer dereference in a pipeline, or writing to a failing writer. In this code the context is map[string]any, so a type mismatch (e.g. Citations not being []string) surfaces here.","triggerScenarios":"The {{range $index, $citation := .Citations}} loop receiving a Citations value that is not a slice (wrong type stored in the map), or the 'inc' funcMap entry changed to a signature incompatible with int, or Content containing data triggering a method-call error in a modified template.","commonSituations":"Refactoring templateContext keys or the Citations type (e.g. switching to []any without updating the template pipeline); concurrent modification of funcMap; building with a customized template that indexes a map key absent from templateContext.","solutions":["Check that templateContext[\"Citations\"] is []string when set and the template ranges over it correctly","Verify the 'inc' function in funcMap still accepts and returns int","Log the runtime error from tmpl.Execute including the field being rendered to pinpoint the failing action","If a custom template was introduced, validate the context keys it references all exist in templateContext","Remember formatResponse falls back to truncated raw content on this error — restore the full summarize path by fixing the data/template mismatch"],"exampleFix":"// before\ntemplateContext[\"Citations\"] = citations // wrong type: **[]string\n// after\ntemplateContext[\"Citations\"] = *citations // dereferenced []string\n","handlingStrategy":"try-catch","validationCode":"// validate context types before Execute\nctx := map[string]any{\"Query\": query, \"MaxLength\": maxLen, \"Content\": content, \"HasCitations\": hasCit}\nif hasCit {\n    if _, ok := ctx[\"Citations\"].([]string); !ok {\n        return \"\", fmt.Errorf(\"Citations must be []string\")\n    }\n}","typeGuard":"func asStringSlice(v any) ([]string, bool) {\n    s, ok := v.([]string)\n    return s, ok\n}","tryCatchPattern":"prompt, err := p.getSummarizePrompt(query, rawContent, response.Citations)\nif err != nil {\n    log.Printf(\"summarize prompt build failed, falling back to truncation: %v\", err)\n    return rawContent[:min(len(rawContent), maxRawContentLength)]\n}","preventionTips":["Enforce exact context key types (Citations as []string) when building templateContext","Add a rendering test that executes the template with sample data, not just Parse","Cover the summarize path in tests — formatResponse currently swallows these errors silently","Any change to funcMap signatures must be paired with template updates"],"tags":["go-templates","text-template","perplexity","summarization"],"backgroundTag":"template-execution-error","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}