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.ProbablyEqer

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Pass an actual page collection (e.g. `.Site.RegularPages`, `where .Site.RegularPages ...`) to pagination/grouping functions.
  2. When working with grouped results, range over the groups and use each `.Pages` field.
  3. If building a custom slice, ensure every element is a Page — check with `{{ printf "%T" $x }}`.
  4. 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

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


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/23ddfb467b2a5b56.json. Report an issue: GitHub ↗.