{"record":{"id":"f76b0e41521862a8","repo":"gohugoio/hugo","slug":"nil-is-not-a-valid-key-to-group-by","errorCode":null,"errorMessage":"nil is not a valid key to group by","messagePattern":"nil is not a valid key to group by","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/collections.go","lineNumber":314,"sourceCode":"\t\t\t\t\t}\n\n\t\t\t\t\tins.handleValuePair(l1vv, l2vv)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn ins.r.Interface(), nil\n\t\tdefault:\n\t\t\treturn nil, errors.New(\"can't iterate over \" + reflect.ValueOf(l2).Type().String())\n\t\t}\n\tdefault:\n\t\treturn nil, errors.New(\"can't iterate over \" + reflect.ValueOf(l1).Type().String())\n\t}\n}\n\n// Group groups a set of items by the given key.\n// This is currently only supported for Pages.\nfunc (ns *Namespace) Group(key any, items any) (any, error) {\n\tif key == nil {\n\t\treturn nil, errors.New(\"nil is not a valid key to group by\")\n\t}\n\n\tif g, ok := items.(collections.Grouper); ok {\n\t\treturn g.Group(key, items)\n\t}\n\n\tin := newSliceElement(items)\n\n\tif g, ok := in.(collections.Grouper); ok {\n\t\treturn g.Group(key, items)\n\t}\n\n\treturn nil, fmt.Errorf(\"grouping not supported for type %T %T\", items, in)\n}\n\n// IsSet returns whether a given array, channel, slice, or map in c has the given key\n// defined.\nfunc (ns *Namespace) IsSet(c any, key any) (bool, error) {","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/collections.go#L296-L332","documentation":"Thrown by the Group template function when its first argument (the group key) is nil. Grouping requires a concrete key to bucket items under, so a nil key is rejected outright before any collection is inspected. This is a hard precondition check at the very top of the function (line 313).","triggerScenarios":"Calling {{ group $key .Pages }} or ns.Group(key, items) in Go where key evaluates to nil (e.g. an unset page param, a missing front matter field, or a template variable never assigned). Also hit when a derived grouping key like .Date is nil on a draft/fixture page.","commonSituations":"Grouping pages by a front matter field (e.g. tags, categories, author) that is omitted on some content; using {{ .Param \"x\" }} that returns nil; content fixtures in tests missing the field the layout groups on; upgrading layouts that assume a field always exists.","solutions":["Default the key before grouping: {{ $key := .Params.category | default \"uncategorized\" }} {{ group $key .Pages }}.","Guard with {{ with }} so the group call only runs when the key is non-nil: {{ with .Params.category }}{{ group . $.Pages }}{{ end }}.","Audit content files and ensure the field used as the group key is present on every page in the set being grouped."],"exampleFix":"// before\n{{ group .Params.author .Pages }}\n// after\n{{ $author := .Params.author | default \"unknown\" }}\n{{ group $author .Pages }}","handlingStrategy":"validation","validationCode":"{{ $key := .Params.category | default \"uncategorized\" }}\n{{ if ne $key nil }}{{ group $key .Pages }}{{ end }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pipe group keys through | default before calling group.","Audit content front matter to ensure the group-by field exists on all pages in the set.","Use {{ with $key }} around the group call so nil keys are skipped."],"tags":["hugo","template","collections","group","nil-check"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}