gohugoio/hugo · error

invalid argument type %T

Error message

invalid argument type %T

What it means

Thrown by Pages.Related when the options argument is neither a related.Document nor a map[string]any. The method uses a type switch with those two cases; any other type falls through to this error reporting the actual type.

Solutions

  1. Pass the current page (a related.Document) for document-based search: {{ .RegularPages.Related . }}.
  2. Pass a map[string]any options object for custom queries (e.g. {"document": ., "indices": ["tags"]}).
  3. For keyword-based search use the deprecated RelatedTo with types.KeyValues, not Related.

Example fix

{{/* before */}}
{{ $r := .RegularPages.Related "tags" }}
{{/* after */}}
{{ $r := .RegularPages.Related . }}
{{/* or options map */}}
{{ $r := .RegularPages.Related (dict "document" . "indices" (slice "tags")) }}
Defensive patterns

Strategy: type-guard

Validate before calling

switch optsv.(type) {
case related.Document, map[string]any:
    // ok
cinstance:
    return fmt.Errorf("Related needs a Document or options map, got %T", optsv)
}

Type guard

func isRelatedArg(v any) bool {
    switch v.(type) {
    case related.Document, map[string]any:
        return true
    }
    return false
}

Try / catch

r, err := pages.Related(ctx, opts)
if err != nil { /* log and return empty */ }

Prevention

When it happens

Trigger: Calling .Related with the wrong argument, e.g. a string ('.Related "tags"'), a slice of keywords, or a struct that does not implement related.Document. The modern API expects either the current page (a Document) or an options map.

Common situations: Using the old positional RelatedIndices/RelatedTo calling convention against the newer Related method, or passing keyword arguments as a bare slice instead of types.KeyValues/options map.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/71cc09c466dd8743. Report an issue: GitHub.

Appendix: source

Thrown at resources/page/pages_related.go:69

}

// Related searches all the configured indices with the search keywords from the
// supplied document.
func (p Pages) Related(ctx context.Context, optsv any) (Pages, error) {
	if len(p) == 0 {
		return nil, nil
	}

	var opts related.SearchOpts
	switch v := optsv.(type) {
	case related.Document:
		opts.Document = v
	case map[string]any:
		if err := mapstructure.WeakDecode(v, &opts); err != nil {
			return nil, err
		}
	default:
		return nil, fmt.Errorf("invalid argument type %T", optsv)
	}

	result, err := p.search(ctx, opts)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// RelatedIndices searches the given indices with the search keywords from the
// supplied document.
// Deprecated: Use Related instead.
func (p Pages) RelatedIndices(ctx context.Context, doc related.Document, indices ...any) (Pages, error) {
	indicesStr, err := cast.ToStringSliceE(indices)
	if err != nil {
		return nil, err
	}

View on GitHub (pinned to 52c9bd7908)