{"record":{"id":"84c1a9d241823068","repo":"siyuan-note/siyuan","slug":"template-output-exceeds-d-bytes","errorCode":null,"errorMessage":"template output exceeds %d bytes","messagePattern":"template output exceeds (.+?) bytes","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template.go","lineNumber":830,"sourceCode":"\ttpl, err := goTpl.Funcs(tplFuncMap).Parse(gulu.Str.FromBytes(md))\n\tif err != nil {\n\t\terr = fmt.Errorf(Conf.Language(44), err.Error())\n\t\treturn\n\t}\n\tif collector.enabled && templateUsesFunction(tpl, \"createDocTree\") {\n\t\tif err = validateTemplateCallGraph(tpl, tpl.Name()); nil != err {\n\t\t\treturn\n\t\t}\n\t}\n\n\tbuf := &bytes.Buffer{}\n\tbuf.Grow(4096)\n\tif err = tpl.Execute(buf, dataModel); err != nil {\n\t\terr = fmt.Errorf(Conf.Language(44), err.Error())\n\t\treturn\n\t}\n\tif 0 < len(collector.nodes) && maxTemplateDocTreeOutputSize < buf.Len() {\n\t\terr = fmt.Errorf(\"template output exceeds %d bytes\", maxTemplateDocTreeOutputSize)\n\t\treturn\n\t}\n\tcollector.totalOutput = buf.Len()\n\tmd = buf.Bytes()\n\ttree, err = parseTemplateKTree(md)\n\tif err != nil {\n\t\tlogging.LogErrorf(\"parse template [%s] failed: %s\", p, err)\n\t\treturn\n\t}\n\ttree.Box = sourceTree.Box\n\tif 0 < len(collector.nodes) {\n\t\tif err = collector.validateLocations(); nil != err {\n\t\t\treturn\n\t\t}\n\t\tif templateTreeContainsAttributeView(tree) {\n\t\t\terr = errors.New(\"database blocks are not supported by createDocTree templates\")\n\t\t\treturn\n\t\t}","sourceCodeStart":812,"sourceCodeEnd":848,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/model/template.go#L812-L848","documentation":"After executing a createDocTree template, the kernel checks the rendered output size: if the template used createDocTree (collector.nodes non-empty) and the rendered buffer exceeds maxTemplateDocTreeOutputSize, rendering aborts with this error. The limit protects the kernel from a runaway template that would create a huge document tree.","triggerScenarios":"Calling RenderTemplateWithMode or PreviewTemplateSource with a template that uses .action{createDocTree ...} and whose expanded output (loops over large query results, repeated document definitions) exceeds the configured byte limit.","commonSituations":"A createDocTree template looping over a query returning thousands of blocks; recursively generating deep document trees; large embedded content repeated for every generated document.","solutions":["Reduce the output size of the template: narrow queries with LIMIT or filters so fewer documents/blocks are generated.","Split the template into several smaller templates run in separate steps.","If legitimately needed, raise maxTemplateDocTreeOutputSize in kernel/model/template.go and rebuild the kernel (self-hosted only)."],"exampleFix":"// before (template markdown)\n.action{$docs := queryBlocks \"SELECT * FROM blocks\"}\n.action{range $docs}.action{createDocTree ...}.action{end}\n// after\n.action{$docs := queryBlocks \"SELECT * FROM blocks WHERE type = 'd' LIMIT 50\"}\n.action{range $docs}.action{createDocTree ...}.action{end}","handlingStrategy":"validation","validationCode":"// Estimate rendered size before invoking the render API\nconst maxTemplateDocTreeOutputSize = 10 * 1024 * 1024 // keep in sync with kernel\nif len(strings.ReplaceAll(md, \".action{\", \"\")) > maxTemplateDocTreeOutputSize { return errors.New(\"template too large\") }\n// Better: bound query results in the template itself (LIMIT clause)","typeGuard":null,"tryCatchPattern":"if err := renderTemplateWithMode(p, mode); err != nil {\n    if strings.Contains(err.Error(), \"template output exceeds\") {\n        return fmt.Errorf(\"narrow the createDocTree queries in %s and retry\", p)\n    }\n    return err\n}","preventionTips":["Always add LIMIT/filters to queries inside createDocTree templates","Avoid unbounded range loops over query results in templates","Split very large document generation into multiple templates"],"tags":["template","size-limit","createdoctree"],"backgroundTag":"file-size-limit-exceeded","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}