siyuan-note/siyuan · error

createDocTree definition must be a list

Error message

createDocTree definition must be a list

What it means

parseNodes type-asserts the definition value to []any. Any value that is not a JSON array — a string, number, object, or null — fails this assertion and produces this error. The createDocTree definition must always be a list of document dictionaries at every level.

Solutions

  1. Wrap a single document in an array: [{"title": "Doc"}] instead of {"title": "Doc"}.
  2. Parse JSON on the client before assigning to def; do not send the definition as a string.
  3. Ensure every 'children' value in the definition is an array (possibly empty-free — use non-empty lists at top level).

Example fix

// before
{"def": {"title": "Doc"}}
// after
{"def": [{"title": "Doc"}]}
Defensive patterns

Strategy: type-guard

Type guard

function isDocTreeDef(def) {
  return Array.isArray(def) && def.every(n => n !== null && typeof n === "object" && !Array.isArray(n));
}
if (!isDocTreeDef(def)) throw new Error("definition must be a list of document objects");

Try / catch

try {
  await createDocTree(payload);
} catch (e) {
  if (String(e).includes("must be a list")) {
    // re-wrap a single object: def = [def]
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling createDocTree with def as a string (e.g. JSON text not parsed), a single object {"title":...} instead of an array wrapping it, or a child field set to a non-array value.

Common situations: Client double-encodes the definition (sending a JSON string of JSON); passing one document object directly instead of a one-element array; a children field accidentally set to a dict instead of a list.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/dbfe355c7e2ee866. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/template_doc_tree.go:127

		return nil, err
	}
	if 0 == len(nodes) {
		return nil, errors.New("createDocTree requires at least one document")
	}
	return nodes, nil
}

type templateDocTreeParseState struct {
	count int
}

func (state *templateDocTreeParseState) parseNodes(value any, depth int) ([]*TemplateDocTreeNode, error) {
	if maxTemplateDocTreeDepth < depth {
		return nil, fmt.Errorf("createDocTree exceeds the maximum depth of %d", maxTemplateDocTreeDepth)
	}
	values, ok := value.([]any)
	if !ok {
		return nil, errors.New("createDocTree definition must be a list")
	}
	if 0 == len(values) {
		return nil, errors.New("createDocTree document list must not be empty")
	}

	nodes := make([]*TemplateDocTreeNode, 0, len(values))
	for _, value := range values {
		definition, ok := value.(map[string]any)
		if !ok {
			return nil, errors.New("createDocTree document must be a dictionary")
		}
		for key := range definition {
			switch key {
			case "title", "template", "define", "children":
			default:
				return nil, fmt.Errorf("createDocTree document contains unknown field [%s]", key)
			}
		}

View on GitHub (pinned to 9f775e8a12)