gohugoio/hugo · error
cannot convert type %T to Pages
Error message
cannot convert type %T to Pages
What it means
ToPages normalizes template values into a page.Pages collection, accepting Pages, *Pages, WeightedPages, PageGroup, []Page, and []any containing only Pages. Anything else — or an []any with a non-Page element — falls through to this error. It is commonly surfaced by functions that operate on page collections, such as .Group, sorting, and pagination helpers.
Source
Thrown at resources/page/pages.go:87
copy(pages, v)
return pages, nil
case []any:
pages := make(Pages, len(v))
success := true
for i, vv := range v {
p, ok := vv.(Page)
if !ok {
success = false
break
}
pages[i] = p
}
if success {
return pages, nil
}
}
return nil, fmt.Errorf("cannot convert type %T to Pages", seq)
}
// Group groups the pages in in by key.
// This implements collections.Grouper.
func (p Pages) Group(key any, in any) (any, error) {
pages, err := ToPages(in)
if err != nil {
return PageGroup{}, err
}
return PageGroup{Key: key, Pages: pages}, nil
}
// Len returns the number of pages in the list.
func (p Pages) Len() int {
return len(p)
}
// ProbablyEq wraps compare.ProbablyEqerView on GitHub ↗ (pinned to 8a468df065)
Solutions
- Pass an actual page collection (e.g. `.Site.RegularPages`, `where .Site.RegularPages ...`) to pagination/grouping functions.
- When working with grouped results, range over the groups and use each `.Pages` field.
- If building a custom slice, ensure every element is a Page — check with `{{ printf "%T" $x }}`.
- For data-file content, render it directly with `range` instead of page-collection functions.
Example fix
// before
{{ $p := .Paginate .Site.Data.products }}
// after
{{ $p := .Paginate (where .Site.RegularPages "Section" "products") }} Defensive patterns
Strategy: type-guard
Type guard
// Check convertibility before calling page.ToPages
func isPagesLike(v any) bool {
switch v.(type) {
case page.Pages, []page.Page, []any, nil:
return true
default:
return false
}
} Try / catch
pages, err := page.ToPages(v)
if err != nil {
return fmt.Errorf("expected a Pages collection, got %T: %w", v, err)
} Prevention
- Don't pass scalar values or maps where a Pages collection is expected in templates
- Keep template variables that hold page collections clearly named (e.g. $pages)
- Test templates with empty and non-empty collections
When it happens
Trigger: Passing a non-page collection to a function that calls ToPages: `.Paginate` on a slice of strings/maps, `group` on a heterogeneous `slice` result, or handing PagesGroup (the whole grouped set) where a single group's .Pages was expected.
Common situations: Paginating `where .Site.Data ...` results or other data-file slices (data is not Pages); building a mixed `slice` in templates that includes non-Page items; passing the output of `.GroupBy` directly instead of ranging over groups.
Related errors
- symdiff: failed to convert value: %w
- the math.Abs function requires a numeric argument
- Ceil operator can't be used with non-float value
- Floor operator can't be used with non-float value
- Log operator can't be used with non integer or float value
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/23ddfb467b2a5b56.json.
Report an issue: GitHub ↗.