vxcontrol/pentagi · error

error executing template: %v

Error message

error executing template: %v

What it means

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.

Source

Thrown at backend/pkg/tools/searchers/perplexity.go:347

	templateContext := map[string]any{
		"Query":        query,
		"MaxLength":    maxRawContentLength,
		"Content":      content,
		"HasCitations": citations != nil && len(*citations) > 0,
	}

	if citations != nil && len(*citations) > 0 {
		templateContext["Citations"] = *citations
	}

	tmpl, err := template.New("summarize").Funcs(funcMap).Parse(templateText)
	if err != nil {
		return "", fmt.Errorf("error creating template: %v", err)
	}

	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, templateContext); err != nil {
		return "", fmt.Errorf("error executing template: %v", err)
	}

	return buf.String(), nil
}

// isAvailable checks the availability of the API
func (p *perplexity) IsAvailable() bool {
	return p.apiKey() != ""
}

func (p *perplexity) apiKey() string {
	if p.cfg == nil {
		return ""
	}

	return p.cfg.PerplexityAPIKey
}

View on GitHub (pinned to ea665308ba)

Solutions

  1. Check that templateContext["Citations"] is []string when set and the template ranges over it correctly
  2. Verify the 'inc' function in funcMap still accepts and returns int
  3. Log the runtime error from tmpl.Execute including the field being rendered to pinpoint the failing action
  4. If a custom template was introduced, validate the context keys it references all exist in templateContext
  5. Remember formatResponse falls back to truncated raw content on this error — restore the full summarize path by fixing the data/template mismatch

Example fix

// before
templateContext["Citations"] = citations // wrong type: **[]string
// after
templateContext["Citations"] = *citations // dereferenced []string
Defensive patterns

Strategy: try-catch

Validate before calling

// validate context types before Execute
ctx := map[string]any{"Query": query, "MaxLength": maxLen, "Content": content, "HasCitations": hasCit}
if hasCit {
    if _, ok := ctx["Citations"].([]string); !ok {
        return "", fmt.Errorf("Citations must be []string")
    }
}

Type guard

func asStringSlice(v any) ([]string, bool) {
    s, ok := v.([]string)
    return s, ok
}

Try / catch

prompt, err := p.getSummarizePrompt(query, rawContent, response.Citations)
if err != nil {
    log.Printf("summarize prompt build failed, falling back to truncation: %v", err)
    return rawContent[:min(len(rawContent), maxRawContentLength)]
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of vxcontrol/pentagi@ea665308ba (2026-09-01). Data as JSON: /api/errors/f52d5e541e1acd82. Report an issue: GitHub.